Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
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 @@ -75,6 +75,7 @@ export default defineConfig([
| :- | :- | :-: |
| [`fenced-code-language`](./docs/rules/fenced-code-language.md) | Require languages for fenced code blocks | yes |
| [`heading-increment`](./docs/rules/heading-increment.md) | Enforce heading levels increment by one | yes |
| [`no-duplicate-definitions`](./docs/rules/no-duplicate-definitions.md) | Disallow duplicate definitions | yes |
| [`no-duplicate-headings`](./docs/rules/no-duplicate-headings.md) | Disallow duplicate headings in the same document | no |
| [`no-empty-images`](./docs/rules/no-empty-images.md) | Disallow empty images | yes |
| [`no-empty-links`](./docs/rules/no-empty-links.md) | Disallow empty links | yes |
Expand Down
91 changes: 91 additions & 0 deletions docs/rules/no-duplicate-definitions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# no-duplicate-definitions

Disallow duplicate definitions.

## Background

In Markdown, it's possible to define the same definition identifier multiple times. However, this is usually a mistake, as it can lead to unintended or incorrect link, image, and footnote references.

Please note that this rule does not report definition-style comments. For example:

```markdown
[//]: # (This is a comment 1)
[//]: <> (This is a comment 2)
```

## Rule Details

> [!IMPORTANT] <!-- eslint-disable-line -- This should be fixed in https://github.com/eslint/markdown/issues/294 -->
>
> The `FootnoteDefinition` node is detected only when using `language` mode [`markdown/gfm`](/README.md#languages).

This rule warns when `Definition` and `FootnoteDefinition` type identifiers are defined multiple times. Please note that this rule is **case-insensitive**, meaning `earth` and `Earth` are treated as the same identifier.

Examples of **incorrect** code:

```markdown
<!-- eslint markdown/no-duplicate-definitions: "error" -->
<!-- definition -->

[mercury]: https://example.com/mercury/
[mercury]: https://example.com/venus/

[earth]: https://example.com/earth/
[Earth]: https://example.com/mars/

<!-- footnote definition -->

[^mercury]: Hello, Mercury!
[^mercury]: Hello, Venus!

[^earth]: Hello, Earth!
[^Earth]: Hello, Mars!
```

Examples of **correct** code:

```markdown
<!-- eslint markdown/no-duplicate-definitions: "error" -->
<!-- definition -->

[mercury]: https://example.com/mercury/
[venus]: https://example.com/venus/

<!-- footnote definition -->

[^mercury]: Hello, Mercury!
[^venus]: Hello, Venus!

<!-- definition-style comment -->

[//]: # (This is a comment 1)
[//]: <> (This is a comment 2)
```

## Options

The following options are available on this rule:

- `allowDefinitions: Array<string>` - when specified, duplicate definitions are allowed if they match one of the identifiers in this array. This is useful for ignoring definitions that are intentionally duplicated. (default: `["//"]`)

Examples of **correct** code when configured as `"no-duplicate-definitions: ["error", { allowDefinitions: ["mercury"] }]`:

```markdown
<!-- eslint markdown/no-duplicate-definitions: ["error", { allowDefinitions: ["mercury"] }] -->
[mercury]: https://example.com/mercury/
[mercury]: https://example.com/venus/
```

- `allowFootnoteDefinitions: Array<string>` - when specified, duplicate footnote definitions are allowed if they match one of the identifiers in this array. This is useful for ignoring footnote definitions that are intentionally duplicated. (default: `[]`)

Examples of **correct** code when configured as `"no-duplicate-definitions: ["error", { allowFootnoteDefinitions: ["mercury"] }]`:

```markdown
<!-- eslint markdown/no-duplicate-definitions: ["error", { allowFootnoteDefinitions: ["mercury"] }] -->
[^mercury]: Hello, Mercury!
[^mercury]: Hello, Venus!
```

## When Not to Use It

If you are using a different style of definition comments, or not concerned with duplicate definitions, you can safely disable this rule.
106 changes: 106 additions & 0 deletions src/rules/no-duplicate-definitions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* @fileoverview Rule to prevent duplicate definitions in Markdown.
* @author 루밀LuMir(lumirlumir)
*/

//-----------------------------------------------------------------------------
// Type Definitions
//-----------------------------------------------------------------------------

/**
* @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: [{ allowDefinitions: string[], allowFootnoteDefinitions: string[]; }]; }>}
* NoDuplicateDefinitionsRuleDefinition
*/

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

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

docs: {
recommended: true,
description: "Disallow duplicate definitions",
url: "https://github.com/eslint/markdown/blob/main/docs/rules/no-duplicate-definitions.md",
},

messages: {
duplicateDefinition: "Unexpected duplicate definition found.",
duplicateFootnoteDefinition:
"Unexpected duplicate footnote definition found.",
},

schema: [
{
type: "object",
properties: {
allowDefinitions: {
type: "array",
items: {
type: "string",
},
uniqueItems: true,
},
allowFootnoteDefinitions: {
type: "array",
items: {
type: "string",
},
uniqueItems: true,
},
},
additionalProperties: false,
},
],

defaultOptions: [
{
allowDefinitions: ["//"],
allowFootnoteDefinitions: [],
},
],
},

create(context) {
const [{ allowDefinitions, allowFootnoteDefinitions }] =
context.options;

const definitions = new Set();
const footnoteDefinitions = new Set();

return {
definition(node) {
if (allowDefinitions.includes(node.identifier)) {
return;
}

if (definitions.has(node.identifier)) {
context.report({
node,
messageId: "duplicateDefinition",
});
} else {
definitions.add(node.identifier);
}
},

footnoteDefinition(node) {
if (allowFootnoteDefinitions.includes(node.identifier)) {
return;
}

if (footnoteDefinitions.has(node.identifier)) {
context.report({
node,
messageId: "duplicateFootnoteDefinition",
});
} else {
footnoteDefinitions.add(node.identifier);
}
},
};
},
};
Loading