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 41f6f7a2..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.trim().split(" ")[0]}`, + filename: `${index}.${block.lang}${block.meta ? `:${block.meta}` : ""}`, text: [ ...block.comments, block.value, @@ -286,7 +286,7 @@ function adjustBlock(block) { const out = { line: lineInCode + blockStart, - column: message.column + block.position.indent[lineInCode - 1] - 1 + column: message.column + block.baseIndentText.length }; if (Number.isInteger(message.endLine)) { diff --git a/package.json b/package.json index 7e399416..82ed1d8f 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "nyc": "^14.1.1" }, "dependencies": { - "remark-parse": "^5.0.0", + "remark-parse": "^9.0.0", "unified": "^6.1.2" }, "peerDependencies": { diff --git a/tests/lib/processor.js b/tests/lib/processor.js index b3b596b4..9dcf8bf6 100644 --- a/tests/lib/processor.js +++ b/tests/lib/processor.js @@ -199,7 +199,7 @@ describe("processor", () => { assert.strictEqual(blocks.length, 1); assert.strictEqual(blocks[0].filename, "0.js"); - assert.strictEqual(blocks[0].text, "\n\n \n \n"); + assert.strictEqual(blocks[0].text, "\n\n\n \n \n"); }); it("should ignore code fences with unspecified info string", () => { @@ -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", () => { @@ -362,7 +362,7 @@ describe("processor", () => { const blocks = processor.preprocess(code); assert.strictEqual(blocks[0].filename, "0.js"); - assert.strictEqual(blocks[0].text, "var answer = 6 * 7;\nconsole.log(answer);\n"); + assert.strictEqual(blocks[0].text, "var answer = 6 * 7;\r\nconsole.log(answer);\n"); }); it("should unindent space-indented code fences", () => { @@ -646,8 +646,8 @@ describe("processor", () => { const result = processor.postprocess(messages); assert.strictEqual(result[2].column, 9); - assert.strictEqual(result[3].column, 2); - assert.strictEqual(result[4].column, 2); + assert.strictEqual(result[3].column, 4); + assert.strictEqual(result[4].column, 4); }); it("should adjust fix range properties", () => {