From f6a8ca0824b8a62d0af2a9eb512104564588b49e Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 4 Feb 2021 12:04:04 +0100 Subject: [PATCH] New: add support for meta info in code blocks Fixes: https://github.com/eslint/eslint-plugin-markdown/issues/170 --- README.md | 16 ++++++++++++---- lib/processor.js | 2 +- tests/lib/processor.js | 10 +++++----- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0a487fde..09e932af 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ module.exports = { { // 3. Optionally, customize the configuration ESLint uses for ```js // fenced code blocks inside .md files. - files: ["**/*.md/*.js"], + files: ["**/*.md/*.js", "**/*.md/*.js:*"], // ... rules: { // ... @@ -87,12 +87,17 @@ module.exports = { // ... { // 1. Target ```js code blocks in .md files. - files: ["**/*.md/*.js"], + files: ["**/*.md/*.js", "**/*.md/*.js:*"], rules: { // 2. Disable other rules. "no-console": "off", "import/no-unresolved": "off" } + }, + { + // 2. Target "```js esm" code blocks + files: ["**/*.md/*.js:esm"], + parserOptions: { type: "module" } } ] }; @@ -167,7 +172,7 @@ module.exports = { // In v2, configuration for fenced code blocks is separate from the // containing Markdown file. Each code block has a virtual filename // appended to the Markdown file's path. - files: ["**/*.md/*.js"], + files: ["**/*.md/*.js", "**/*.md/*.js:*"], // Configuration for fenced code blocks goes with the override for // the code block's virtual filename, for example: parserOptions: { @@ -195,7 +200,10 @@ module.exports = { processor: "markdown/markdown" }, { - files: ["**/*.{md,mkdn,mdown,markdown}/*.{js,javascript,jsx,node}"] + files: [ + "**/*.{md,mkdn,mdown,markdown}/*.{js,javascript,jsx,node}", + "**/*.{md,mkdn,mdown,markdown}/*.{js,javascript,jsx,node}:*" + ] // ... } ] diff --git a/lib/processor.js b/lib/processor.js index 9268d068..34a75de6 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}${block.meta ? `:${block.meta}` : ""}`, text: [ ...block.comments, block.value, diff --git a/tests/lib/processor.js b/tests/lib/processor.js index f9a7c380..9dcf8bf6 100644 --- a/tests/lib/processor.js +++ b/tests/lib/processor.js @@ -273,16 +273,16 @@ describe("processor", () => { assert.strictEqual(blocks[0].filename, "0.JavaScript"); }); - it("should ignore anything after the first word of the info string", () => { + it("should should handle meta info", () => { const code = [ - "```js more words are ignored", - "var answer = 6 * 7;", + "```js esm", + "export {};", "```" ].join("\n"); const blocks = processor.preprocess(code); assert.strictEqual(blocks.length, 1); - assert.strictEqual(blocks[0].filename, "0.js"); + assert.strictEqual(blocks[0].filename, "0.js:esm"); }); it("should ignore leading whitespace in the info string", () => { @@ -294,7 +294,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.js:ignores leading whitespace"); }); it("should ignore trailing whitespace in the info string", () => {