From e40891fa0b7b25173e10092a5694948ca748ea3c Mon Sep 17 00:00:00 2001 From: Daniel Martens Date: Wed, 27 Mar 2024 18:31:37 +0100 Subject: [PATCH 1/7] feat: map known code block languages to respective file extensions --- lib/processor.js | 29 +++++++++++++++++++++-------- tests/lib/processor.js | 24 ++++++++++++++++++------ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/lib/processor.js b/lib/processor.js index 4cf6a71f..01712e5d 100644 --- a/lib/processor.js +++ b/lib/processor.js @@ -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. @@ -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") + }; + }); } /** diff --git a/tests/lib/processor.js b/tests/lib/processor.js index 629c448e..68a2b24f 100644 --- a/tests/lib/processor.js +++ b/tests/lib/processor.js @@ -115,7 +115,7 @@ describe("processor", () => { "```js", "backticks", "```", - "~~~javascript", + "~~~js", "tildes", "~~~" ].join("\n"); @@ -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"); }); @@ -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", () => { @@ -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", () => { @@ -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 = [ "", @@ -408,7 +420,7 @@ describe("processor", () => { "var answer = 6 * 7;", "```", "", - "```javascript", + "```js", "console.log(answer);", "```", "", @@ -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"); }); From df4ef23461a6bfbf0a2a43eec671b3e5cb9135d9 Mon Sep 17 00:00:00 2001 From: Daniel Martens Date: Sat, 30 Mar 2024 11:38:43 +0100 Subject: [PATCH 2/7] chore: add missing verb in test case description --- tests/lib/processor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib/processor.js b/tests/lib/processor.js index 68a2b24f..a9cfe205 100644 --- a/tests/lib/processor.js +++ b/tests/lib/processor.js @@ -330,7 +330,7 @@ describe("processor", () => { assert.strictEqual(blocks[0].filename, "0.js"); }); - it("should the language to its file extension with leading whitespace and trailing characters", () => { + it("should translate the language to its file extension with leading whitespace and trailing characters", () => { const code = [ "``` javascript CUSTOM", "var answer = 6 * 7;", From ccbcc7f2fae54316ea12e9d1924a25d0b28f395b Mon Sep 17 00:00:00 2001 From: Daniel Martens Date: Sat, 30 Mar 2024 11:53:34 +0100 Subject: [PATCH 3/7] chore: resolve README lint errors --- eslint.config.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/eslint.config.js b/eslint.config.js index dc073903..4234697c 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -41,7 +41,13 @@ module.exports = [ }, rules: { "lines-around-comment": "off", - "n/no-missing-import": "off" + "n/no-missing-import": "off", + "no-var": "off", + "padding-line-between-statements": "off", + "no-console": "off", + "no-alert": "off", + "eslint-comments/require-description": "off", + "jsdoc/require-jsdoc": "off" } } ]; From a9552ad2ec1e6d367b18f8a88da978f642b9466a Mon Sep 17 00:00:00 2001 From: Daniel Martens Date: Mon, 8 Apr 2024 18:10:34 +0200 Subject: [PATCH 4/7] fix: remove mapping from code block language node to javascript --- lib/processor.js | 1 - tests/lib/processor.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/processor.js b/lib/processor.js index 01712e5d..433e7509 100644 --- a/lib/processor.js +++ b/lib/processor.js @@ -237,7 +237,6 @@ function getBlockRangeMap(text, node, comments) { const languageToFileExtension = { javascript: "js", - node: "js", ecmascript: "js", typescript: "ts", markdown: "md" diff --git a/tests/lib/processor.js b/tests/lib/processor.js index a9cfe205..9c87a3d5 100644 --- a/tests/lib/processor.js +++ b/tests/lib/processor.js @@ -267,7 +267,7 @@ describe("processor", () => { const blocks = processor.preprocess(code); assert.strictEqual(blocks.length, 1); - assert.strictEqual(blocks[0].filename, "0.js"); + assert.strictEqual(blocks[0].filename, "0.node"); }); it("should find code fences with jsx info string", () => { From a7d2783abdb76414707e7fb2d536d2e9b2d8d690 Mon Sep 17 00:00:00 2001 From: Daniel Martens Date: Fri, 12 Apr 2024 16:45:55 +0200 Subject: [PATCH 5/7] docs: explain code block language mapping --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3d9f9883..e304f173 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,10 @@ You can manually include the Markdown processor by setting the `processor` optio Each fenced code block inside a Markdown document has a virtual filename appended to the Markdown file's path. -The virtual filename's extension will match the fenced code block's syntax tag, so for example, ```js code blocks in README.md would match README.md/*.js. +The virtual filename's extension will match the fenced code block's syntax tag, except for the following: `javascript`, `ecmascript`, `typescript` and `markdown`. +The exceptions are mapped to their shorter syntax tag variants: `js`, `js`, `ts` and `md` respectively. +So for example, ```js code blocks in README.md would match README.md/*.js and ``typescript in CONTRIBUTING.md would match CONTRIBUTING.md/*.ts. + You can use glob patterns for these virtual filenames to customize configuration for code blocks without affecting regular code. For more information on configuring processors, refer to the [ESLint documentation](https://eslint.org/docs/user-guide/configuring#specifying-processor). From af81ab2d2aab8b95a7a198e28c9e918d4d78f26d Mon Sep 17 00:00:00 2001 From: fnx <966276+DMartens@users.noreply.github.com> Date: Sat, 13 Apr 2024 15:41:43 +0200 Subject: [PATCH 6/7] docs: make language mappings more explicit Co-authored-by: Nicholas C. Zakas --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e304f173..81fe7e9a 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,13 @@ You can manually include the Markdown processor by setting the `processor` optio Each fenced code block inside a Markdown document has a virtual filename appended to the Markdown file's path. -The virtual filename's extension will match the fenced code block's syntax tag, except for the following: `javascript`, `ecmascript`, `typescript` and `markdown`. -The exceptions are mapped to their shorter syntax tag variants: `js`, `js`, `ts` and `md` respectively. -So for example, ```js code blocks in README.md would match README.md/*.js and ``typescript in CONTRIBUTING.md would match CONTRIBUTING.md/*.ts. +The virtual filename's extension will match the fenced code block's syntax tag, except for the following: + +* `javascript` and `ecmascript` are mapped to `js` +* `typescript` is mapped to `ts` +* `markdown` is mapped to `md` + +For example, ```js code blocks in `README.md` would match `README.md/*.js` and ``typescript in `CONTRIBUTING.md` would match `CONTRIBUTING.md/*.ts`. You can use glob patterns for these virtual filenames to customize configuration for code blocks without affecting regular code. For more information on configuring processors, refer to the [ESLint documentation](https://eslint.org/docs/user-guide/configuring#specifying-processor). From 27b9e28416079e7622bb3dcc6c393410d09186ea Mon Sep 17 00:00:00 2001 From: fnx <966276+DMartens@users.noreply.github.com> Date: Mon, 29 Apr 2024 00:02:09 +0200 Subject: [PATCH 7/7] docs: use backticks instead of code blocks Co-authored-by: Francesco Trotta --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 81fe7e9a..bd2c497e 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ The virtual filename's extension will match the fenced code block's syntax tag, * `typescript` is mapped to `ts` * `markdown` is mapped to `md` -For example, ```js code blocks in `README.md` would match `README.md/*.js` and ``typescript in `CONTRIBUTING.md` would match `CONTRIBUTING.md/*.ts`. +For example, ```` ```js ```` code blocks in `README.md` would match `README.md/*.js` and ```` ```typescript ```` in `CONTRIBUTING.md` would match `CONTRIBUTING.md/*.ts`. You can use glob patterns for these virtual filenames to customize configuration for code blocks without affecting regular code. For more information on configuring processors, refer to the [ESLint documentation](https://eslint.org/docs/user-guide/configuring#specifying-processor).