Skip to content

Commit

Permalink
feat: map known code block languages to respective file extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
DMartens committed Mar 27, 2024
1 parent bb5c3d4 commit e40891f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
29 changes: 21 additions & 8 deletions lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ function getBlockRangeMap(text, node, comments) {
return rangeMap;
}

const languageToFileExtension = {
javascript: "js",
node: "js",
ecmascript: "js",
typescript: "ts",
markdown: "md"
};

/**
* Extracts lintable code blocks from Markdown text.
* @param {string} text The text of the file.
Expand Down Expand Up @@ -295,14 +303,19 @@ function preprocess(text, filename) {
}
});

return blocks.map((block, index) => ({
filename: `${index}.${block.lang.trim().split(" ")[0]}`,
text: [
...block.comments,
block.value,
""
].join("\n")
}));
return blocks.map((block, index) => {
const [language] = block.lang.trim().split(" ");
const fileExtension = Object.hasOwn(languageToFileExtension, language) ? languageToFileExtension[language] : language;

return {
filename: `${index}.${fileExtension}`,
text: [
...block.comments,
block.value,
""
].join("\n")
};
});
}

/**
Expand Down
24 changes: 18 additions & 6 deletions tests/lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe("processor", () => {
"```js",
"backticks",
"```",
"~~~javascript",
"~~~js",
"tildes",
"~~~"
].join("\n");
Expand All @@ -124,7 +124,7 @@ describe("processor", () => {
assert.strictEqual(blocks.length, 2);
assert.strictEqual(blocks[0].filename, "0.js");
assert.strictEqual(blocks[0].text, "backticks\n");
assert.strictEqual(blocks[1].filename, "1.javascript");
assert.strictEqual(blocks[1].filename, "1.js");
assert.strictEqual(blocks[1].text, "tildes\n");
});

Expand Down Expand Up @@ -255,7 +255,7 @@ describe("processor", () => {
const blocks = processor.preprocess(code);

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

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

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

it("should find code fences with jsx info string", () => {
Expand Down Expand Up @@ -330,6 +330,18 @@ describe("processor", () => {
assert.strictEqual(blocks[0].filename, "0.js");
});

it("should the language to its file extension with leading whitespace and trailing characters", () => {
const code = [
"``` javascript CUSTOM",
"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", () => {
const code = [
"<!-- eslint-disable -->",
Expand Down Expand Up @@ -408,7 +420,7 @@ describe("processor", () => {
"var answer = 6 * 7;",
"```",
"",
"```javascript",
"```js",
"console.log(answer);",
"```",
"",
Expand All @@ -419,7 +431,7 @@ describe("processor", () => {
assert.strictEqual(blocks.length, 2);
assert.strictEqual(blocks[0].filename, "0.js");
assert.strictEqual(blocks[0].text, "var answer = 6 * 7;\n");
assert.strictEqual(blocks[1].filename, "1.javascript");
assert.strictEqual(blocks[1].filename, "1.js");
assert.strictEqual(blocks[1].text, "console.log(answer);\n");
});

Expand Down

0 comments on commit e40891f

Please sign in to comment.