diff --git a/packages/@atjson/renderer-commonmark/src/index.ts b/packages/@atjson/renderer-commonmark/src/index.ts index 2c0044e62..7cb67ad00 100644 --- a/packages/@atjson/renderer-commonmark/src/index.ts +++ b/packages/@atjson/renderer-commonmark/src/index.ts @@ -474,6 +474,10 @@ export default class CommonmarkRenderer extends Renderer { return ""; } + if (context.parent == null && context.next == null) { + return ""; + } + // MD code and html blocks cannot contain line breaks // https://spec.commonmark.org/0.29/#example-637 if (context.parent?.type === "code" || context.parent?.type === "html") { diff --git a/packages/@atjson/renderer-commonmark/test/__snapshots__/commonmark-spec.test.ts.snap b/packages/@atjson/renderer-commonmark/test/__snapshots__/commonmark-spec.test.ts.snap index baa398c80..ce238b364 100644 --- a/packages/@atjson/renderer-commonmark/test/__snapshots__/commonmark-spec.test.ts.snap +++ b/packages/@atjson/renderer-commonmark/test/__snapshots__/commonmark-spec.test.ts.snap @@ -1098,6 +1098,42 @@ exports[`ATX headings foo } `; +exports[`Autolinks < http://foo.bar > + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..19]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [], + "text": "< http://foo.bar >", +} +`; + +exports[`Autolinks < http://foo.bar > + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..19]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [], + "text": "< http://foo.bar >", +} +`; + exports[`Autolinks < https://foo.bar > 1`] = ` { @@ -1458,6 +1494,168 @@ exports[`Autolinks } `; +exports[`Autolinks + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..11]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://../", + }, + "id": "M00000000", + "range": "(1..11]", + "type": "link", + }, + ], + "text": "http://../", +} +`; + +exports[`Autolinks + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..11]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://../", + }, + "id": "M00000000", + "range": "(1..11]", + "type": "link", + }, + ], + "text": "http://../", +} +`; + +exports[`Autolinks + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..23]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://example.com/%5C%5B%5C", + }, + "id": "M00000000", + "range": "(1..23]", + "type": "link", + }, + ], + "text": "http://example.com/\\[\\", +} +`; + +exports[`Autolinks + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..23]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://example.com/%5C%5B%5C", + }, + "id": "M00000000", + "range": "(1..23]", + "type": "link", + }, + ], + "text": "http://example.com/\\[\\", +} +`; + +exports[`Autolinks + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..46]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://foo.bar.baz/test?q=hello&id=22&boolean", + }, + "id": "M00000000", + "range": "(1..46]", + "type": "link", + }, + ], + "text": "http://foo.bar.baz/test?q=hello&id=22&boolean", +} +`; + +exports[`Autolinks + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..46]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://foo.bar.baz/test?q=hello&id=22&boolean", + }, + "id": "M00000000", + "range": "(1..46]", + "type": "link", + }, + ], + "text": "http://foo.bar.baz/test?q=hello&id=22&boolean", +} +`; + exports[`Autolinks 1`] = ` { @@ -1512,6 +1710,42 @@ exports[`Autolinks } `; +exports[`Autolinks + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..25]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [], + "text": "", +} +`; + +exports[`Autolinks + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..25]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [], + "text": "", +} +`; + exports[`Autolinks 1`] = ` { @@ -1944,6 +2178,42 @@ exports[`Autolinks foo@bar.example.com } `; +exports[`Autolinks http://example.com + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..19]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [], + "text": "http://example.com", +} +`; + +exports[`Autolinks http://example.com + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..19]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [], + "text": "http://example.com", +} +`; + exports[`Autolinks https://example.com 1`] = ` { @@ -2056,7 +2326,7 @@ exports[`Backslash escapes } `; -exports[`Backslash escapes +exports[`Backslash escapes 1`] = ` { "blocks": [ @@ -2064,7 +2334,7 @@ exports[`Backslash escapes "attributes": {}, "id": "B00000000", "parents": [], - "range": "(0..28]", + "range": "(0..27]", "selfClosing": false, "type": "paragraph", }, @@ -2072,18 +2342,18 @@ exports[`Backslash escapes "marks": [ { "attributes": { - "href": "https://example.com?find=%5C*", + "href": "http://example.com?find=%5C*", }, "id": "M00000000", - "range": "(1..28]", + "range": "(1..27]", "type": "link", }, ], - "text": "https://example.com?find=\\*", + "text": "http://example.com?find=\\*", } `; -exports[`Backslash escapes +exports[`Backslash escapes 2`] = ` { "blocks": [ @@ -2091,7 +2361,7 @@ exports[`Backslash escapes "attributes": {}, "id": "B00000000", "parents": [], - "range": "(0..28]", + "range": "(0..27]", "selfClosing": false, "type": "paragraph", }, @@ -2099,20 +2369,18 @@ exports[`Backslash escapes "marks": [ { "attributes": { - "href": "https://example.com?find=%5C*", + "href": "http://example.com?find=%5C*", }, "id": "M00000000", - "range": "(1..28]", + "range": "(1..27]", "type": "link", }, ], - "text": "https://example.com?find=\\*", + "text": "http://example.com?find=\\*", } `; -exports[`Backslash escapes [foo] - -[foo]: /bar\\* "ti\\*tle" +exports[`Backslash escapes 1`] = ` { "blocks": [ @@ -2120,7 +2388,7 @@ exports[`Backslash escapes [foo] "attributes": {}, "id": "B00000000", "parents": [], - "range": "(0..4]", + "range": "(0..28]", "selfClosing": false, "type": "paragraph", }, @@ -2128,21 +2396,18 @@ exports[`Backslash escapes [foo] "marks": [ { "attributes": { - "href": "/bar*", - "title": "ti*tle", + "href": "https://example.com?find=%5C*", }, "id": "M00000000", - "range": "(1..4]", + "range": "(1..28]", "type": "link", }, ], - "text": "foo", + "text": "https://example.com?find=\\*", } `; -exports[`Backslash escapes [foo] - -[foo]: /bar\\* "ti\\*tle" +exports[`Backslash escapes 2`] = ` { "blocks": [ @@ -2150,7 +2415,7 @@ exports[`Backslash escapes [foo] "attributes": {}, "id": "B00000000", "parents": [], - "range": "(0..4]", + "range": "(0..28]", "selfClosing": false, "type": "paragraph", }, @@ -2158,19 +2423,78 @@ exports[`Backslash escapes [foo] "marks": [ { "attributes": { - "href": "/bar*", - "title": "ti*tle", + "href": "https://example.com?find=%5C*", }, "id": "M00000000", - "range": "(1..4]", + "range": "(1..28]", "type": "link", }, ], - "text": "foo", + "text": "https://example.com?find=\\*", } `; -exports[`Backslash escapes [foo](/bar\\* "ti\\*tle") +exports[`Backslash escapes [foo] + +[foo]: /bar\\* "ti\\*tle" + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..4]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "/bar*", + "title": "ti*tle", + }, + "id": "M00000000", + "range": "(1..4]", + "type": "link", + }, + ], + "text": "foo", +} +`; + +exports[`Backslash escapes [foo] + +[foo]: /bar\\* "ti\\*tle" + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..4]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "/bar*", + "title": "ti*tle", + }, + "id": "M00000000", + "range": "(1..4]", + "type": "link", + }, + ], + "text": "foo", +} +`; + +exports[`Backslash escapes [foo](/bar\\* "ti\\*tle") 1`] = ` { "blocks": [ @@ -4756,6 +5080,60 @@ exports[`Code spans \` } `; +exports[`Code spans \` + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..21]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://foo.bar.%60baz", + }, + "id": "M00000000", + "range": "(1..20]", + "type": "link", + }, + ], + "text": "http://foo.bar.\`baz\`", +} +`; + +exports[`Code spans \` + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..21]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://foo.bar.%60baz", + }, + "id": "M00000000", + "range": "(1..20]", + "type": "link", + }, + ], + "text": "http://foo.bar.\`baz\`", +} +`; + exports[`Code spans \` 1`] = ` { @@ -5060,6 +5438,56 @@ exports[`Code spans \`\` } `; +exports[`Code spans \`\` + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..22]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": {}, + "id": "M00000000", + "range": "(1..17]", + "type": "code_inline", + }, + ], + "text": "\`", +} +`; + +exports[`Code spans \`\` + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..22]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": {}, + "id": "M00000000", + "range": "(1..17]", + "type": "code_inline", + }, + ], + "text": "\`", +} +`; + exports[`Code spans \`\` 1`] = ` { @@ -6642,6 +7070,60 @@ Asclepias physocarpa)", } `; +exports[`Emphasis and strong emphasis **a + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..24]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://foo.bar/?q=**", + }, + "id": "M00000000", + "range": "(4..24]", + "type": "link", + }, + ], + "text": "**ahttp://foo.bar/?q=**", +} +`; + +exports[`Emphasis and strong emphasis **a + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..24]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://foo.bar/?q=**", + }, + "id": "M00000000", + "range": "(4..24]", + "type": "link", + }, + ], + "text": "**ahttp://foo.bar/?q=**", +} +`; + exports[`Emphasis and strong emphasis **a 1`] = ` { @@ -9842,7 +10324,7 @@ exports[`Emphasis and strong emphasis ___foo__ } `; -exports[`Emphasis and strong emphasis __a +exports[`Emphasis and strong emphasis __a 1`] = ` { "blocks": [ @@ -9850,7 +10332,7 @@ exports[`Emphasis and strong emphasis __a "attributes": {}, "id": "B00000000", "parents": [], - "range": "(0..25]", + "range": "(0..24]", "selfClosing": false, "type": "paragraph", }, @@ -9858,18 +10340,18 @@ exports[`Emphasis and strong emphasis __a "marks": [ { "attributes": { - "href": "https://foo.bar/?q=__", + "href": "http://foo.bar/?q=__", }, "id": "M00000000", - "range": "(4..25]", + "range": "(4..24]", "type": "link", }, ], - "text": "__ahttps://foo.bar/?q=__", + "text": "__ahttp://foo.bar/?q=__", } `; -exports[`Emphasis and strong emphasis __a +exports[`Emphasis and strong emphasis __a 2`] = ` { "blocks": [ @@ -9877,7 +10359,7 @@ exports[`Emphasis and strong emphasis __a "attributes": {}, "id": "B00000000", "parents": [], - "range": "(0..25]", + "range": "(0..24]", "selfClosing": false, "type": "paragraph", }, @@ -9885,18 +10367,18 @@ exports[`Emphasis and strong emphasis __a "marks": [ { "attributes": { - "href": "https://foo.bar/?q=__", + "href": "http://foo.bar/?q=__", }, "id": "M00000000", - "range": "(4..25]", + "range": "(4..24]", "type": "link", }, ], - "text": "__ahttps://foo.bar/?q=__", + "text": "__ahttp://foo.bar/?q=__", } `; -exports[`Emphasis and strong emphasis __foo __bar__ baz__ +exports[`Emphasis and strong emphasis __a 1`] = ` { "blocks": [ @@ -9904,31 +10386,54 @@ exports[`Emphasis and strong emphasis __foo __bar__ baz__ "attributes": {}, "id": "B00000000", "parents": [], - "range": "(0..12]", + "range": "(0..25]", "selfClosing": false, "type": "paragraph", }, ], "marks": [ { - "attributes": {}, + "attributes": { + "href": "https://foo.bar/?q=__", + }, "id": "M00000000", - "range": "(1..12]", - "type": "strong", + "range": "(4..25]", + "type": "link", }, + ], + "text": "__ahttps://foo.bar/?q=__", +} +`; + +exports[`Emphasis and strong emphasis __a + 2`] = ` +{ + "blocks": [ { "attributes": {}, - "id": "M00000001", - "range": "(5..8]", - "type": "strong", + "id": "B00000000", + "parents": [], + "range": "(0..25]", + "selfClosing": false, + "type": "paragraph", }, ], - "text": "foo bar baz", + "marks": [ + { + "attributes": { + "href": "https://foo.bar/?q=__", + }, + "id": "M00000000", + "range": "(4..25]", + "type": "link", + }, + ], + "text": "__ahttps://foo.bar/?q=__", } `; exports[`Emphasis and strong emphasis __foo __bar__ baz__ - 2`] = ` + 1`] = ` { "blocks": [ { @@ -9958,8 +10463,39 @@ exports[`Emphasis and strong emphasis __foo __bar__ baz__ } `; -exports[`Emphasis and strong emphasis __foo _bar_ baz__ - 1`] = ` +exports[`Emphasis and strong emphasis __foo __bar__ baz__ + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..12]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": {}, + "id": "M00000000", + "range": "(1..12]", + "type": "strong", + }, + { + "attributes": {}, + "id": "M00000001", + "range": "(5..8]", + "type": "strong", + }, + ], + "text": "foo bar baz", +} +`; + +exports[`Emphasis and strong emphasis __foo _bar_ baz__ + 1`] = ` { "blocks": [ { @@ -24244,6 +24780,118 @@ exports[`Links [foo*]: /url } `; +exports[`Links [foo + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..38]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://example.com/?search=%5D(uri)", + }, + "id": "M00000000", + "range": "(5..38]", + "type": "link", + }, + ], + "text": "[foohttp://example.com/?search=](uri)", +} +`; + +exports[`Links [foo + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..38]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://example.com/?search=%5D(uri)", + }, + "id": "M00000000", + "range": "(5..38]", + "type": "link", + }, + ], + "text": "[foohttp://example.com/?search=](uri)", +} +`; + +exports[`Links [foo + +[ref]: /uri + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..38]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://example.com/?search=%5D%5Bref%5D", + }, + "id": "M00000000", + "range": "(5..38]", + "type": "link", + }, + ], + "text": "[foohttp://example.com/?search=][ref]", +} +`; + +exports[`Links [foo + +[ref]: /uri + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..38]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "http://example.com/?search=%5D%5Bref%5D", + }, + "id": "M00000000", + "range": "(5..38]", + "type": "link", + }, + ], + "text": "[foohttp://example.com/?search=][ref]", +} +`; + exports[`Links [foo 1`] = ` { @@ -26254,6 +26902,132 @@ exports[`Links [link]("title") exports[`Links [link](#fragment) +[link](http://example.com#fragment) + +[link](http://example.com?foo=3#frag) + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..5]", + "selfClosing": false, + "type": "paragraph", + }, + { + "attributes": {}, + "id": "B00000001", + "parents": [], + "range": "(5..10]", + "selfClosing": false, + "type": "paragraph", + }, + { + "attributes": {}, + "id": "B00000002", + "parents": [], + "range": "(10..15]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "#fragment", + }, + "id": "M00000000", + "range": "(1..5]", + "type": "link", + }, + { + "attributes": { + "href": "http://example.com#fragment", + }, + "id": "M00000001", + "range": "(6..10]", + "type": "link", + }, + { + "attributes": { + "href": "http://example.com?foo=3#frag", + }, + "id": "M00000002", + "range": "(11..15]", + "type": "link", + }, + ], + "text": "linklinklink", +} +`; + +exports[`Links [link](#fragment) + +[link](http://example.com#fragment) + +[link](http://example.com?foo=3#frag) + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..5]", + "selfClosing": false, + "type": "paragraph", + }, + { + "attributes": {}, + "id": "B00000001", + "parents": [], + "range": "(5..10]", + "selfClosing": false, + "type": "paragraph", + }, + { + "attributes": {}, + "id": "B00000002", + "parents": [], + "range": "(10..15]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": { + "href": "#fragment", + }, + "id": "M00000000", + "range": "(1..5]", + "type": "link", + }, + { + "attributes": { + "href": "http://example.com#fragment", + }, + "id": "M00000001", + "range": "(6..10]", + "type": "link", + }, + { + "attributes": { + "href": "http://example.com?foo=3#frag", + }, + "id": "M00000002", + "range": "(11..15]", + "type": "link", + }, + ], + "text": "linklinklink", +} +`; + +exports[`Links [link](#fragment) + [link](https://example.com#fragment) [link](https://example.com?foo=3#frag) @@ -39780,6 +40554,96 @@ exports[`Raw HTML foo &<]]> } `; +exports[`Raw HTML foo + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..42]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [], + "text": "foo ", +} +`; + +exports[`Raw HTML foo + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..42]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [], + "text": "foo ", +} +`; + +exports[`Raw HTML foo + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..45]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": {}, + "id": "M00000000", + "range": "(5..45]", + "type": "html_inline", + }, + ], + "text": "foo ", +} +`; + +exports[`Raw HTML foo + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..45]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [ + { + "attributes": {}, + "id": "M00000000", + "range": "(5..45]", + "type": "html_inline", + }, + ], + "text": "foo ", +} +`; + exports[`Raw HTML foo 1`] = ` @@ -39836,6 +40700,62 @@ comment - with hyphens -->", exports[`Raw HTML foo foo --> +foo + 1`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..18]", + "selfClosing": false, + "type": "paragraph", + }, + { + "attributes": {}, + "id": "B00000001", + "parents": [], + "range": "(18..35]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [], + "text": "foo foo -->foo ", +} +`; + +exports[`Raw HTML foo foo --> + +foo + 2`] = ` +{ + "blocks": [ + { + "attributes": {}, + "id": "B00000000", + "parents": [], + "range": "(0..18]", + "selfClosing": false, + "type": "paragraph", + }, + { + "attributes": {}, + "id": "B00000001", + "parents": [], + "range": "(18..35]", + "selfClosing": false, + "type": "paragraph", + }, + ], + "marks": [], + "text": "foo foo -->foo ", +} +`; + +exports[`Raw HTML foo foo --> + foo foo --> 1`] = ` { diff --git a/packages/@atjson/renderer-commonmark/test/line-break.test.ts b/packages/@atjson/renderer-commonmark/test/line-break.test.ts new file mode 100644 index 000000000..763e09216 --- /dev/null +++ b/packages/@atjson/renderer-commonmark/test/line-break.test.ts @@ -0,0 +1,201 @@ +import CommonmarkRenderer from "../src"; + +describe("terminal line breaks are removed", () => { + test("in text", () => { + const document = { + text: "\ufffctest\ufffc", + blocks: [ + { + id: "B00000000", + type: "text", + parents: [], + selfClosing: false, + attributes: {}, + }, + { + id: "B00000000", + type: "line-break", + parents: ["text"], + selfClosing: true, + attributes: {}, + }, + ], + marks: [], + }; + + expect(CommonmarkRenderer.render(document)).toBe("test"); + }); + + test("in text with preceding linebreaks", () => { + const document = { + text: "\ufffctest\ufffctest\ufffc", + blocks: [ + { + id: "B00000000", + type: "text", + parents: [], + selfClosing: false, + attributes: {}, + }, + { + id: "B00000000", + type: "line-break", + parents: ["text"], + selfClosing: true, + attributes: {}, + }, + { + id: "B00000000", + type: "line-break", + parents: ["text"], + selfClosing: true, + attributes: {}, + }, + ], + marks: [], + }; + + expect(CommonmarkRenderer.render(document)).toBe("test\\\ntest"); + }); + + test.skip("in text terminating a mark", () => { + const document = { + text: "\ufffctest\ufffc", + blocks: [ + { + id: "B00000000", + type: "text", + parents: [], + selfClosing: false, + attributes: {}, + }, + { + id: "B00000000", + type: "line-break", + parents: ["text"], + selfClosing: true, + attributes: {}, + }, + ], + marks: [ + { + id: "M00000000", + type: "bold", + range: "(1..6]" as const, + attributes: {}, + }, + ], + }; + + expect(CommonmarkRenderer.render(document)).toBe("**test**"); + }); + + test("in paragraph", () => { + const document = { + text: "\ufffctest\ufffc", + blocks: [ + { + id: "B00000000", + type: "paragraph", + parents: [], + selfClosing: false, + attributes: {}, + }, + { + id: "B00000000", + type: "line-break", + parents: ["paragraph"], + selfClosing: true, + attributes: {}, + }, + ], + marks: [], + }; + + expect(CommonmarkRenderer.render(document)).toBe("test\n\n"); + }); +}); + +describe("are slash escaped", () => { + test("in text", () => { + const document = { + text: "\ufffcline 1\ufffcline 2", + blocks: [ + { + id: "B00000000", + type: "text", + parents: [], + selfClosing: false, + attributes: {}, + }, + { + id: "B00000000", + type: "line-break", + parents: ["text"], + selfClosing: true, + attributes: {}, + }, + ], + marks: [], + }; + + expect(CommonmarkRenderer.render(document)).toBe("line 1\\\nline 2"); + }); + + test("in text terminating a mark", () => { + const document = { + text: "\ufffcline 1\ufffcline 2", + blocks: [ + { + id: "B00000000", + type: "text", + parents: [], + selfClosing: false, + attributes: {}, + }, + { + id: "B00000000", + type: "line-break", + parents: ["text"], + selfClosing: true, + attributes: {}, + }, + ], + marks: [ + { + id: "M00000000", + type: "bold", + range: "(1..8]" as const, + attributes: {}, + }, + ], + }; + + expect(CommonmarkRenderer.render(document)).toBe("**line 1**\\\nline 2"); + }); + + test("in paragraph", () => { + const document = { + text: "\ufffcline 1\ufffcline 2", + blocks: [ + { + id: "B00000000", + type: "paragraph", + parents: [], + selfClosing: false, + attributes: {}, + }, + { + id: "B00000000", + type: "line-break", + parents: ["paragraph"], + selfClosing: true, + attributes: {}, + }, + ], + marks: [], + }; + + expect(CommonmarkRenderer.render(document)).toBe("line 1\\\nline 2\n\n"); + }); +});