Skip to content

Commit 0818b78

Browse files
committed
fix(headings) Properly add new lines when transitioning to headings #45
Signed-off-by: Jerome Simeon <[email protected]>
1 parent 2a647da commit 0818b78

File tree

3 files changed

+353
-9
lines changed

3 files changed

+353
-9
lines changed

packages/markdown-common/src/ToMarkdownStringVisitor.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ class ToMarkdownStringVisitor {
118118
/**
119119
* Prints a new paragraph if not first
120120
* @param {*} parameters - the current parameters
121+
* @param {*} level - number of new lines
121122
*/
122-
static newParagraph(parameters) {
123-
if (!parameters.first) {
124-
parameters.result += '\n\n';
125-
}
123+
static newBlock(parameters,level) {
124+
const newlines = parameters.first ? '' : Array(level).fill('\n').join('');
125+
parameters.result += newlines;
126126
}
127127

128128
/**
@@ -134,7 +134,7 @@ class ToMarkdownStringVisitor {
134134

135135
switch(thing.getType()) {
136136
case 'CodeBlock':
137-
ToMarkdownStringVisitor.newParagraph(parameters);
137+
ToMarkdownStringVisitor.newBlock(parameters,2);
138138
parameters.result += `\`\`\`${thing.info ? ' ' + thing.info : ''}\n${thing.text}\`\`\`\n\n`;
139139
break;
140140
case 'Code':
@@ -151,11 +151,12 @@ class ToMarkdownStringVisitor {
151151
break;
152152
case 'BlockQuote': {
153153
const parametersIn = ToMarkdownStringVisitor.mkParametersIn(parameters);
154-
ToMarkdownStringVisitor.newParagraph(parameters);
154+
ToMarkdownStringVisitor.newBlock(parameters,2);
155155
parameters.result += `> ${ToMarkdownStringVisitor.visitChildren(this, thing, parametersIn)}`;
156156
}
157157
break;
158158
case 'Heading': {
159+
ToMarkdownStringVisitor.newBlock(parameters,2);
159160
const level = parseInt(thing.level);
160161
if (level < 3) {
161162
parameters.result += `${ToMarkdownStringVisitor.visitChildren(this, thing)}\n${ToMarkdownStringVisitor.mkSetextHeading(level)}`;
@@ -165,7 +166,7 @@ class ToMarkdownStringVisitor {
165166
}
166167
break;
167168
case 'ThematicBreak':
168-
ToMarkdownStringVisitor.newParagraph(parameters);
169+
ToMarkdownStringVisitor.newBlock(parameters,2);
169170
parameters.result += '---\n';
170171
break;
171172
case 'Linebreak':
@@ -181,11 +182,11 @@ class ToMarkdownStringVisitor {
181182
parameters.result += `![${ToMarkdownStringVisitor.visitChildren(this, thing)}](${thing.destination})`;
182183
break;
183184
case 'Paragraph':
184-
ToMarkdownStringVisitor.newParagraph(parameters);
185+
ToMarkdownStringVisitor.newBlock(parameters,2);
185186
parameters.result += `${ToMarkdownStringVisitor.visitChildren(this, thing)}`;
186187
break;
187188
case 'HtmlBlock':
188-
ToMarkdownStringVisitor.newParagraph(parameters);
189+
ToMarkdownStringVisitor.newBlock(parameters,2);
189190
parameters.result += `${thing.text}`;
190191
break;
191192
case 'Text':

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

Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,319 @@ Object {
352352
}
353353
`;
354354

355+
exports[`markdown converts editor.md to concerto JSON 1`] = `
356+
Object {
357+
"$class": "org.accordproject.commonmark.Document",
358+
"nodes": Array [
359+
Object {
360+
"$class": "org.accordproject.commonmark.Heading",
361+
"level": "1",
362+
"nodes": Array [
363+
Object {
364+
"$class": "org.accordproject.commonmark.Text",
365+
"text": "Heading One",
366+
},
367+
],
368+
},
369+
Object {
370+
"$class": "org.accordproject.commonmark.Paragraph",
371+
"nodes": Array [
372+
Object {
373+
"$class": "org.accordproject.commonmark.Text",
374+
"text": "This is text. This is ",
375+
},
376+
Object {
377+
"$class": "org.accordproject.commonmark.Emph",
378+
"nodes": Array [
379+
Object {
380+
"$class": "org.accordproject.commonmark.Text",
381+
"text": "italic",
382+
},
383+
],
384+
},
385+
Object {
386+
"$class": "org.accordproject.commonmark.Text",
387+
"text": " text. This is ",
388+
},
389+
Object {
390+
"$class": "org.accordproject.commonmark.Strong",
391+
"nodes": Array [
392+
Object {
393+
"$class": "org.accordproject.commonmark.Text",
394+
"text": "bold",
395+
},
396+
],
397+
},
398+
Object {
399+
"$class": "org.accordproject.commonmark.Text",
400+
"text": " text. This is a ",
401+
},
402+
Object {
403+
"$class": "org.accordproject.commonmark.Link",
404+
"destination": "https://clause.io",
405+
"nodes": Array [
406+
Object {
407+
"$class": "org.accordproject.commonmark.Text",
408+
"text": "link",
409+
},
410+
],
411+
"title": "",
412+
},
413+
Object {
414+
"$class": "org.accordproject.commonmark.Text",
415+
"text": ". This is ",
416+
},
417+
Object {
418+
"$class": "org.accordproject.commonmark.Code",
419+
"text": "inline code",
420+
},
421+
Object {
422+
"$class": "org.accordproject.commonmark.Text",
423+
"text": ".",
424+
},
425+
],
426+
},
427+
Object {
428+
"$class": "org.accordproject.commonmark.Paragraph",
429+
"nodes": Array [
430+
Object {
431+
"$class": "org.accordproject.commonmark.Text",
432+
"text": "This is ",
433+
},
434+
Object {
435+
"$class": "org.accordproject.commonmark.Emph",
436+
"nodes": Array [
437+
Object {
438+
"$class": "org.accordproject.commonmark.Strong",
439+
"nodes": Array [
440+
Object {
441+
"$class": "org.accordproject.commonmark.Text",
442+
"text": "bold and italic",
443+
},
444+
],
445+
},
446+
],
447+
},
448+
Object {
449+
"$class": "org.accordproject.commonmark.Text",
450+
"text": " text",
451+
},
452+
],
453+
},
454+
Object {
455+
"$class": "org.accordproject.commonmark.BlockQuote",
456+
"nodes": Array [
457+
Object {
458+
"$class": "org.accordproject.commonmark.Paragraph",
459+
"nodes": Array [
460+
Object {
461+
"$class": "org.accordproject.commonmark.Text",
462+
"text": "This is a quote.",
463+
},
464+
],
465+
},
466+
],
467+
},
468+
Object {
469+
"$class": "org.accordproject.commonmark.Heading",
470+
"level": "2",
471+
"nodes": Array [
472+
Object {
473+
"$class": "org.accordproject.commonmark.Text",
474+
"text": "Heading Two",
475+
},
476+
],
477+
},
478+
Object {
479+
"$class": "org.accordproject.commonmark.Paragraph",
480+
"nodes": Array [
481+
Object {
482+
"$class": "org.accordproject.commonmark.Text",
483+
"text": "This is more text.",
484+
},
485+
],
486+
},
487+
Object {
488+
"$class": "org.accordproject.commonmark.Paragraph",
489+
"nodes": Array [
490+
Object {
491+
"$class": "org.accordproject.commonmark.Text",
492+
"text": "Ordered lists:",
493+
},
494+
],
495+
},
496+
Object {
497+
"$class": "org.accordproject.commonmark.List",
498+
"delimiter": "period",
499+
"nodes": Array [
500+
Object {
501+
"$class": "org.accordproject.commonmark.Item",
502+
"nodes": Array [
503+
Object {
504+
"$class": "org.accordproject.commonmark.Paragraph",
505+
"nodes": Array [
506+
Object {
507+
"$class": "org.accordproject.commonmark.Text",
508+
"text": "one",
509+
},
510+
],
511+
},
512+
],
513+
},
514+
Object {
515+
"$class": "org.accordproject.commonmark.Item",
516+
"nodes": Array [
517+
Object {
518+
"$class": "org.accordproject.commonmark.Paragraph",
519+
"nodes": Array [
520+
Object {
521+
"$class": "org.accordproject.commonmark.Text",
522+
"text": "two",
523+
},
524+
],
525+
},
526+
],
527+
},
528+
Object {
529+
"$class": "org.accordproject.commonmark.Item",
530+
"nodes": Array [
531+
Object {
532+
"$class": "org.accordproject.commonmark.Paragraph",
533+
"nodes": Array [
534+
Object {
535+
"$class": "org.accordproject.commonmark.Text",
536+
"text": "three",
537+
},
538+
],
539+
},
540+
],
541+
},
542+
],
543+
"start": "1",
544+
"tight": "true",
545+
"type": "ordered",
546+
},
547+
Object {
548+
"$class": "org.accordproject.commonmark.Paragraph",
549+
"nodes": Array [
550+
Object {
551+
"$class": "org.accordproject.commonmark.Text",
552+
"text": "Or:",
553+
},
554+
],
555+
},
556+
Object {
557+
"$class": "org.accordproject.commonmark.List",
558+
"nodes": Array [
559+
Object {
560+
"$class": "org.accordproject.commonmark.Item",
561+
"nodes": Array [
562+
Object {
563+
"$class": "org.accordproject.commonmark.Paragraph",
564+
"nodes": Array [
565+
Object {
566+
"$class": "org.accordproject.commonmark.Text",
567+
"text": "apples",
568+
},
569+
],
570+
},
571+
],
572+
},
573+
Object {
574+
"$class": "org.accordproject.commonmark.Item",
575+
"nodes": Array [
576+
Object {
577+
"$class": "org.accordproject.commonmark.Paragraph",
578+
"nodes": Array [
579+
Object {
580+
"$class": "org.accordproject.commonmark.Text",
581+
"text": "pears",
582+
},
583+
],
584+
},
585+
],
586+
},
587+
Object {
588+
"$class": "org.accordproject.commonmark.Item",
589+
"nodes": Array [
590+
Object {
591+
"$class": "org.accordproject.commonmark.Paragraph",
592+
"nodes": Array [
593+
Object {
594+
"$class": "org.accordproject.commonmark.Text",
595+
"text": "peaches",
596+
},
597+
],
598+
},
599+
],
600+
},
601+
],
602+
"tight": "true",
603+
"type": "bullet",
604+
},
605+
Object {
606+
"$class": "org.accordproject.commonmark.Heading",
607+
"level": "3",
608+
"nodes": Array [
609+
Object {
610+
"$class": "org.accordproject.commonmark.Text",
611+
"text": "Sub heading",
612+
},
613+
],
614+
},
615+
Object {
616+
"$class": "org.accordproject.commonmark.Paragraph",
617+
"nodes": Array [
618+
Object {
619+
"$class": "org.accordproject.commonmark.Text",
620+
"text": "Video:",
621+
},
622+
],
623+
},
624+
Object {
625+
"$class": "org.accordproject.commonmark.HtmlBlock",
626+
"tag": Object {
627+
"$class": "org.accordproject.commonmark.TagInfo",
628+
"attributeString": "",
629+
"attributes": Array [],
630+
"closed": true,
631+
"content": "",
632+
"tagName": "video",
633+
},
634+
"text": "<video/>",
635+
},
636+
Object {
637+
"$class": "org.accordproject.commonmark.Paragraph",
638+
"nodes": Array [
639+
Object {
640+
"$class": "org.accordproject.commonmark.Text",
641+
"text": "Another video:",
642+
},
643+
],
644+
},
645+
Object {
646+
"$class": "org.accordproject.commonmark.HtmlBlock",
647+
"tag": Object {
648+
"$class": "org.accordproject.commonmark.TagInfo",
649+
"attributeString": "src = \\"https://www.youtube.com/embed/cmmq-JBMbbQ\\" ",
650+
"attributes": Array [
651+
Object {
652+
"$class": "org.accordproject.commonmark.Attribute",
653+
"name": "src",
654+
"value": "https://www.youtube.com/embed/cmmq-JBMbbQ",
655+
},
656+
],
657+
"closed": true,
658+
"content": "",
659+
"tagName": "video",
660+
},
661+
"text": "<video src=\\"https://www.youtube.com/embed/cmmq-JBMbbQ\\"/>",
662+
},
663+
],
664+
"xmlns": "http://commonmark.org/xml/1.0",
665+
}
666+
`;
667+
355668
exports[`markdown converts emph.md to concerto JSON 1`] = `
356669
Object {
357670
"$class": "org.accordproject.commonmark.Document",

0 commit comments

Comments
 (0)