Skip to content

Commit f7208c7

Browse files
committed
fix(template) Properly closes templatemark nodes when building from tokens
Signed-off-by: Jerome Simeon <[email protected]>
1 parent 011b3ee commit f7208c7

File tree

7 files changed

+49
-6
lines changed

7 files changed

+49
-6
lines changed

packages/markdown-common/lib/FromMarkdownIt.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,49 @@ class FromMarkdownIt {
3434
}
3535
}
3636

37+
/**
38+
* Takes the stack of constructed inline nodes
39+
* properly closing them (if the close token is missing in the markdown)
40+
* returns the final root node for the inline
41+
*
42+
* @param {*[]} rules - the rules for each kind of markdown-it tokens
43+
* @param {*[]} stack - the stack of constructed nodes
44+
* @returns {*} the final inline node
45+
*/
46+
static closeInlines(rules,stack) {
47+
let currentNode = stack.pop();
48+
while(stack.peek()) {
49+
const rule = Object.values(rules.inlines).find((x) => x.tag === currentNode.$class && x.exit);
50+
if (rule && rule.exit) {
51+
rule.exit(currentNode,null,FromMarkdownIt.inlineCallback(rules));
52+
}
53+
currentNode = stack.pop();
54+
}
55+
return mergeAdjacentHtmlNodes(currentNode.nodes,true);
56+
}
57+
3758
/**
3859
* Create a callback for inlines
3960
*
4061
* @param {*[]} rules - the rules for each kind of markdown-it tokens
4162
* @returns {*} the callback
4263
*/
4364
static inlineCallback(rules) {
44-
return (tokens) => FromMarkdownIt.inlineToCommonMark(rules,tokens);
65+
return (tokens) => {
66+
const stack = new Stack();
67+
FromMarkdownIt.inlineToCommonMark(rules,tokens,stack); // Updates the stack
68+
return FromMarkdownIt.closeInlines(rules,stack);
69+
};
4570
}
4671

4772
/**
4873
* Process an inline node to CommonMark DOM
4974
*
5075
* @param {*[]} rules - the rules for each kind of markdown-it tokens
5176
* @param {*} tokens - the content of the inline node
52-
* @returns {*} the CommonMark nodes
77+
* @param {*[]} stack - the stack of constructed nodes
5378
*/
54-
static inlineToCommonMark(rules,tokens) {
55-
let stack = new Stack();
79+
static inlineToCommonMark(rules,tokens,stack) {
5680
const rootNode = {
5781
'$class': 'org.accordproject.commonmark.Inline',
5882
'nodes': [],
@@ -87,8 +111,6 @@ class FromMarkdownIt {
87111
if (rule.enter) { rule.enter(node,token,FromMarkdownIt.inlineCallback(rules)); }
88112
}
89113
}
90-
91-
return mergeAdjacentHtmlNodes(rootNode.nodes,true);
92114
}
93115

94116
/**

packages/markdown-template/test/TemplateMarkTransformer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ const successes = [
5757
{name:'test10',kind:'clause'},
5858
{name:'test11',kind:'clause'},
5959
{name:'test12',kind:'clause'},
60+
{name:'test13',kind:'clause'},
61+
{name:'test14',kind:'clause'},
6062
{name:'testFormula',kind:'contract'},
6163
{name:'testDateTime',kind:'clause'},
6264
{name:'testDateTime2',kind:'clause'},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"$class":"org.test.MyClause","seller":"Steve","buyer":"Betty","amount":3131,"currency":"EUR","forceMajeure":true,"clauseId":"cf1cc82b-17c7-47da-ba65-0952dd5dd168"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"$class":"org.accordproject.commonmark.Document","xmlns":"http://commonmark.org/xml/1.0","nodes":[{"$class":"org.accordproject.templatemark.ClauseDefinition","name":"top","elementType":"org.test.MyClause","nodes":[{"$class":"org.accordproject.commonmark.Paragraph","nodes":[{"$class":"org.accordproject.commonmark.Text","text":"This is a contract between "},{"$class":"org.accordproject.templatemark.VariableDefinition","name":"seller","elementType":"String"},{"$class":"org.accordproject.commonmark.Text","text":" and "},{"$class":"org.accordproject.templatemark.VariableDefinition","name":"buyer","elementType":"String"},{"$class":"org.accordproject.commonmark.Text","text":" for the amount of "},{"$class":"org.accordproject.templatemark.VariableDefinition","name":"amount","elementType":"Double"},{"$class":"org.accordproject.commonmark.Text","text":" "},{"$class":"org.accordproject.templatemark.EnumVariableDefinition","enumValues":["AED","AFN","ALL","AMD","ANG","AOA","ARS","AUD","AWG","AZN","BAM","BBD","BDT","BGN","BHD","BIF","BMD","BND","BOB","BOV","BRL","BSD","BTN","BWP","BYN","BZD","CAD","CDF","CHE","CHF","CHW","CLF","CLP","CNY","COP","COU","CRC","CUC","CUP","CVE","CZK","DJF","DKK","DOP","DZD","EGP","ERN","ETB","EUR","FJD","FKP","GBP","GEL","GHS","GIP","GMD","GNF","GTQ","GYD","HKD","HNL","HRK","HTG","HUF","IDR","ILS","INR","IQD","IRR","ISK","JMD","JOD","JPY","KES","KGS","KHR","KMF","KPW","KRW","KWD","KYD","KZT","LAK","LBP","LKR","LRD","LSL","LYD","MAD","MDL","MGA","MKD","MMK","MNT","MOP","MRU","MUR","MVR","MWK","MXN","MXV","MYR","MZN","NAD","NGN","NIO","NOK","NPR","NZD","OMR","PAB","PEN","PGK","PHP","PKR","PLN","PYG","QAR","RON","RSD","RUB","RWF","SAR","SBD","SCR","SDG","SEK","SGD","SHP","SLL","SOS","SRD","SSP","STN","SVC","SYP","SZL","THB","TJS","TMT","TND","TOP","TRY","TTD","TWD","TZS","UAH","UGX","USD","USN","UYI","UYU","UZS","VEF","VND","VUV","WST","XAF","XAG","XAU","XBA","XBB","XBC","XBD","XCD","XDR","XOF","XPD","XPF","XPT","XSU","XTS","XUA","XXX","YER","ZAR","ZMW","ZWL"],"name":"currency","elementType":"org.accordproject.money.CurrencyCode"},{"$class":"org.accordproject.templatemark.ConditionalDefinition","whenTrue":[{"$class":"org.accordproject.commonmark.Text","text":", even in the presence of force majeure."}],"whenFalse":[],"name":"forceMajeure"}]}]}]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a contract between {{seller}} and {{buyer}} for the amount of {{amount}} {{currency}}{{#if forceMajeure}}, even in the presence of force majeure.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace org.test
2+
3+
import org.accordproject.cicero.contract.* from https://models.accordproject.org/cicero/contract.cto
4+
import org.accordproject.money.CurrencyCode from https://models.accordproject.org/[email protected]
5+
6+
/**
7+
* The template model
8+
*/
9+
asset MyClause extends AccordClause {
10+
o String seller
11+
o String buyer
12+
o Double amount
13+
o CurrencyCode currency
14+
o Boolean forceMajeure
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a contract between "Steve" and "Betty" for the amount of 3131.0 EUR, even in the presence of force majeure.

0 commit comments

Comments
 (0)