mirror of
https://github.com/theoludwig/markdownlint-rule-relative-links.git
synced 2024-10-05 03:16:08 +02:00
Compare commits
5 Commits
146f904866
...
f332c833ca
Author | SHA1 | Date | |
---|---|---|---|
f332c833ca | |||
e20ee54b05 | |||
5c39afbe20 | |||
cc9a1cf6a2 | |||
1095647d41 |
@ -1,4 +1,3 @@
|
||||
#!/bin/sh
|
||||
. "$(dirname "$0")/_/husky.sh"
|
||||
#!/usr/bin/env sh
|
||||
|
||||
npm run lint:commit -- --edit
|
||||
|
@ -1,5 +1,4 @@
|
||||
#!/bin/sh
|
||||
. "$(dirname "$0")/_/husky.sh"
|
||||
#!/usr/bin/env sh
|
||||
|
||||
npm run lint:staged
|
||||
npm run lint:javascript
|
||||
|
@ -1,6 +1,7 @@
|
||||
{
|
||||
"*": ["editorconfig-checker"],
|
||||
"*.{js,jsx,ts,tsx}": ["prettier --write", "eslint --fix"],
|
||||
"*.{json,jsonc,yml,yaml}": ["prettier --write"],
|
||||
"*.{md,mdx}": ["prettier --write", "markdownlint-cli2 --fix"]
|
||||
"**/*": ["editorconfig-checker", "prettier --write --ignore-unknown"],
|
||||
"**/*.md": ["markdownlint-cli2 --fix --no-globs"],
|
||||
"**/*.{js,jsx,ts,tsx}": [
|
||||
"eslint --fix --max-warnings 0 --report-unused-disable-directives"
|
||||
]
|
||||
}
|
||||
|
@ -3,9 +3,9 @@
|
||||
"extends": "markdownlint/style/prettier",
|
||||
"default": true,
|
||||
"relative-links": true,
|
||||
"no-inline-html": false
|
||||
"no-inline-html": false,
|
||||
},
|
||||
"globs": ["**/*.{md,mdx}"],
|
||||
"globs": ["**/*.md"],
|
||||
"ignores": ["**/node_modules", "**/test/fixtures/**"],
|
||||
"customRules": ["./src/index.js"]
|
||||
"customRules": ["./src/index.js"],
|
||||
}
|
||||
|
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
||||
# MIT License
|
||||
|
||||
Copyright (c) Théo LUDWIG
|
||||
Copyright (c) Théo LUDWIG <contact@theoludwig.fr>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -66,7 +66,7 @@ Contributions are welcome to improve the rule, and to alleviate these limitation
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- [Node.js](https://nodejs.org/) >= 16.0.0
|
||||
[Node.js](https://nodejs.org/) >= 16.0.0
|
||||
|
||||
## Installation
|
||||
|
||||
@ -88,7 +88,7 @@ We recommend configuring [markdownlint-cli2](https://github.com/DavidAnson/markd
|
||||
"default": true,
|
||||
"relative-links": true
|
||||
},
|
||||
"globs": ["**/*.{md,mdx}"],
|
||||
"globs": ["**/*.md"],
|
||||
"ignores": ["**/node_modules"],
|
||||
"customRules": ["markdownlint-rule-relative-links"]
|
||||
}
|
||||
|
@ -20,6 +20,6 @@
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
}
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
},
|
||||
}
|
||||
|
759
package-lock.json
generated
759
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
18
package.json
18
package.json
@ -37,9 +37,9 @@
|
||||
"lint:prettier": "prettier . --check --ignore-path .gitignore",
|
||||
"lint:javascript": "tsc --project jsconfig.json --noEmit",
|
||||
"lint:staged": "lint-staged",
|
||||
"test": "node --test --experimental-test-coverage",
|
||||
"test": "node --test",
|
||||
"release": "semantic-release",
|
||||
"postinstall": "husky install",
|
||||
"postinstall": "husky",
|
||||
"prepublishOnly": "pinst --disable",
|
||||
"postpublish": "pinst --enable"
|
||||
},
|
||||
@ -47,10 +47,10 @@
|
||||
"markdown-it": "14.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "18.4.4",
|
||||
"@commitlint/config-conventional": "18.4.4",
|
||||
"@commitlint/cli": "18.6.0",
|
||||
"@commitlint/config-conventional": "18.6.0",
|
||||
"@types/markdown-it": "13.0.7",
|
||||
"@types/node": "20.11.0",
|
||||
"@types/node": "20.11.10",
|
||||
"editorconfig-checker": "5.1.2",
|
||||
"eslint": "8.56.0",
|
||||
"eslint-config-conventions": "13.1.0",
|
||||
@ -59,13 +59,13 @@
|
||||
"eslint-plugin-prettier": "5.1.3",
|
||||
"eslint-plugin-promise": "6.1.1",
|
||||
"eslint-plugin-unicorn": "50.0.1",
|
||||
"husky": "8.0.3",
|
||||
"husky": "9.0.7",
|
||||
"lint-staged": "15.2.0",
|
||||
"markdownlint": "0.33.0",
|
||||
"markdownlint-cli2": "0.11.0",
|
||||
"markdownlint-cli2": "0.12.1",
|
||||
"pinst": "3.0.0",
|
||||
"prettier": "3.1.1",
|
||||
"semantic-release": "22.0.12",
|
||||
"prettier": "3.2.4",
|
||||
"semantic-release": "23.0.0",
|
||||
"typescript": "5.3.3"
|
||||
}
|
||||
}
|
||||
|
41
src/index.js
41
src/index.js
@ -8,6 +8,9 @@ const {
|
||||
convertHeadingToHTMLFragment,
|
||||
getMarkdownHeadings,
|
||||
getMarkdownIdOrAnchorNameFragments,
|
||||
isValidIntegerString,
|
||||
getNumberOfLines,
|
||||
getLineNumberStringFromFragment,
|
||||
} = require("./utils.js")
|
||||
|
||||
/** @typedef {import('markdownlint').Rule} MarkdownLintRule */
|
||||
@ -72,7 +75,7 @@ const customRule = {
|
||||
|
||||
if (url.hash.length <= 0) {
|
||||
if (hrefSrc.includes("#")) {
|
||||
if (type !== "link_open") {
|
||||
if (type === "image") {
|
||||
onError({
|
||||
lineNumber,
|
||||
detail: `${detail} should not have a fragment identifier as it is an image`,
|
||||
@ -89,7 +92,7 @@ const customRule = {
|
||||
continue
|
||||
}
|
||||
|
||||
if (type !== "link_open") {
|
||||
if (type === "image") {
|
||||
onError({
|
||||
lineNumber,
|
||||
detail: `${detail} should not have a fragment identifier as it is an image`,
|
||||
@ -97,6 +100,10 @@ const customRule = {
|
||||
continue
|
||||
}
|
||||
|
||||
if (!url.pathname.endsWith(".md")) {
|
||||
continue
|
||||
}
|
||||
|
||||
const fileContent = fs.readFileSync(url, { encoding: "utf8" })
|
||||
const headings = getMarkdownHeadings(fileContent)
|
||||
const idOrAnchorNameHTMLFragments =
|
||||
@ -117,7 +124,35 @@ const customRule = {
|
||||
|
||||
fragmentsHTML.push(...idOrAnchorNameHTMLFragments)
|
||||
|
||||
if (!fragmentsHTML.includes(url.hash)) {
|
||||
if (!fragmentsHTML.includes(url.hash.toLowerCase())) {
|
||||
if (url.hash.startsWith("#L")) {
|
||||
const lineNumberFragmentString = getLineNumberStringFromFragment(
|
||||
url.hash,
|
||||
)
|
||||
|
||||
const hasOnlyDigits = isValidIntegerString(lineNumberFragmentString)
|
||||
if (!hasOnlyDigits) {
|
||||
onError({
|
||||
lineNumber,
|
||||
detail: `${detail} should have a valid fragment identifier`,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
const lineNumberFragment = Number.parseInt(
|
||||
lineNumberFragmentString,
|
||||
10,
|
||||
)
|
||||
const numberOfLines = getNumberOfLines(fileContent)
|
||||
if (lineNumberFragment > numberOfLines) {
|
||||
onError({
|
||||
lineNumber,
|
||||
detail: `${detail} should have a valid fragment identifier, ${detail} should have at least ${lineNumberFragment} lines to be valid`,
|
||||
})
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
onError({
|
||||
lineNumber,
|
||||
detail: `${detail} should have a valid fragment identifier`,
|
||||
|
43
src/utils.js
43
src/utils.js
@ -2,6 +2,8 @@ const MarkdownIt = require("markdown-it")
|
||||
|
||||
const { getHtmlAttributeRe } = require("./markdownlint-rule-helpers/helpers.js")
|
||||
|
||||
const markdownIt = new MarkdownIt({ html: true })
|
||||
|
||||
/**
|
||||
* Converts a Markdown heading into an HTML fragment according to the rules
|
||||
* used by GitHub.
|
||||
@ -37,7 +39,6 @@ const ignoredTokens = new Set(["heading_open", "heading_close"])
|
||||
* @returns {string[]}
|
||||
*/
|
||||
const getMarkdownHeadings = (content) => {
|
||||
const markdownIt = new MarkdownIt({ html: true })
|
||||
const tokens = markdownIt.parse(content, {})
|
||||
|
||||
/** @type {string[]} */
|
||||
@ -86,7 +87,6 @@ const idHTMLAttributeRegex = getHtmlAttributeRe("id")
|
||||
* @returns {string[]}
|
||||
*/
|
||||
const getMarkdownIdOrAnchorNameFragments = (content) => {
|
||||
const markdownIt = new MarkdownIt({ html: true })
|
||||
const tokens = markdownIt.parse(content, {})
|
||||
|
||||
/** @type {string[]} */
|
||||
@ -114,8 +114,47 @@ const getMarkdownIdOrAnchorNameFragments = (content) => {
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a string is a valid integer.
|
||||
*
|
||||
* Using `Number.parseInt` combined with `Number.isNaN` will not be sufficient enough because `Number.parseInt("1abc", 10)` will return `1` (a valid number) instead of `NaN`.
|
||||
*
|
||||
* @param {string} value
|
||||
* @returns {boolean}
|
||||
* @example isValidIntegerString("1") // true
|
||||
* @example isValidIntegerString("45") // true
|
||||
* @example isValidIntegerString("1abc") // false
|
||||
* @example isValidIntegerString("1.0") // false
|
||||
*/
|
||||
const isValidIntegerString = (value) => {
|
||||
const regex = /^\d+$/
|
||||
return regex.test(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of lines in a string, based on the number of `\n` characters.
|
||||
* @param {string} content
|
||||
* @returns {number}
|
||||
*/
|
||||
const getNumberOfLines = (content) => {
|
||||
return content.split("\n").length
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the line number string from a fragment.
|
||||
* @param {string} fragment
|
||||
* @returns {string}
|
||||
* @example getLineNumberStringFromFragment("#L50") // 50
|
||||
*/
|
||||
const getLineNumberStringFromFragment = (fragment) => {
|
||||
return fragment.slice(2)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
convertHeadingToHTMLFragment,
|
||||
getMarkdownHeadings,
|
||||
getMarkdownIdOrAnchorNameFragments,
|
||||
isValidIntegerString,
|
||||
getNumberOfLines,
|
||||
getLineNumberStringFromFragment,
|
||||
}
|
||||
|
1
test/fixtures/invalid/invalid-heading-with-L-fragment/awesome.md
vendored
Normal file
1
test/fixtures/invalid/invalid-heading-with-L-fragment/awesome.md
vendored
Normal file
@ -0,0 +1 @@
|
||||
# Awesome
|
3
test/fixtures/invalid/invalid-heading-with-L-fragment/invalid-heading-with-L-fragment.md
vendored
Normal file
3
test/fixtures/invalid/invalid-heading-with-L-fragment/invalid-heading-with-L-fragment.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Invalid
|
||||
|
||||
[Link fragment line number 7](./awesome.md#L7abc)
|
1
test/fixtures/invalid/invalid-line-number-fragment/awesome.md
vendored
Normal file
1
test/fixtures/invalid/invalid-line-number-fragment/awesome.md
vendored
Normal file
@ -0,0 +1 @@
|
||||
# Awesome
|
3
test/fixtures/invalid/invalid-line-number-fragment/invalid-line-number-fragment.md
vendored
Normal file
3
test/fixtures/invalid/invalid-line-number-fragment/invalid-line-number-fragment.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Invalid
|
||||
|
||||
[Link fragment line number 7](./awesome.md#L7)
|
3
test/fixtures/valid/existing-heading-case-insensitive/awesome.md
vendored
Normal file
3
test/fixtures/valid/existing-heading-case-insensitive/awesome.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Awesome
|
||||
|
||||
## Existing Heading
|
3
test/fixtures/valid/existing-heading-case-insensitive/existing-heading-case-insensitive.md
vendored
Normal file
3
test/fixtures/valid/existing-heading-case-insensitive/existing-heading-case-insensitive.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Valid
|
||||
|
||||
[Link fragment](./awesome.md#ExistIng-Heading)
|
@ -1,4 +1,4 @@
|
||||
# Valid
|
||||
# Awesome
|
||||
|
||||
## Existing Heading
|
||||
|
||||
|
0
test/fixtures/valid/only-parse-markdown-files-for-fragments/abc.txt
vendored
Normal file
0
test/fixtures/valid/only-parse-markdown-files-for-fragments/abc.txt
vendored
Normal file
9
test/fixtures/valid/only-parse-markdown-files-for-fragments/awesome.html
vendored
Normal file
9
test/fixtures/valid/only-parse-markdown-files-for-fragments/awesome.html
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Awesome</title>
|
||||
</head>
|
||||
<body></body>
|
||||
</html>
|
@ -0,0 +1,7 @@
|
||||
# Valid
|
||||
|
||||
[Link fragment HTML](./awesome.html#existing-heading)
|
||||
|
||||
[Link fragment TXT](./abc.txt#existing-heading)
|
||||
|
||||
[Link fragment Image](../../image.png#existing-heading)
|
3
test/fixtures/valid/valid-heading-like-number-fragment/awesome.md
vendored
Normal file
3
test/fixtures/valid/valid-heading-like-number-fragment/awesome.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Awesome
|
||||
|
||||
## L7
|
3
test/fixtures/valid/valid-heading-like-number-fragment/valid-heading-like-number-fragment.md
vendored
Normal file
3
test/fixtures/valid/valid-heading-like-number-fragment/valid-heading-like-number-fragment.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Valid
|
||||
|
||||
[Link fragment](./awesome.md#L7)
|
9
test/fixtures/valid/valid-line-number-fragment/awesome.md
vendored
Normal file
9
test/fixtures/valid/valid-line-number-fragment/awesome.md
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
# Awesome
|
||||
|
||||
ABC
|
||||
|
||||
Line 5
|
||||
|
||||
Line 7 Text
|
||||
|
||||
## L7
|
3
test/fixtures/valid/valid-line-number-fragment/valid-line-number-fragment.md
vendored
Normal file
3
test/fixtures/valid/valid-line-number-fragment/valid-line-number-fragment.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Valid
|
||||
|
||||
[Link fragment line number 7](./awesome.md#L7)
|
@ -45,6 +45,19 @@ test("ensure the rule validates correctly", async (t) => {
|
||||
error:
|
||||
'"./awesome.md#not-an-id-should-be-ignored" should have a valid fragment identifier',
|
||||
},
|
||||
{
|
||||
name: "with invalid heading with #L fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/invalid-heading-with-L-fragment/invalid-heading-with-L-fragment.md",
|
||||
error: '"./awesome.md#L7abc" should have a valid fragment identifier',
|
||||
},
|
||||
{
|
||||
name: "with a invalid line number fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/invalid-line-number-fragment/invalid-line-number-fragment.md",
|
||||
error:
|
||||
'"./awesome.md#L7" should have a valid fragment identifier, "./awesome.md#L7" should have at least 7 lines to be valid',
|
||||
},
|
||||
{
|
||||
name: "with a non-existing anchor name fragment",
|
||||
fixturePath:
|
||||
@ -118,11 +131,31 @@ test("ensure the rule validates correctly", async (t) => {
|
||||
fixturePath:
|
||||
"test/fixtures/valid/existing-element-id-fragment/existing-element-id-fragment.md",
|
||||
},
|
||||
{
|
||||
name: "with an existing heading fragment (case insensitive)",
|
||||
fixturePath:
|
||||
"test/fixtures/valid/existing-heading-case-insensitive/existing-heading-case-insensitive.md",
|
||||
},
|
||||
{
|
||||
name: "with an existing heading fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md",
|
||||
},
|
||||
{
|
||||
name: "should only parse markdown files for fragments checking",
|
||||
fixturePath:
|
||||
"test/fixtures/valid/only-parse-markdown-files-for-fragments/only-parse-markdown-files-for-fragments.md",
|
||||
},
|
||||
{
|
||||
name: 'with valid heading "like" line number fragment',
|
||||
fixturePath:
|
||||
"test/fixtures/valid/valid-heading-like-number-fragment/valid-heading-like-number-fragment.md",
|
||||
},
|
||||
{
|
||||
name: "with valid line number fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/valid/valid-line-number-fragment/valid-line-number-fragment.md",
|
||||
},
|
||||
{
|
||||
name: "with an existing file",
|
||||
fixturePath: "test/fixtures/valid/existing-file.md",
|
||||
|
@ -5,6 +5,9 @@ const {
|
||||
convertHeadingToHTMLFragment,
|
||||
getMarkdownHeadings,
|
||||
getMarkdownIdOrAnchorNameFragments,
|
||||
isValidIntegerString,
|
||||
getNumberOfLines,
|
||||
getLineNumberStringFromFragment,
|
||||
} = require("../src/utils.js")
|
||||
|
||||
test("utils", async (t) => {
|
||||
@ -54,4 +57,23 @@ test("utils", async (t) => {
|
||||
assert.deepStrictEqual(getMarkdownIdOrAnchorNameFragments("<a>"), [])
|
||||
assert.deepStrictEqual(getMarkdownIdOrAnchorNameFragments("<a id=>"), [])
|
||||
})
|
||||
|
||||
await t.test("isValidIntegerString", async () => {
|
||||
assert.strictEqual(isValidIntegerString("1"), true)
|
||||
assert.strictEqual(isValidIntegerString("45"), true)
|
||||
assert.strictEqual(isValidIntegerString("1abc"), false)
|
||||
assert.strictEqual(isValidIntegerString("1.0"), false)
|
||||
})
|
||||
|
||||
await t.test("getNumberOfLines", async () => {
|
||||
assert.strictEqual(getNumberOfLines(""), 1)
|
||||
assert.strictEqual(getNumberOfLines("Hello"), 1)
|
||||
assert.strictEqual(getNumberOfLines("Hello\nWorld"), 2)
|
||||
assert.strictEqual(getNumberOfLines("Hello\nWorld\n"), 3)
|
||||
assert.strictEqual(getNumberOfLines("Hello\nWorld\n\n"), 4)
|
||||
})
|
||||
|
||||
await t.test("getLineNumberStringFromFragment", async () => {
|
||||
assert.strictEqual(getLineNumberStringFromFragment("#L50"), "50")
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user