-
Notifications
You must be signed in to change notification settings - Fork 80
feat: add fenced-code-meta rule #512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TKDev7
wants to merge
8
commits into
eslint:main
Choose a base branch
from
TKDev7:fenced-code-meta
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2ab4991
feat: add fenced-code-meta rule
TKDev7 8312c6a
Merge branch 'main' into fenced-code-meta
TKDev7 97de75f
revert README.md changes
TKDev7 9c5c8cc
apply suggested changes from code review
TKDev7 2159a81
add more tests for indented code blocks
TKDev7 cbd7439
correct column reporting for indented fences
TKDev7 56555f9
Merge branch 'main' into fenced-code-meta
TKDev7 406e71f
apply suggestions
TKDev7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # fenced-code-meta | ||
|
|
||
| Require or disallow metadata for fenced code blocks. | ||
|
|
||
| ## Background | ||
|
|
||
| Fenced code blocks can include an [info string](https://spec.commonmark.org/0.31.2/#info-string) after the opening fence. The first word typically specifies the language (e.g., `js`). Many tools also support additional metadata after the language (separated by whitespace), such as titles or line highlighting parameters. This rule enforces a consistent policy for including such metadata. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule warns when the presence of metadata in a fenced code block's info string does not match the configured mode. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ````markdown | ||
| <!-- eslint markdown/fenced-code-meta: "error" --> | ||
|
|
||
| ```js | ||
| console.log("Hello, world!"); | ||
| ``` | ||
| ```` | ||
|
|
||
| ## Options | ||
|
|
||
| This rule accepts a single string option: | ||
|
|
||
| - `"always"` (default): Require metadata when a language is specified. | ||
| - `"never"`: Disallow metadata in the info string. | ||
|
|
||
| Examples of **incorrect** code when configured as `"fenced-code-meta": ["error", "always"]`: | ||
|
|
||
| ````markdown | ||
| <!-- eslint markdown/fenced-code-meta: ["error", "always"] --> | ||
|
|
||
| ```js | ||
| console.log("Hello, world!"); | ||
| ``` | ||
| ```` | ||
|
|
||
| Examples of **correct** code when configured as `"fenced-code-meta": ["error", "always"]`: | ||
|
|
||
| ````markdown | ||
| <!-- eslint markdown/fenced-code-meta: ["error", "always"] --> | ||
|
|
||
| ```js title="example.js" | ||
| console.log("Hello, world!"); | ||
| ``` | ||
| ```` | ||
|
|
||
| Examples of **incorrect** code when configured as `"fenced-code-meta": ["error", "never"]`: | ||
|
|
||
| ````markdown | ||
| <!-- eslint markdown/fenced-code-meta: ["error", "never"] --> | ||
|
|
||
| ```js title="example.js" | ||
| console.log("Hello, world!"); | ||
| ``` | ||
| ```` | ||
|
|
||
| Examples of **correct** code when configured as `"fenced-code-meta": ["error", "never"]`: | ||
|
|
||
| ````markdown | ||
| <!-- eslint markdown/fenced-code-meta: ["error", "never"] --> | ||
|
|
||
| ```js | ||
| console.log("Hello, world!"); | ||
| ``` | ||
| ```` | ||
|
|
||
| ## When Not to Use It | ||
|
|
||
| If you aren't concerned with metadata in info strings, you can safely disable this rule. | ||
|
|
||
| ## Prior Art | ||
|
|
||
| * [MD040 - Fenced code blocks should have a language specified](https://github.com/DavidAnson/markdownlint/blob/main/doc/md040.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /** | ||
| * @fileoverview Rule to require or disallow metadata for fenced code blocks. | ||
| * @author TKDev7 | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Type Definitions | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * @import { MarkdownRuleDefinition } from "../types.js"; | ||
| * @typedef {"missingMetadata" | "disallowedMetadata"} FencedCodeMetaMessageIds | ||
| * @typedef {["always" | "never"]} FencedCodeMetaOptions | ||
| * @typedef {MarkdownRuleDefinition<{ RuleOptions: FencedCodeMetaOptions, MessageIds: FencedCodeMetaMessageIds }>} FencedCodeMetaRuleDefinition | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Rule Definition | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| /** @type {FencedCodeMetaRuleDefinition} */ | ||
| export default { | ||
| meta: { | ||
| type: "problem", | ||
|
|
||
| docs: { | ||
| recommended: false, | ||
| description: "Require or disallow metadata for fenced code blocks", | ||
| url: "https://github.com/eslint/markdown/blob/main/docs/rules/fenced-code-meta.md", | ||
| }, | ||
|
|
||
| messages: { | ||
| missingMetadata: "Missing code block metadata.", | ||
| disallowedMetadata: "Code block metadata is not allowed.", | ||
| }, | ||
|
|
||
| schema: [ | ||
| { | ||
| enum: ["always", "never"], | ||
| }, | ||
| ], | ||
|
|
||
| defaultOptions: ["always"], | ||
| }, | ||
|
|
||
| create(context) { | ||
| const [mode] = context.options; | ||
| const { sourceCode } = context; | ||
|
|
||
| return { | ||
| code(node) { | ||
| const lineText = sourceCode.lines[node.position.start.line - 1]; | ||
TKDev7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const fenceLineText = lineText.slice( | ||
| node.position.start.column - 1, | ||
| ); | ||
|
|
||
| if (mode === "always") { | ||
| if (node.lang && !node.meta) { | ||
| const langIndex = fenceLineText.indexOf(node.lang); | ||
|
|
||
| context.report({ | ||
| loc: { | ||
| start: node.position.start, | ||
| end: { | ||
| line: node.position.start.line, | ||
| column: | ||
| node.position.start.column + | ||
| langIndex + | ||
| node.lang.length, | ||
| }, | ||
| }, | ||
| messageId: "missingMetadata", | ||
| }); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (node.meta) { | ||
| const metaIndex = fenceLineText.lastIndexOf(node.meta); | ||
|
|
||
| context.report({ | ||
| loc: { | ||
| start: { | ||
| line: node.position.start.line, | ||
| column: node.position.start.column + metaIndex, | ||
| }, | ||
| end: { | ||
| line: node.position.start.line, | ||
| column: | ||
| node.position.start.column + | ||
| metaIndex + | ||
| node.meta.trimEnd().length, | ||
| }, | ||
| }, | ||
| messageId: "disallowedMetadata", | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.