Skip to content

Commit

Permalink
fix: don't render terminal linebreaks
Browse files Browse the repository at this point in the history
Our markdown syntax does not support teminal line-breaks, either
in blocks like a paragraph or just in text.

We had correctly been suppressing rendering the slash-escaped line
break when it appeared in a paragraph, but not in text
  • Loading branch information
bachbui committed Jun 26, 2024
1 parent 01a19fb commit 5fdf67c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 6 deletions.
7 changes: 1 addition & 6 deletions packages/@atjson/renderer-commonmark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,7 @@ export default class CommonmarkRenderer extends Renderer {
*LineBreak(_: any, context: Context): Generator<void, string, string[]> {
// Line breaks cannot end markdown block elements or paragraphs
// https://spec.commonmark.org/0.29/#example-641
if (
context.parent &&
"parents" in context.parent &&
context.parent.parents &&
context.next == null
) {
if (!context.next) {
return "";
}

Expand Down
105 changes: 105 additions & 0 deletions packages/@atjson/renderer-commonmark/test/line-break.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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 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 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");
});
});

0 comments on commit 5fdf67c

Please sign in to comment.