-
Notifications
You must be signed in to change notification settings - Fork 82
feat: add no-missing-atx-heading-space rule #371
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
Merged
snitin315
merged 21 commits into
eslint:main
from
SwetaTanwar:feat/no-missing-space-atx
May 23, 2025
Merged
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5f38ba3
feat: add no-missing-space-atx rule
SwetaTanwar 8a67f19
test: added more test cases
SwetaTanwar 41b54c4
fix: removed extra code
SwetaTanwar dca6041
refactor: comments
SwetaTanwar 0a7f1ec
fix: added handling for markdown link
SwetaTanwar e9a8f7d
test: added test case for markdown lint
SwetaTanwar d52f01d
test: added test case for # in image
SwetaTanwar 5148efb
docs: update docs/rules/no-missing-space-atx.md
SwetaTanwar 74d9545
fix: added handling for double quotes
SwetaTanwar 33c8917
docs: update examples
SwetaTanwar 24d1065
refactor: renamed the rule
SwetaTanwar 618649e
chore: renamed rule
SwetaTanwar 73001bb
fix: review comments
SwetaTanwar 136e940
docs: review comments
SwetaTanwar f1f8aaf
chore: review comments
SwetaTanwar cdaa832
fix: updated regex
SwetaTanwar 7b8f7e6
fix: error positions
SwetaTanwar 4a74e5a
test: covered few more test cases
SwetaTanwar b628911
chore: review comment
SwetaTanwar ce3df04
chore: review comments
SwetaTanwar 845cee6
Merge branch 'main' of github.com:SwetaTanwar/markdown into feat/no-m…
SwetaTanwar 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
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,48 @@ | ||
| # no-missing-atx-heading-space | ||
|
|
||
| This rule warns when spaces are missing after the hash characters in an ATX style heading. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| In Markdown, headings can be created using ATX style (using hash (`#`) characters at the beginning of the line) or Setext style (using underlining with equals (`=`) or hyphens (`-`)). | ||
|
|
||
| For ATX style headings, a space should be used after the hash characters to improve readability and ensure proper rendering across various Markdown parsers. | ||
|
|
||
| This rule is automatically fixable by the `--fix` command line option. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```md | ||
| #Heading 1 | ||
| ##Heading 2 | ||
| ###Heading 3 | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```md | ||
| # Heading 1 | ||
SwetaTanwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Heading 2 | ||
| ### Heading 3 | ||
|
|
||
| <h1>#Some Heading</h1> | ||
|
|
||
| [#123](link.com) | ||
|
|
||
| ![#alttext][link.png] | ||
|
|
||
| This is a paragraph with a #hashtag, not a heading. | ||
| ``` | ||
|
|
||
| ## When Not To Use It | ||
|
|
||
| You might want to turn this rule off if you're working with a Markdown variant that doesn't require spaces after hash characters in headings. | ||
|
|
||
| ## Prior Art | ||
|
|
||
| - [MD018 - No space after hash on atx style heading](https://github.com/DavidAnson/markdownlint/blob/main/doc/md018.md) | ||
|
|
||
| ## Further Reading | ||
|
|
||
| - [Markdown Syntax: Headings](https://daringfireball.net/projects/markdown/syntax#header) | ||
| - [CommonMark Spec: ATX Headings](https://spec.commonmark.org/0.30/#atx-headings) | ||
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,96 @@ | ||
| /** | ||
| * @fileoverview Rule to ensure there is a space after hash on ATX style headings in Markdown. | ||
| * @author Sweta Tanwar (@SwetaTanwar) | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Type Definitions | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: []; }>} | ||
| * NoMissingAtxHeadingSpaceRuleDefinition | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Helpers | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| const HEADING_PATTERN = /^(#{1,6})(?:[^#\s]|$)/u; | ||
| const NEW_LINE_PATTERN = /\r?\n/u; | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Rule Definition | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| /** @type {NoMissingAtxHeadingSpaceRuleDefinition} */ | ||
| export default { | ||
| meta: { | ||
| type: "problem", | ||
|
|
||
| docs: { | ||
| recommended: true, | ||
| description: | ||
| "Disallow headings without a space after the hash characters", | ||
| url: "https://github.com/eslint/markdown/blob/main/docs/rules/no-missing-atx-heading-space.md", | ||
| }, | ||
|
|
||
| fixable: "whitespace", | ||
|
|
||
| messages: { | ||
| missingSpace: "Missing space after hash(es) on ATX style heading.", | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| return { | ||
| paragraph(node) { | ||
| if (node.children && node.children.length > 0) { | ||
| const firstTextChild = node.children.find( | ||
| child => child.type === "text", | ||
| ); | ||
lumirlumir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!firstTextChild) { | ||
| return; | ||
| } | ||
|
|
||
| const text = context.sourceCode.getText(firstTextChild); | ||
| const lines = text.split(NEW_LINE_PATTERN); | ||
|
|
||
| lines.forEach((line, idx) => { | ||
| const lineNum = | ||
| firstTextChild.position.start.line + idx; | ||
|
|
||
| const match = HEADING_PATTERN.exec(line); | ||
| if (!match) { | ||
| return; | ||
| } | ||
|
|
||
| const hashes = match[1]; | ||
|
|
||
| context.report({ | ||
| loc: { | ||
| start: { line: lineNum, column: hashes.length }, | ||
| end: { line: lineNum, column: line.length }, | ||
| }, | ||
| messageId: "missingSpace", | ||
| fix(fixer) { | ||
| const offset = | ||
| firstTextChild.position.start.offset + | ||
| lines.slice(0, idx).join("\n").length + | ||
| (idx > 0 ? 1 : 0); | ||
|
|
||
| return fixer.insertTextAfterRange( | ||
| [ | ||
| offset + hashes.length - 1, | ||
| offset + hashes.length, | ||
| ], | ||
| " ", | ||
| ); | ||
| }, | ||
| }); | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
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.