Skip to content

Commit

Permalink
reflow: fix case for trailing content after unbreakable node
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed May 4, 2021
1 parent fd5a1ec commit 339f6ea
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
10 changes: 5 additions & 5 deletions markdown/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { diff } from "../lib/diff.ts";

import format from "./format.ts";

// run a simple format with default remark to ensure everything works
/**
* Run a simple format with default remark to ensure everything works in the general case.
*
* If this test fails, use the result to derive a more specific test in the appropriate plugin.
*/
const testSuite = "markdown/format";
const input = `---
field: wow
Expand All @@ -26,10 +30,6 @@ $$
L = \frac{1}{2} \rho v^2 S C_L
$$
`;

/**
* TODO: this currently fails with redundant newlines between nodes. Not sure why yet.
*/
const want = `---
field: wow
---
Expand Down
23 changes: 23 additions & 0 deletions plugins/readable/reflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ And another one.`,
expect: `# Document
A short \`sentence\`! And another one.
`,
},
{
case: "short sentence followed by long sentence should be broken",
input: `# Document
A short \`sentence\`! This next sentence is long, with [a link](https://bobheadxi.dev) and **emphasis** and *italics* and ~~strike~~ and a ![cute image](https://bobheadxi.dev/assets/images/profile.jpg).`,
expect: `# Document
A short \`sentence\`!
This next sentence is long, with [a link](https://bobheadxi.dev) and **emphasis** and *italics* and ~~strike~~ and a ![cute image](https://bobheadxi.dev/assets/images/profile.jpg).
`,
},
{
case: "handle trailing punctuation after unbreakable",
input:
`This is a sentence with a trailing period [after an unbreakable node]().
The next sentence is here.`,
expect:
`This is a sentence with a trailing period [after an unbreakable node]().
The next sentence is here.
`,
},
]);
Expand Down
27 changes: 22 additions & 5 deletions plugins/readable/reflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,22 @@ class ReflowParagraphState {
/**
* Export the current lines.
*/
render(): string {
const rendered = this.lines.map((line) => line.join(" ")).join("\n");
render(opts?: { isLastChild?: boolean; isUnbreakable?: boolean }): string {
// If this is the last child, drop trailing empty line before rendering.
if (
opts?.isLastChild && this.lines.length > 1 &&
this.currentLine.length === 0
) {
this.lines.pop();
}

let rendered = this.lines.map((line) => line.join(" ")).join("\n");

// Preserve spacing if this is an unbreakable node.
if (opts?.isUnbreakable) {
rendered += " ";
}

console.debug(
`Rendered ${JSON.stringify(this.lines)} to '${escapeContent(rendered)}'`,
);
Expand Down Expand Up @@ -243,6 +257,7 @@ function reflowParagraph(paragraph: ParentNode) {

// Process all text in this tree
for (let i = 0; i < tree.children.length; ++i) {
const isLastChild = i === (tree.children.length - 1);
const current = tree.children[i];

switch (current.type) {
Expand All @@ -253,7 +268,7 @@ function reflowParagraph(paragraph: ParentNode) {
continue;
}
state.addText(current.value, isTreePlain);
current.value = state.render();
current.value = state.render({ isLastChild });
console.debug("Added text node", state.getState());
break;

Expand All @@ -266,8 +281,10 @@ function reflowParagraph(paragraph: ParentNode) {
const shouldBreakLine = state.addRawNode(current);
if (shouldBreakLine && previous) {
if (isLiteralNode(previous)) {
// Preserve spacing
previous.value = state.render() + " ";
previous.value = state.render({
isLastChild,
isUnbreakable: true,
});
} else {
tree.children.splice(i, 0, newTextNode("\n", previous.position));
}
Expand Down

0 comments on commit 339f6ea

Please sign in to comment.