Skip to content

Commit

Permalink
New: add support for meta info in code blocks
Browse files Browse the repository at this point in the history
Fixes: eslint#170
  • Loading branch information
aduh95 committed Feb 4, 2021
1 parent cdc9737 commit f6a8ca0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
// ...
Expand Down Expand Up @@ -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" }
}
]
};
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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}:*"
]
// ...
}
]
Expand Down
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}${block.meta ? `:${block.meta}` : ""}`,
text: [
...block.comments,
block.value,
Expand Down
10 changes: 5 additions & 5 deletions tests/lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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", () => {
Expand Down

0 comments on commit f6a8ca0

Please sign in to comment.