Skip to content

Commit

Permalink
Fix: Ignore words in info string after syntax (fixes #166)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
btmills committed Dec 16, 2020
1 parent d30c50f commit 1b9a268
Show file tree
Hide file tree
Showing 2 changed files with 21 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.trimStart().split(" ")[0]}`,
text: [
...block.comments,
block.value,
Expand Down
20 changes: 20 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,19 @@ 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 find code fences not surrounded by blank lines", () => {
Expand All @@ -293,6 +311,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 1b9a268

Please sign in to comment.