Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export default defineConfig([
| [`no-missing-label-refs`](./docs/rules/no-missing-label-refs.md) | Disallow missing label references | yes |
| [`no-missing-link-fragments`](./docs/rules/no-missing-link-fragments.md) | Disallow link fragments that do not reference valid headings | yes |
| [`no-multiple-h1`](./docs/rules/no-multiple-h1.md) | Disallow multiple H1 headings in the same document | yes |
| [`no-reference-like-url`](./docs/rules/no-reference-like-url.md) | Disallow URLs that match defined reference identifiers | no |
| [`no-reversed-media-syntax`](./docs/rules/no-reversed-media-syntax.md) | Disallow reversed link and image syntax | yes |
| [`require-alt-text`](./docs/rules/require-alt-text.md) | Require alternative text for images | yes |
| [`table-column-count`](./docs/rules/table-column-count.md) | Disallow data rows in a GitHub Flavored Markdown table from having more cells than the header row | yes |
Expand Down
57 changes: 57 additions & 0 deletions docs/rules/no-reference-like-url.md
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.

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** is a planet](venus).

[venus]: https://example.com/venus.jpg
```

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)
99 changes: 99 additions & 0 deletions src/rules/no-reference-like-url.js
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
*/

//-----------------------------------------------------------------------------
// Rule Definition
//-----------------------------------------------------------------------------

/** @type {NoReferenceLikeUrlRuleDefinition} */
export default {
meta: {
type: "problem",

docs: {
description:
"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.",
},
},

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();

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}]`,
);
},
});
}
}
},

definition(node) {
definitionIdentifiers.add(node.identifier.toLowerCase());
},

link(node) {
resources.push(node);
},

image(node) {
resources.push(node);
},
};
},
};
Loading