Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
81 changes: 81 additions & 0 deletions docs/rules/no-duplicate-definitions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 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

This rule warns when `Definition` and `FootnoteDefinition` type identifiers are defined multiple times.

Examples of **incorrect** code:

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

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

<!-- footnote definition -->

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

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:

- `ignoreDefinition: Array<string>` - when specified, definitions are ignored 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", { ignoreDefinition: ["mercury"] }]`:

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

- `ignoreFootnoteDefinition: Array<string>` - when specified, footnote definitions are ignored 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", { ignoreFootnoteDefinition: ["mercury"] }]`:

```markdown
<!-- eslint markdown/no-duplicate-definitions: ["error", { ignoreFootnoteDefinition: ["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.
145 changes: 145 additions & 0 deletions src/rules/no-duplicate-definitions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* @fileoverview Rule to prevent duplicate definitions in Markdown.
* @author 루밀LuMir(lumirlumir)
*/

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

/** @typedef {import("mdast").Definition} Definition */
/** @typedef {import("mdast").FootnoteDefinition} FootnoteDefinition */
/**
* @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: [{ ignoreDefinition: string[], ignoreFootnoteDefinition: string[]; }]; }>}
* NoDuplicateDefinitionsRuleDefinition
*/

//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------

/**
* Appends a node to a map, grouping by identifier.
* Creates a new array if the identifier doesn't exist,
* or appends to the existing array if it does.
* @param {Map<string, Array<Definition | FootnoteDefinition>>} map The map to store nodes in.
* @param {Definition | FootnoteDefinition} node The node to add to the map.
* @returns {void}
*/
function appendNodeToMap(map, node) {
map.set(
node.identifier,
map.has(node.identifier) ? [...map.get(node.identifier), node] : [node],
);
}

/**
* Finds duplicate nodes in a map if they exceed one occurrence and are not ignored.
* @param {Map<string, Array<Definition | FootnoteDefinition>>} map The map of nodes to check.
* @param {string[]} ignoreList The list of identifiers to ignore.
* @returns {Array<Definition | FootnoteDefinition>} The array of duplicate nodes.
*/
function findDuplicates(map, ignoreList) {
/** @type {Array<Definition | FootnoteDefinition>} */
const duplicates = [];

map.forEach((nodes, identifier) => {
if (nodes.length <= 1 || ignoreList.includes(identifier)) {
return;
}

duplicates.push(...nodes);
});

return duplicates;
}

//-----------------------------------------------------------------------------
// 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: {
ignoreDefinition: {
type: "array",
items: {
type: "string",
},
uniqueItems: true,
},
ignoreFootnoteDefinition: {
type: "array",
items: {
type: "string",
},
uniqueItems: true,
},
},
additionalProperties: false,
},
],

defaultOptions: [
{
ignoreDefinition: ["//"],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignoreFootnoteDefinition: [],
},
],
},

create(context) {
const [{ ignoreDefinition, ignoreFootnoteDefinition }] =
context.options;

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

return {
definition(node) {
appendNodeToMap(definitions, node);
},

footnoteDefinition(node) {
appendNodeToMap(footnoteDefinitions, node);
},

"root:exit"() {
findDuplicates(definitions, ignoreDefinition).forEach(node => {
context.report({
node,
messageId: "duplicateDefinition",
});
});

findDuplicates(
footnoteDefinitions,
ignoreFootnoteDefinition,
).forEach(node => {
context.report({
node,
messageId: "duplicateFootnoteDefinition",
});
});
},
};
},
};
Loading