-
Notifications
You must be signed in to change notification settings - Fork 73
feat: add no-space-in-emphasis rule #403
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
Pixel998
wants to merge
5
commits into
eslint:main
Choose a base branch
from
Pixel998:no-space-in-emphasis
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.
+1,608
−102
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eca779a
feat: add no-space-in-emphasis rule
Pixel998 199af62
Merge branch 'main' into no-space-in-emphasis
Pixel998 ed30d52
Merge branch 'main' into no-space-in-emphasis
Pixel998 ad10599
refactor
Pixel998 a74c3d0
Merge branch 'main' into no-space-in-emphasis
Pixel998 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,43 @@ | ||
# no-space-in-emphasis | ||
|
||
Disallow spaces around emphasis markers. | ||
|
||
## Background | ||
|
||
In Markdown, emphasis (bold and italic) is created using asterisks (`*`) or underscores (`_`), and strikethrough is created using tildes (`~`). The emphasis markers must be directly adjacent to the text they're emphasizing, with no spaces between the markers and the text. When spaces are present, the emphasis is not rendered correctly. | ||
|
||
## Rule Details | ||
|
||
This rule warns when it finds emphasis markers that have spaces between the markers and the text they're emphasizing. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```markdown | ||
<!-- eslint markdown/no-space-in-emphasis: "error" --> | ||
|
||
Here is some ** bold ** text. | ||
Here is some * italic * text. | ||
Here is some __ bold __ text. | ||
Here is some _ italic _ text. | ||
Here is some ~~ strikethrough ~~ text. | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```markdown | ||
<!-- eslint markdown/no-space-in-emphasis: "error" --> | ||
|
||
Here is some **bold** text. | ||
Here is some *italic* text. | ||
Here is some __bold__ text. | ||
Here is some _italic_ text. | ||
Here is some ~~strikethrough~~ text. | ||
``` | ||
|
||
## When Not to Use It | ||
|
||
If you aren't concerned with proper emphasis rendering in your Markdown documents, you can safely disable this rule. | ||
|
||
## Prior Art | ||
|
||
* [MD037 - Spaces inside emphasis markers](https://github.com/DavidAnson/markdownlint/blob/main/doc/md037.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,145 @@ | ||
/** | ||
* @fileoverview Rule to prevent spaces around emphasis markers in Markdown. | ||
* @author Pixel998 | ||
*/ | ||
|
||
//----------------------------------------------------------------------------- | ||
// Imports | ||
//----------------------------------------------------------------------------- | ||
|
||
import { findOffsets } from "../util.js"; | ||
|
||
//----------------------------------------------------------------------------- | ||
// Type Definitions | ||
//----------------------------------------------------------------------------- | ||
|
||
/** | ||
* @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: []; }>} | ||
* NoSpaceInEmphasisRuleDefinition | ||
*/ | ||
Pixel998 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
//----------------------------------------------------------------------------- | ||
// Helpers | ||
//----------------------------------------------------------------------------- | ||
|
||
const markerPattern = /(?<!\\)(\*\*\*|\*\*|\*|___|__|_|~~|~)/gu; | ||
const whitespacePattern = /\s/u; | ||
|
||
/** | ||
* Finds all emphasis markers in the text | ||
* @param {string} text The text to search | ||
* @returns {Array<{marker: string, start: number, end: number}>} Array of emphasis markers | ||
*/ | ||
function findEmphasisMarkers(text) { | ||
const markers = []; | ||
let match; | ||
|
||
while ((match = markerPattern.exec(text)) !== null) { | ||
const marker = match[1]; | ||
const start = match.index; | ||
const end = start + marker.length; | ||
|
||
markers.push({ marker, start, end }); | ||
} | ||
|
||
return markers; | ||
} | ||
|
||
//----------------------------------------------------------------------------- | ||
// Rule Definition | ||
//----------------------------------------------------------------------------- | ||
|
||
/** @type {NoSpaceInEmphasisRuleDefinition} */ | ||
export default { | ||
meta: { | ||
type: "problem", | ||
|
||
docs: { | ||
recommended: true, | ||
description: "Disallow spaces around emphasis markers", | ||
url: "https://github.com/eslint/markdown/blob/main/docs/rules/no-space-in-emphasis.md", | ||
}, | ||
|
||
fixable: "whitespace", | ||
|
||
messages: { | ||
spaceInEmphasis: "Unexpected space around emphasis marker.", | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const { sourceCode } = context; | ||
|
||
return { | ||
text(node) { | ||
const text = sourceCode.getText(node); | ||
const markers = findEmphasisMarkers(text); | ||
|
||
for (let i = 0; i < markers.length - 1; i += 2) { | ||
const startMarker = markers[i]; | ||
const endMarker = markers[i + 1]; | ||
|
||
if (startMarker.marker !== endMarker.marker) { | ||
continue; | ||
} | ||
|
||
const hasStartSpace = whitespacePattern.test( | ||
text[startMarker.end], | ||
); | ||
const hasEndSpace = whitespacePattern.test( | ||
text[endMarker.start - 1], | ||
); | ||
|
||
if (hasStartSpace || hasEndSpace) { | ||
const { | ||
lineOffset: startLineOffset, | ||
columnOffset: startColumnOffset, | ||
} = findOffsets(text, startMarker.start); | ||
const { | ||
lineOffset: endLineOffset, | ||
columnOffset: endColumnOffset, | ||
} = findOffsets(text, endMarker.end); | ||
|
||
const startLine = | ||
node.position.start.line + startLineOffset; | ||
const endLine = | ||
node.position.start.line + endLineOffset; | ||
const startColumn = | ||
node.position.start.column + startColumnOffset; | ||
const endColumn = | ||
node.position.start.column + endColumnOffset; | ||
|
||
context.report({ | ||
loc: { | ||
start: { line: startLine, column: startColumn }, | ||
end: { line: endLine, column: endColumn }, | ||
}, | ||
messageId: "spaceInEmphasis", | ||
fix(fixer) { | ||
const betweenText = text.slice( | ||
startMarker.end, | ||
endMarker.start, | ||
); | ||
|
||
const fixedText = | ||
startMarker.marker + | ||
betweenText.trim() + | ||
endMarker.marker; | ||
|
||
const nodeStart = node.position.start.offset; | ||
const relativeStart = | ||
nodeStart + startMarker.start; | ||
const relativeEnd = nodeStart + endMarker.end; | ||
|
||
return fixer.replaceTextRange( | ||
[relativeStart, relativeEnd], | ||
fixedText, | ||
); | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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.