Skip to content

Commit

Permalink
Fix: Ignore words in info string after syntax (fixes #166) (#167)
Browse files Browse the repository at this point in the history
* Fix: Ignore words in info string after syntax (fixes #166)

This was working correctly in v1 but stopped working when v2 switched to
the new processor API. The generated filename was `0.js more words are
ignored`. The test case for this was insufficiently strict: it asserted
that the code block was extracted, which was sufficient with hard-coded
extensions in v1, but it didn't assert the generated name was `0.js` now
that we're taking the extension from the info string.

Per CommonMark 0.29 [1]:

> The line with the opening code fence may optionally contain some text
> following the code fence; this is trimmed of leading and trailing
> whitespace and called the info string.

I've strengthened the tests for generated filename extensions and added
a new test to ensure that leading whitespace is trimmed correctly.

[1]: https://spec.commonmark.org/0.29/#info-string

* Use replace instead of trimStart for Node.js 8.x

* Ignore trailing whitespace in info strings

Markdown info strings permit trimming both leading and trailing
whitespace, so this shouldn't cause issues. `.trim()` is much nicer than
a regex `.replace()`.
  • Loading branch information
btmills authored Dec 20, 2020
1 parent 8f729d3 commit 23ac2b9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ function preprocess(text) {
});

return blocks.map((block, index) => ({
filename: `${index}.${block.lang}`,
filename: `${index}.${block.lang.trim().split(" ")[0]}`,
text: [
...block.comments,
block.value,
Expand Down
32 changes: 32 additions & 0 deletions tests/lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ describe("processor", () => {
const blocks = processor.preprocess(code);

assert.strictEqual(blocks.length, 1);
assert.strictEqual(blocks[0].filename, "0.js");
});

it("should find code fences with javascript info string", () => {
Expand All @@ -233,6 +234,7 @@ describe("processor", () => {
const blocks = processor.preprocess(code);

assert.strictEqual(blocks.length, 1);
assert.strictEqual(blocks[0].filename, "0.javascript");
});

it("should find code fences with node info string", () => {
Expand All @@ -244,6 +246,7 @@ describe("processor", () => {
const blocks = processor.preprocess(code);

assert.strictEqual(blocks.length, 1);
assert.strictEqual(blocks[0].filename, "0.node");
});

it("should find code fences with jsx info string", () => {
Expand All @@ -255,6 +258,7 @@ describe("processor", () => {
const blocks = processor.preprocess(code);

assert.strictEqual(blocks.length, 1);
assert.strictEqual(blocks[0].filename, "0.jsx");
});

it("should find code fences ignoring info string case", () => {
Expand All @@ -266,6 +270,7 @@ describe("processor", () => {
const blocks = processor.preprocess(code);

assert.strictEqual(blocks.length, 1);
assert.strictEqual(blocks[0].filename, "0.JavaScript");
});

it("should ignore anything after the first word of the info string", () => {
Expand All @@ -277,6 +282,31 @@ describe("processor", () => {
const blocks = processor.preprocess(code);

assert.strictEqual(blocks.length, 1);
assert.strictEqual(blocks[0].filename, "0.js");
});

it("should ignore leading whitespace in the info string", () => {
const code = [
"``` js ignores leading whitespace",
"var answer = 6 * 7;",
"```"
].join("\n");
const blocks = processor.preprocess(code);

assert.strictEqual(blocks.length, 1);
assert.strictEqual(blocks[0].filename, "0.js");
});

it("should ignore trailing whitespace in the info string", () => {
const code = [
"```js ",
"var answer = 6 * 7;",
"```"
].join("\n");
const blocks = processor.preprocess(code);

assert.strictEqual(blocks.length, 1);
assert.strictEqual(blocks[0].filename, "0.js");
});

it("should find code fences not surrounded by blank lines", () => {
Expand All @@ -293,6 +323,8 @@ describe("processor", () => {
const blocks = processor.preprocess(code);

assert.strictEqual(blocks.length, 2);
assert.strictEqual(blocks[0].filename, "0.js");
assert.strictEqual(blocks[1].filename, "1.js");
});

it("should return the source code in the block", () => {
Expand Down

0 comments on commit 23ac2b9

Please sign in to comment.