feat: support columns numbers (and line range) in links fragments

Fixes #7
This commit is contained in:
Théo LUDWIG 2024-05-27 22:26:21 +02:00
parent 2df95e97d8
commit 450cdb84f0
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
7 changed files with 123 additions and 23 deletions

View File

@ -11,6 +11,7 @@ const {
isValidIntegerString,
getNumberOfLines,
getLineNumberStringFromFragment,
lineFragmentRe,
} = require("./utils.js")
/** @typedef {import('markdownlint').Rule} MarkdownLintRule */
@ -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`,

View File

@ -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.
@ -151,6 +153,7 @@ const getLineNumberStringFromFragment = (fragment) => {
}
module.exports = {
lineFragmentRe,
convertHeadingToHTMLFragment,
getMarkdownHeadings,
getMarkdownIdOrAnchorNameFragments,

View File

@ -0,0 +1 @@
# Awesome

View File

@ -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)

View File

@ -0,0 +1,3 @@
# Awesome
## L12 Not A Line Link

View File

@ -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)

View File

@ -29,92 +29,126 @@ 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 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,
relativeLinksRule.description,
)
assert.equal(lintResults?.[0]?.errorDetail, error)
const lintResults = (await validateMarkdownLint(fixturePath)) ?? []
const errorsDetails = lintResults.map((result) => {
assert.deepEqual(result.ruleNames, relativeLinksRule.names)
assert.deepEqual(
result.ruleDescription,
relativeLinksRule.description,
)
return result.errorDetail
})
assert.deepStrictEqual(errorsDetails, errors)
})
}
})
@ -151,6 +185,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 +225,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,
`Expected no errors, got ${errorsDetails.join(", ")}`,
)
})
}
})