-
Notifications
You must be signed in to change notification settings - Fork 76
feat: add no-reference-like-urls rule #433
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
12
commits into
eslint:main
Choose a base branch
from
TKDev7:no-reference-like-url
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 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8cfbe1f
feat: add no-reference-like-url rule
TKDev7 4e2ea0a
address review feedback
TKDev7 e35922c
Merge branch 'main' into no-reference-like-url
TKDev7 a5272a5
refactor types
TKDev7 355f238
normalize urls and add more tests
TKDev7 b9a408f
fix CI
TKDev7 5ac7d7d
use regex-based parsing
TKDev7 243e831
refactor and add more tests
TKDev7 ae3713c
optimize with targeted node processing
TKDev7 06d9597
rename rule
TKDev7 22ff7fc
refactor tests
TKDev7 5aef967
simplify with ESQuery selector
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
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,57 @@ | ||
# no-reference-like-url | ||
|
||
Disallow URLs that match defined reference identifiers. | ||
|
||
## Background | ||
|
||
In Markdown, you can create links using either inline syntax `[text](url)` or reference syntax `[text][id]` with a separate definition `[id]: url`. This rule encourages reference syntax when a link's URL matches an existing reference identifier. | ||
TKDev7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
For example, if you have a definition like `[mercury]: https://example.com/mercury/`, then using `[text](mercury)` should be written as `[text][mercury]` instead. | ||
|
||
## Rule Details | ||
|
||
This rule flags URLs that match defined reference identifiers. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```markdown | ||
<!-- eslint markdown/no-reference-like-url: "error" --> | ||
|
||
[**Mercury**](mercury) is the first planet from the sun. | ||
|
||
[mercury]: https://example.com/mercury/ | ||
``` | ||
|
||
```markdown | ||
<!-- eslint markdown/no-reference-like-url: "error" --> | ||
|
||
. | ||
|
||
[venus]: https://example.com/venus.jpg | ||
``` | ||
TKDev7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Examples of **correct** code for this rule: | ||
|
||
```markdown | ||
<!-- eslint markdown/no-reference-like-url: "error" --> | ||
|
||
[**Mercury**][mercury] is the first planet from the sun. | ||
|
||
[mercury]: https://example.com/mercury/ | ||
``` | ||
|
||
```markdown | ||
<!-- eslint markdown/no-reference-like-url: "error" --> | ||
|
||
![**Venus** is a planet][venus]. | ||
|
||
[venus]: https://example.com/venus.jpg | ||
``` | ||
|
||
## When Not to Use It | ||
|
||
If you prefer inline link syntax even when reference definitions are available, or if you're working in an environment where reference syntax is not preferred, you can safely disable this rule. | ||
|
||
## Prior Art | ||
|
||
* [remark-lint-no-reference-like-url](https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-no-reference-like-url) |
TKDev7 marked this conversation as resolved.
Show resolved
Hide resolved
|
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,99 @@ | ||
/** | ||
* @fileoverview Rule to enforce reference-style links when URL matches a defined identifier. | ||
* @author TKDev7 | ||
*/ | ||
|
||
//----------------------------------------------------------------------------- | ||
// Type Definitions | ||
//----------------------------------------------------------------------------- | ||
|
||
/** @typedef {import("mdast").Node} Node */ | ||
/** @typedef {import("mdast").Link} LinkNode */ | ||
/** @typedef {import("mdast").Image} ImageNode */ | ||
/** @typedef {import("mdast").Definition} DefinitionNode */ | ||
/** | ||
* @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: []; }>} | ||
* NoReferenceLikeUrlRuleDefinition | ||
*/ | ||
TKDev7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
//----------------------------------------------------------------------------- | ||
// Rule Definition | ||
//----------------------------------------------------------------------------- | ||
|
||
/** @type {NoReferenceLikeUrlRuleDefinition} */ | ||
export default { | ||
meta: { | ||
type: "problem", | ||
|
||
docs: { | ||
description: | ||
lumirlumir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Disallow URLs that match defined reference identifiers", | ||
url: "https://github.com/eslint/markdown/blob/main/docs/rules/no-reference-like-url.md", | ||
}, | ||
|
||
fixable: "code", | ||
|
||
messages: { | ||
referenceLikeUrl: | ||
"Unexpected resource {{type}} with URL that matches a definition identifier. Use [text][id] syntax instead.", | ||
TKDev7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
|
||
create(context) { | ||
const { sourceCode } = context; | ||
/** @type {Set<string>} */ | ||
const definitionIdentifiers = new Set(); | ||
/** @type {Array<LinkNode|ImageNode>} */ | ||
const resources = []; | ||
|
||
return { | ||
"root:exit"() { | ||
for (const node of resources) { | ||
const url = node.url.toLowerCase(); | ||
TKDev7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (definitionIdentifiers.has(url)) { | ||
context.report({ | ||
node, | ||
messageId: "referenceLikeUrl", | ||
data: { | ||
type: node.type, | ||
}, | ||
fix(fixer) { | ||
const text = sourceCode.getText(node); | ||
|
||
const bracketParenIndex = text.indexOf("]("); | ||
const closeParenIndex = text.lastIndexOf(")"); | ||
|
||
const startOffset = | ||
node.position.start.offset + | ||
bracketParenIndex + | ||
1; | ||
const endOffset = | ||
node.position.start.offset + | ||
closeParenIndex + | ||
1; | ||
|
||
return fixer.replaceTextRange( | ||
[startOffset, endOffset], | ||
`[${node.url}]`, | ||
); | ||
TKDev7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
|
||
definition(node) { | ||
definitionIdentifiers.add(node.identifier.toLowerCase()); | ||
TKDev7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
|
||
link(node) { | ||
resources.push(node); | ||
}, | ||
|
||
image(node) { | ||
resources.push(node); | ||
}, | ||
}; | ||
}, | ||
}; |
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.