diff --git a/lib/processor.js b/lib/processor.js index 81a2a970..41f6f7a2 100644 --- a/lib/processor.js +++ b/lib/processor.js @@ -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, diff --git a/tests/lib/processor.js b/tests/lib/processor.js index 9b6e500e..b3b596b4 100644 --- a/tests/lib/processor.js +++ b/tests/lib/processor.js @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -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", () => {