mirror of
https://github.com/theoludwig/markdownlint-rule-relative-links.git
synced 2024-10-05 03:16:08 +02:00
Compare commits
3 Commits
bf9403ad84
...
85f465306f
Author | SHA1 | Date | |
---|---|---|---|
85f465306f | |||
450cdb84f0 | |||
2df95e97d8 |
2592
package-lock.json
generated
2592
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
@ -47,25 +47,25 @@
|
||||
"markdown-it": "14.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "19.1.0",
|
||||
"@commitlint/config-conventional": "19.1.0",
|
||||
"@types/markdown-it": "14.0.0",
|
||||
"@types/node": "20.12.5",
|
||||
"@commitlint/cli": "19.2.2",
|
||||
"@commitlint/config-conventional": "19.2.2",
|
||||
"@types/markdown-it": "14.1.1",
|
||||
"@types/node": "20.12.12",
|
||||
"editorconfig-checker": "5.1.5",
|
||||
"eslint": "8.56.0",
|
||||
"eslint-config-conventions": "14.1.0",
|
||||
"eslint": "8.57.0",
|
||||
"eslint-config-conventions": "14.2.0",
|
||||
"eslint-config-prettier": "9.1.0",
|
||||
"eslint-plugin-import": "2.29.1",
|
||||
"eslint-plugin-prettier": "5.1.3",
|
||||
"eslint-plugin-promise": "6.1.1",
|
||||
"eslint-plugin-unicorn": "51.0.1",
|
||||
"eslint-plugin-promise": "6.2.0",
|
||||
"eslint-plugin-unicorn": "53.0.0",
|
||||
"husky": "9.0.11",
|
||||
"lint-staged": "15.2.2",
|
||||
"lint-staged": "15.2.5",
|
||||
"markdownlint": "0.34.0",
|
||||
"markdownlint-cli2": "0.13.0",
|
||||
"pinst": "3.0.0",
|
||||
"prettier": "3.2.5",
|
||||
"semantic-release": "23.0.7",
|
||||
"typescript": "5.4.4"
|
||||
"semantic-release": "23.1.1",
|
||||
"typescript": "5.4.5"
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ const {
|
||||
isValidIntegerString,
|
||||
getNumberOfLines,
|
||||
getLineNumberStringFromFragment,
|
||||
lineFragmentRe,
|
||||
} = require("./utils.js")
|
||||
|
||||
/** @typedef {import('markdownlint').Rule} MarkdownLintRule */
|
||||
@ -125,7 +126,7 @@ const customRule = {
|
||||
|
||||
fragmentsHTML.push(...idOrAnchorNameHTMLFragments)
|
||||
|
||||
if (!fragmentsHTML.includes(url.hash.toLowerCase())) {
|
||||
if (!fragmentsHTML.includes(url.hash)) {
|
||||
if (url.hash.startsWith("#L")) {
|
||||
const lineNumberFragmentString = getLineNumberStringFromFragment(
|
||||
url.hash,
|
||||
@ -133,6 +134,10 @@ const customRule = {
|
||||
|
||||
const hasOnlyDigits = isValidIntegerString(lineNumberFragmentString)
|
||||
if (!hasOnlyDigits) {
|
||||
if (lineFragmentRe.test(url.hash)) {
|
||||
continue
|
||||
}
|
||||
|
||||
onError({
|
||||
lineNumber,
|
||||
detail: `${detail} should have a valid fragment identifier`,
|
||||
@ -152,6 +157,8 @@ const customRule = {
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
onError({
|
||||
|
@ -4,6 +4,8 @@ const { getHtmlAttributeRe } = require("./markdownlint-rule-helpers/helpers.js")
|
||||
|
||||
const markdownIt = new MarkdownIt({ html: true })
|
||||
|
||||
const lineFragmentRe = /^#(?:L\d+(?:C\d+)?-L\d+(?:C\d+)?|L\d+)$/
|
||||
|
||||
/**
|
||||
* Converts a Markdown heading into an HTML fragment according to the rules
|
||||
* used by GitHub.
|
||||
@ -26,7 +28,7 @@ const convertHeadingToHTMLFragment = (inlineText) => {
|
||||
"",
|
||||
)
|
||||
.replace(/ /gu, "-"),
|
||||
).toLowerCase()
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@ -151,6 +153,7 @@ const getLineNumberStringFromFragment = (fragment) => {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
lineFragmentRe,
|
||||
convertHeadingToHTMLFragment,
|
||||
getMarkdownHeadings,
|
||||
getMarkdownIdOrAnchorNameFragments,
|
||||
|
1
test/fixtures/invalid/invalid-line-column-range-number-fragment/awesome.md
vendored
Normal file
1
test/fixtures/invalid/invalid-line-column-range-number-fragment/awesome.md
vendored
Normal file
@ -0,0 +1 @@
|
||||
# Awesome
|
@ -0,0 +1,31 @@
|
||||
# Invalid
|
||||
|
||||
[Invalid](./awesome.md#L12-not-a-line-link)
|
||||
|
||||
[Invalid](./awesome.md#l7)
|
||||
|
||||
[Invalid](./awesome.md#L)
|
||||
|
||||
[Invalid](./awesome.md#L7extra)
|
||||
|
||||
[Invalid](./awesome.md#L30C)
|
||||
|
||||
[Invalid](./awesome.md#L30Cextra)
|
||||
|
||||
[Invalid](./awesome.md#L30L12)
|
||||
|
||||
[Invalid](./awesome.md#L30C12)
|
||||
|
||||
[Invalid](./awesome.md#L30C11-)
|
||||
|
||||
[Invalid](./awesome.md#L30C11-L)
|
||||
|
||||
[Invalid](./awesome.md#L30C11-L31C)
|
||||
|
||||
[Invalid](./awesome.md#L30C11-C31)
|
||||
|
||||
[Invalid](./awesome.md#C30)
|
||||
|
||||
[Invalid](./awesome.md#C11-C31)
|
||||
|
||||
[Invalid](./awesome.md#C11-L4C31)
|
@ -1,3 +1,3 @@
|
||||
# Valid
|
||||
|
||||
[Link fragment](./awesome.md#L7)
|
||||
[Link fragment](./awesome.md#l7)
|
||||
|
3
test/fixtures/valid/valid-line-column-range-number-fragment/awesome.md
vendored
Normal file
3
test/fixtures/valid/valid-line-column-range-number-fragment/awesome.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Awesome
|
||||
|
||||
## L12 Not A Line Link
|
@ -0,0 +1,11 @@
|
||||
# Valid
|
||||
|
||||
[Valid](./awesome.md#l12-not-a-line-link)
|
||||
|
||||
[Valid](./awesome.md#L30-L31)
|
||||
|
||||
[Valid](./awesome.md#L3C24-L88)
|
||||
|
||||
[Valid](./awesome.md#L304-L314C98)
|
||||
|
||||
[Valid](./awesome.md#L200C4-L3244C2)
|
@ -29,92 +29,138 @@ test("ensure the rule validates correctly", async (t) => {
|
||||
name: "should be invalid with an empty id fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/empty-id-fragment/empty-id-fragment.md",
|
||||
error: '"./awesome.md#" should have a valid fragment identifier',
|
||||
errors: ['"./awesome.md#" should have a valid fragment identifier'],
|
||||
},
|
||||
{
|
||||
name: "should be invalid with a name fragment other than for an anchor",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/ignore-name-fragment-if-not-an-anchor/ignore-name-fragment-if-not-an-anchor.md",
|
||||
error:
|
||||
errors: [
|
||||
'"./awesome.md#name-should-be-ignored" should have a valid fragment identifier',
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "should be invalid with a non-existing id fragment (data-id !== id)",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/ignore-not-an-id-fragment/ignore-not-an-id-fragment.md",
|
||||
error:
|
||||
errors: [
|
||||
'"./awesome.md#not-an-id-should-be-ignored" should have a valid fragment identifier',
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "should be invalid with uppercase letters in fragment (case sensitive)",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/invalid-heading-case-sensitive/invalid-heading-case-sensitive.md",
|
||||
errors: [
|
||||
'"./awesome.md#ExistIng-Heading" should have a valid fragment identifier',
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "should be invalid 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',
|
||||
errors: [
|
||||
'"./awesome.md#L7abc" should have a valid fragment identifier',
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "should be invalid with a invalid line column range number fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/invalid-line-column-range-number-fragment/invalid-line-column-range-number-fragment.md",
|
||||
errors: [
|
||||
'"./awesome.md#L12-not-a-line-link" should have a valid fragment identifier',
|
||||
'"./awesome.md#l7" should have a valid fragment identifier',
|
||||
'"./awesome.md#L" should have a valid fragment identifier',
|
||||
'"./awesome.md#L7extra" should have a valid fragment identifier',
|
||||
'"./awesome.md#L30C" should have a valid fragment identifier',
|
||||
'"./awesome.md#L30Cextra" should have a valid fragment identifier',
|
||||
'"./awesome.md#L30L12" should have a valid fragment identifier',
|
||||
'"./awesome.md#L30C12" should have a valid fragment identifier',
|
||||
'"./awesome.md#L30C11-" should have a valid fragment identifier',
|
||||
'"./awesome.md#L30C11-L" should have a valid fragment identifier',
|
||||
'"./awesome.md#L30C11-L31C" should have a valid fragment identifier',
|
||||
'"./awesome.md#L30C11-C31" should have a valid fragment identifier',
|
||||
'"./awesome.md#C30" should have a valid fragment identifier',
|
||||
'"./awesome.md#C11-C31" should have a valid fragment identifier',
|
||||
'"./awesome.md#C11-L4C31" should have a valid fragment identifier',
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "should be invalid with a invalid line number fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/invalid-line-number-fragment/invalid-line-number-fragment.md",
|
||||
error:
|
||||
errors: [
|
||||
'"./awesome.md#L7" should have a valid fragment identifier, "./awesome.md#L7" should have at least 7 lines to be valid',
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "should be invalid with a non-existing anchor name fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/non-existing-anchor-name-fragment/non-existing-anchor-name-fragment.md",
|
||||
error:
|
||||
errors: [
|
||||
'"./awesome.md#non-existing-anchor-name-fragment" should have a valid fragment identifier',
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "should be invalid with a non-existing element id fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/non-existing-element-id-fragment/non-existing-element-id-fragment.md",
|
||||
error:
|
||||
errors: [
|
||||
'"./awesome.md#non-existing-element-id-fragment" should have a valid fragment identifier',
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "should be invalid with a non-existing heading fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md",
|
||||
error:
|
||||
errors: [
|
||||
'"./awesome.md#non-existing-heading" should have a valid fragment identifier',
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "should be invalid with a link to an image with a empty fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/ignore-empty-fragment-checking-for-image.md",
|
||||
error:
|
||||
errors: [
|
||||
'"../image.png#" should not have a fragment identifier as it is an image',
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "should be invalid with a link to an image with a fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/ignore-fragment-checking-for-image.md",
|
||||
error:
|
||||
errors: [
|
||||
'"../image.png#non-existing-fragment" should not have a fragment identifier as it is an image',
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "should be invalid with a non-existing file",
|
||||
fixturePath: "test/fixtures/invalid/non-existing-file.md",
|
||||
error: '"./index.test.js" should exist in the file system',
|
||||
errors: ['"./index.test.js" should exist in the file system'],
|
||||
},
|
||||
{
|
||||
name: "should be invalid with a non-existing image",
|
||||
fixturePath: "test/fixtures/invalid/non-existing-image.md",
|
||||
error: '"./image.png" should exist in the file system',
|
||||
errors: ['"./image.png" should exist in the file system'],
|
||||
},
|
||||
]
|
||||
|
||||
for (const { name, fixturePath, error } of testCases) {
|
||||
for (const { name, fixturePath, errors } of testCases) {
|
||||
await t.test(name, async () => {
|
||||
const lintResults = await validateMarkdownLint(fixturePath)
|
||||
assert.equal(lintResults?.length, 1)
|
||||
assert.deepEqual(lintResults?.[0]?.ruleNames, relativeLinksRule.names)
|
||||
assert.equal(
|
||||
lintResults?.[0]?.ruleDescription,
|
||||
const lintResults = (await validateMarkdownLint(fixturePath)) ?? []
|
||||
const errorsDetails = lintResults.map((result) => {
|
||||
assert.deepEqual(result.ruleNames, relativeLinksRule.names)
|
||||
assert.deepEqual(
|
||||
result.ruleDescription,
|
||||
relativeLinksRule.description,
|
||||
)
|
||||
assert.equal(lintResults?.[0]?.errorDetail, error)
|
||||
return result.errorDetail
|
||||
})
|
||||
assert.deepStrictEqual(
|
||||
errorsDetails,
|
||||
errors,
|
||||
`${fixturePath}: Expected errors`,
|
||||
)
|
||||
})
|
||||
}
|
||||
})
|
||||
@ -131,11 +177,6 @@ test("ensure the rule validates correctly", async (t) => {
|
||||
fixturePath:
|
||||
"test/fixtures/valid/existing-element-id-fragment/existing-element-id-fragment.md",
|
||||
},
|
||||
{
|
||||
name: "should be valid with an existing heading fragment (case insensitive)",
|
||||
fixturePath:
|
||||
"test/fixtures/valid/existing-heading-case-insensitive/existing-heading-case-insensitive.md",
|
||||
},
|
||||
{
|
||||
name: "should be valid with an existing heading fragment",
|
||||
fixturePath:
|
||||
@ -151,6 +192,11 @@ test("ensure the rule validates correctly", async (t) => {
|
||||
fixturePath:
|
||||
"test/fixtures/valid/only-parse-markdown-files-for-fragments/only-parse-markdown-files-for-fragments.md",
|
||||
},
|
||||
{
|
||||
name: "should support lines and columns range numbers in link fragments",
|
||||
fixturePath:
|
||||
"test/fixtures/valid/valid-line-column-range-number-fragment/valid-line-column-range-number-fragment.md",
|
||||
},
|
||||
{
|
||||
name: 'should be valid with valid heading "like" line number fragment',
|
||||
fixturePath:
|
||||
@ -186,8 +232,15 @@ test("ensure the rule validates correctly", async (t) => {
|
||||
|
||||
for (const { name, fixturePath } of testCases) {
|
||||
await t.test(name, async () => {
|
||||
const lintResults = await validateMarkdownLint(fixturePath)
|
||||
assert.equal(lintResults?.length, 0)
|
||||
const lintResults = (await validateMarkdownLint(fixturePath)) ?? []
|
||||
const errorsDetails = lintResults.map((result) => {
|
||||
return result.errorDetail
|
||||
})
|
||||
assert.equal(
|
||||
errorsDetails.length,
|
||||
0,
|
||||
`${fixturePath}: Expected no errors, got ${errorsDetails.join(", ")}`,
|
||||
)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user