Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Ignore words in info string after syntax (fixes #166) #167

Merged
merged 3 commits into from
Dec 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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