3 Commits

Author SHA1 Message Date
Delta Umhöfer
36ed1ee788 feat: add config-option for fragment-index-divider (#20) 2026-02-25 15:04:32 +01:00
f48932001a fix: update markdown-it to v14.1.1 2026-02-20 02:58:22 +01:00
c744c4872a fix: ignore external https image links 2026-02-05 21:12:22 +01:00
9 changed files with 860 additions and 446 deletions

View File

@@ -1,4 +1,10 @@
{ {
"$schema": "./node_modules/oxlint/configuration_schema.json", "$schema": "./node_modules/oxlint/configuration_schema.json",
"extends": ["node_modules/eslint-config-conventions/.oxlintrc.json"] "extends": ["node_modules/eslint-config-conventions/.oxlintrc.json"],
"env": {
"builtin": true,
"browser": true,
"node": true,
"shared-node-browser": true
}
} }

View File

@@ -52,6 +52,8 @@ awesome.md:3 relative-links Relative links should be valid ["./invalid.txt" shou
- Support links fragments similar to the [built-in `markdownlint` rule - MD051](https://github.com/DavidAnson/markdownlint/blob/main/doc/md051.md) (e.g: `[Link](./awesome.md#heading)`). - Support links fragments similar to the [built-in `markdownlint` rule - MD051](https://github.com/DavidAnson/markdownlint/blob/main/doc/md051.md) (e.g: `[Link](./awesome.md#heading)`).
- Ignore external links and absolute paths as it only checks relative links (e.g: `https://example.com/` or `/absolute/path.png`). - Ignore external links and absolute paths as it only checks relative links (e.g: `https://example.com/` or `/absolute/path.png`).
- If necessary, absolute paths can be validated too, with [`root_path` configuration option](#absolute-paths). - If necessary, absolute paths can be validated too, with [`root_path` configuration option](#absolute-paths).
- Headings defined multiple times in one file, will get enumerated as `<heading><divider><count>`.
The divier can be customized with [`fragment-count-divider`](#divider-for-fragment-index).
### Limitations ### Limitations
@@ -128,6 +130,16 @@ After this change, all absolute paths will be converted to relative paths, and w
For example, if you run markdownlint from a subdirectory (if `package.json` is located in a subdirectory), you should set `root_path` to `".."`. For example, if you run markdownlint from a subdirectory (if `package.json` is located in a subdirectory), you should set `root_path` to `".."`.
### Divider for Fragment-Index
Headers with the same name in the same file, are appended with their index when converting them to the fragment.
Between the original fragment and the index a divider will be inserted.
The final fragment is `<original-fragment><divider><index>`.
This divider can be configured with `fragment-index-divider` to accomodate different markdown-engines.
The default-value is `-`.
## Usage ## Usage
```sh ```sh

1210
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -36,18 +36,18 @@
"release": "semantic-release" "release": "semantic-release"
}, },
"dependencies": { "dependencies": {
"markdown-it": "14.1.0", "markdown-it": "14.1.1",
"mime": "4.1.0" "mime": "4.1.0"
}, },
"devDependencies": { "devDependencies": {
"@types/markdown-it": "14.1.2", "@types/markdown-it": "14.1.2",
"@types/node": "25.2.1", "@types/node": "25.3.0",
"eslint-config-conventions": "21.2.0", "eslint-config-conventions": "21.2.2",
"markdownlint": "0.40.0", "markdownlint": "0.40.0",
"markdownlint-cli2": "0.20.0", "markdownlint-cli2": "0.21.0",
"oxfmt": "0.28.0", "oxfmt": "0.34.0",
"oxlint": "1.43.0", "oxlint": "1.49.0",
"oxlint-tsgolint": "0.11.4", "oxlint-tsgolint": "0.14.1",
"semantic-release": "25.0.3", "semantic-release": "25.0.3",
"typescript": "5.9.3" "typescript": "5.9.3"
}, },

View File

@@ -70,11 +70,24 @@ const relativeLinksRule = {
url = new URL(hrefSrc, pathToFileURL(params.name)) url = new URL(hrefSrc, pathToFileURL(params.name))
} }
if (url.protocol !== "file:" && type !== "image") { const detail = `"${hrefSrc}"`
if (
type === "image" &&
url.protocol !== "file:" &&
url.protocol !== "http:" &&
url.protocol !== "https:"
) {
onError({
lineNumber,
detail: `${detail} should be an image`,
})
continue continue
} }
const detail = `"${hrefSrc}"` if (url.protocol !== "file:") {
continue
}
if (!fs.existsSync(url)) { if (!fs.existsSync(url)) {
onError({ onError({
@@ -131,12 +144,14 @@ const relativeLinksRule = {
/** @type {Map<string, number>} */ /** @type {Map<string, number>} */
const fragments = new Map() const fragments = new Map()
const fragmentCountDivider = params.config["fragment-index-divider"] ?? "-"
const fragmentsHTML = headings.map((heading) => { const fragmentsHTML = headings.map((heading) => {
const fragment = convertHeadingToHTMLFragment(heading) const fragment = convertHeadingToHTMLFragment(heading)
const count = fragments.get(fragment) ?? 0 const count = fragments.get(fragment) ?? 0
fragments.set(fragment, count + 1) fragments.set(fragment, count + 1)
if (count !== 0) { if (count !== 0) {
return `${fragment}-${count}` return `${fragment}${fragmentCountDivider}${count}`
} }
return fragment return fragment
}) })

View File

@@ -0,0 +1,13 @@
# Awesome
## Existing Heading
### Repeated Heading
Text
### Repeated Heading
Text
### Repeated Heading

View File

@@ -0,0 +1,9 @@
# Valid
[Link fragment](./awesome.md#existing-heading)
[Link fragment Repeated 0](./awesome.md#repeated-heading)
[Link fragment Repeated 1](./awesome.md#repeated-heading_1)
[Link fragment Repeated 2](./awesome.md#repeated-heading_2)

View File

@@ -0,0 +1,3 @@
# Valid
![External Image](https://example.com/image.png)

View File

@@ -152,7 +152,7 @@ test("ensure the rule validates correctly", async (t) => {
fixturePath: "test/fixtures/invalid/invalid-image.md", fixturePath: "test/fixtures/invalid/invalid-image.md",
errors: [ errors: [
'"../not-an-image.txt" should be an image', '"../not-an-image.txt" should be an image',
'"mailto:not-an-image@pictures.com" should exist in the file system', '"mailto:not-an-image@pictures.com" should be an image',
], ],
}, },
] ]
@@ -239,6 +239,20 @@ test("ensure the rule validates correctly", async (t) => {
}, },
}, },
}, },
{
name: "should be valid with multiple existing element id fragments",
fixturePath:
"test/fixtures/valid/existing-heading-fragment-divider/existing-heading-fragment.md",
config: {
"relative-links": {
"fragment-index-divider": "_",
},
},
},
{
name: "should ignore external image links",
fixturePath: "test/fixtures/valid/ignore-external-image.md",
},
] ]
for (const { name, fixturePath, config = defaultConfig } of testCases) { for (const { name, fixturePath, config = defaultConfig } of testCases) {