Skip to content

Commit 302edfb

Browse files
committed
fix(common) handles empty headings in markdown translation
Signed-off-by: Jerome Simeon <[email protected]>
1 parent 7173851 commit 302edfb

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

packages/markdown-common/src/ToMarkdownStringVisitor.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,18 @@ class ToMarkdownStringVisitor {
197197
break;
198198
case 'Heading': {
199199
const level = parseInt(thing.level);
200-
if (level < 3) {
200+
const headingText = ToMarkdownStringVisitor.visitChildren(this, thing, parameters);
201+
if (level < 3 && headingText !== '') {
201202
parameters.result += ToMarkdownStringVisitor.mkPrefix(parameters,2);
202-
parameters.result += ToMarkdownStringVisitor.visitChildren(this, thing, parameters);
203+
parameters.result += headingText;
203204
parameters.first = false;
204205
parameters.result += ToMarkdownStringVisitor.mkPrefix(parameters,1);
205206
parameters.result += ToMarkdownStringVisitor.mkSetextHeading(level);
206207
} else {
207208
parameters.result += ToMarkdownStringVisitor.mkPrefix(parameters,2);
208209
parameters.result += ToMarkdownStringVisitor.mkATXHeading(level);
209210
parameters.result += ' ';
210-
parameters.result += ToMarkdownStringVisitor.visitChildren(this, thing, parameters);
211+
parameters.result += headingText;
211212
}
212213
}
213214
break;

packages/markdown-common/src/__snapshots__/CommonMarkTransformer.test.js.snap

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,43 @@ Object {
12551255
}
12561256
`;
12571257

1258+
exports[`markdown converts heading-empty.md to concerto JSON 1`] = `
1259+
Object {
1260+
"$class": "org.accordproject.commonmark.Document",
1261+
"nodes": Array [
1262+
Object {
1263+
"$class": "org.accordproject.commonmark.Heading",
1264+
"level": "1",
1265+
},
1266+
Object {
1267+
"$class": "org.accordproject.commonmark.Heading",
1268+
"level": "2",
1269+
},
1270+
Object {
1271+
"$class": "org.accordproject.commonmark.Heading",
1272+
"level": "1",
1273+
"nodes": Array [
1274+
Object {
1275+
"$class": "org.accordproject.commonmark.Text",
1276+
"text": "x",
1277+
},
1278+
],
1279+
},
1280+
Object {
1281+
"$class": "org.accordproject.commonmark.Heading",
1282+
"level": "2",
1283+
"nodes": Array [
1284+
Object {
1285+
"$class": "org.accordproject.commonmark.Text",
1286+
"text": "x",
1287+
},
1288+
],
1289+
},
1290+
],
1291+
"xmlns": "http://commonmark.org/xml/1.0",
1292+
}
1293+
`;
1294+
12581295
exports[`markdown converts headingdigit.md to concerto JSON 1`] = `
12591296
Object {
12601297
"$class": "org.accordproject.commonmark.Document",
@@ -22720,6 +22757,12 @@ exports[`to plain text h5.md 1`] = `"Heading Five"`;
2272022757

2272122758
exports[`to plain text h6.md 1`] = `"Heading Six"`;
2272222759

22760+
exports[`to plain text heading-empty.md 1`] = `
22761+
"x
22762+
22763+
x"
22764+
`;
22765+
2272322766
exports[`to plain text headingdigit.md 1`] = `
2272422767
"1\\\\. This is a heading with a digit
2272522768

packages/markdown-common/src/removeFormatting.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ function mapObject(obj, stack) {
3939
$class: 'org.accordproject.commonmark.Paragraph',
4040
nodes: []
4141
});
42-
obj.nodes.forEach(element => {
43-
mapObject(element, stack);
44-
});
42+
if (obj.nodes) {
43+
obj.nodes.forEach(element => {
44+
mapObject(element, stack);
45+
});
46+
}
4547
stack.pop();
4648
break;
4749
// wrap in a para and grab text
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
3+
##
4+
5+
# x
6+
7+
## x

0 commit comments

Comments
 (0)