2024-01-10 00:01:13 +01:00
|
|
|
const { test } = require("node:test")
|
|
|
|
const assert = require("node:assert/strict")
|
|
|
|
|
|
|
|
const { markdownlint } = require("markdownlint").promises
|
|
|
|
|
|
|
|
const relativeLinksRule = require("../src/index.js")
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} fixtureFile
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
const validateMarkdownLint = async (fixtureFile) => {
|
|
|
|
const lintResults = await markdownlint({
|
|
|
|
files: [fixtureFile],
|
|
|
|
config: {
|
|
|
|
default: false,
|
|
|
|
"relative-links": true,
|
|
|
|
},
|
|
|
|
customRules: [relativeLinksRule],
|
|
|
|
})
|
|
|
|
return lintResults[fixtureFile]
|
|
|
|
}
|
|
|
|
|
|
|
|
test("ensure the rule validates correctly", async (t) => {
|
|
|
|
await t.test("should be invalid", async (t) => {
|
2024-01-12 00:33:05 +01:00
|
|
|
const testCases = [
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be invalid with an empty id fragment",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/invalid/empty-id-fragment/empty-id-fragment.md",
|
2024-05-27 22:26:21 +02:00
|
|
|
errors: ['"./awesome.md#" should have a valid fragment identifier'],
|
2024-01-12 00:33:05 +01:00
|
|
|
},
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be invalid with a name fragment other than for an anchor",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/invalid/ignore-name-fragment-if-not-an-anchor/ignore-name-fragment-if-not-an-anchor.md",
|
2024-05-27 22:26:21 +02:00
|
|
|
errors: [
|
2024-01-12 00:33:05 +01:00
|
|
|
'"./awesome.md#name-should-be-ignored" should have a valid fragment identifier',
|
2024-05-27 22:26:21 +02:00
|
|
|
],
|
2024-01-12 00:33:05 +01:00
|
|
|
},
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be invalid with a non-existing id fragment (data-id !== id)",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/invalid/ignore-not-an-id-fragment/ignore-not-an-id-fragment.md",
|
2024-05-27 22:26:21 +02:00
|
|
|
errors: [
|
2024-01-12 00:33:05 +01:00
|
|
|
'"./awesome.md#not-an-id-should-be-ignored" should have a valid fragment identifier',
|
2024-05-27 22:26:21 +02:00
|
|
|
],
|
2024-01-12 00:33:05 +01:00
|
|
|
},
|
2024-05-27 22:50:43 +02:00
|
|
|
{
|
|
|
|
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',
|
|
|
|
],
|
|
|
|
},
|
2024-01-31 01:14:27 +01:00
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be invalid with invalid heading with #L fragment",
|
2024-01-31 01:14:27 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/invalid/invalid-heading-with-L-fragment/invalid-heading-with-L-fragment.md",
|
2024-05-27 22:26:21 +02:00
|
|
|
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',
|
|
|
|
],
|
2024-01-31 01:14:27 +01:00
|
|
|
},
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be invalid with a invalid line number fragment",
|
2024-01-31 01:14:27 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/invalid/invalid-line-number-fragment/invalid-line-number-fragment.md",
|
2024-05-27 22:26:21 +02:00
|
|
|
errors: [
|
2024-01-31 01:14:27 +01:00
|
|
|
'"./awesome.md#L7" should have a valid fragment identifier, "./awesome.md#L7" should have at least 7 lines to be valid',
|
2024-05-27 22:26:21 +02:00
|
|
|
],
|
2024-01-31 01:14:27 +01:00
|
|
|
},
|
2024-01-12 00:33:05 +01:00
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be invalid with a non-existing anchor name fragment",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/invalid/non-existing-anchor-name-fragment/non-existing-anchor-name-fragment.md",
|
2024-05-27 22:26:21 +02:00
|
|
|
errors: [
|
2024-01-12 00:33:05 +01:00
|
|
|
'"./awesome.md#non-existing-anchor-name-fragment" should have a valid fragment identifier',
|
2024-05-27 22:26:21 +02:00
|
|
|
],
|
2024-01-12 00:33:05 +01:00
|
|
|
},
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be invalid with a non-existing element id fragment",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/invalid/non-existing-element-id-fragment/non-existing-element-id-fragment.md",
|
2024-05-27 22:26:21 +02:00
|
|
|
errors: [
|
2024-01-12 00:33:05 +01:00
|
|
|
'"./awesome.md#non-existing-element-id-fragment" should have a valid fragment identifier',
|
2024-05-27 22:26:21 +02:00
|
|
|
],
|
2024-01-12 00:33:05 +01:00
|
|
|
},
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be invalid with a non-existing heading fragment",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md",
|
2024-05-27 22:26:21 +02:00
|
|
|
errors: [
|
2024-01-12 00:33:05 +01:00
|
|
|
'"./awesome.md#non-existing-heading" should have a valid fragment identifier',
|
2024-05-27 22:26:21 +02:00
|
|
|
],
|
2024-01-12 00:33:05 +01:00
|
|
|
},
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be invalid with a link to an image with a empty fragment",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/invalid/ignore-empty-fragment-checking-for-image.md",
|
2024-05-27 22:26:21 +02:00
|
|
|
errors: [
|
2024-01-12 00:33:05 +01:00
|
|
|
'"../image.png#" should not have a fragment identifier as it is an image',
|
2024-05-27 22:26:21 +02:00
|
|
|
],
|
2024-01-12 00:33:05 +01:00
|
|
|
},
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be invalid with a link to an image with a fragment",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/invalid/ignore-fragment-checking-for-image.md",
|
2024-05-27 22:26:21 +02:00
|
|
|
errors: [
|
2024-01-12 00:33:05 +01:00
|
|
|
'"../image.png#non-existing-fragment" should not have a fragment identifier as it is an image',
|
2024-05-27 22:26:21 +02:00
|
|
|
],
|
2024-01-12 00:33:05 +01:00
|
|
|
},
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be invalid with a non-existing file",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath: "test/fixtures/invalid/non-existing-file.md",
|
2024-05-27 22:26:21 +02:00
|
|
|
errors: ['"./index.test.js" should exist in the file system'],
|
2024-01-12 00:33:05 +01:00
|
|
|
},
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be invalid with a non-existing image",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath: "test/fixtures/invalid/non-existing-image.md",
|
2024-05-27 22:26:21 +02:00
|
|
|
errors: ['"./image.png" should exist in the file system'],
|
2024-01-12 00:33:05 +01:00
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2024-05-27 22:26:21 +02:00
|
|
|
for (const { name, fixturePath, errors } of testCases) {
|
2024-01-12 00:33:05 +01:00
|
|
|
await t.test(name, async () => {
|
2024-05-27 22:26:21 +02:00
|
|
|
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
|
|
|
|
})
|
2024-05-27 22:50:43 +02:00
|
|
|
assert.deepStrictEqual(
|
|
|
|
errorsDetails,
|
|
|
|
errors,
|
|
|
|
`${fixturePath}: Expected errors`,
|
|
|
|
)
|
2024-01-12 00:33:05 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2024-01-10 00:01:13 +01:00
|
|
|
|
2024-01-12 00:33:05 +01:00
|
|
|
await t.test("should be valid", async (t) => {
|
|
|
|
const testCases = [
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be valid with an existing anchor name fragment",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/valid/existing-anchor-name-fragment/existing-anchor-name-fragment.md",
|
|
|
|
},
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be valid with an existing element id fragment",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/valid/existing-element-id-fragment/existing-element-id-fragment.md",
|
|
|
|
},
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be valid with an existing heading fragment",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md",
|
|
|
|
},
|
2024-01-31 21:56:55 +01:00
|
|
|
{
|
|
|
|
name: 'should be valid with an existing heading fragment with accents (e.g: "é")',
|
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/valid/existing-heading-with-accents/existing-heading-with-accents.md",
|
|
|
|
},
|
2024-01-31 00:10:41 +01:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
2024-05-27 22:26:21 +02:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
2024-01-31 01:14:27 +01:00
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: 'should be valid with valid heading "like" line number fragment',
|
2024-01-31 01:14:27 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/valid/valid-heading-like-number-fragment/valid-heading-like-number-fragment.md",
|
|
|
|
},
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be valid with valid line number fragment",
|
2024-01-31 01:14:27 +01:00
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/valid/valid-line-number-fragment/valid-line-number-fragment.md",
|
|
|
|
},
|
2024-01-12 00:33:05 +01:00
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be valid with an existing file",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath: "test/fixtures/valid/existing-file.md",
|
|
|
|
},
|
|
|
|
{
|
2024-01-31 21:56:55 +01:00
|
|
|
name: "should be valid with an existing image",
|
2024-01-12 00:33:05 +01:00
|
|
|
fixturePath: "test/fixtures/valid/existing-image.md",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "should ignore absolute paths",
|
|
|
|
fixturePath: "test/fixtures/valid/ignore-absolute-paths.md",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "should ignore external links",
|
|
|
|
fixturePath: "test/fixtures/valid/ignore-external-links.md",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "should ignore checking fragment in own file",
|
|
|
|
fixturePath:
|
|
|
|
"test/fixtures/valid/ignore-fragment-checking-in-own-file.md",
|
|
|
|
},
|
|
|
|
]
|
2024-01-10 00:01:13 +01:00
|
|
|
|
2024-01-12 00:33:05 +01:00
|
|
|
for (const { name, fixturePath } of testCases) {
|
|
|
|
await t.test(name, async () => {
|
2024-05-27 22:26:21 +02:00
|
|
|
const lintResults = (await validateMarkdownLint(fixturePath)) ?? []
|
|
|
|
const errorsDetails = lintResults.map((result) => {
|
|
|
|
return result.errorDetail
|
|
|
|
})
|
|
|
|
assert.equal(
|
|
|
|
errorsDetails.length,
|
|
|
|
0,
|
2024-05-27 22:50:43 +02:00
|
|
|
`${fixturePath}: Expected no errors, got ${errorsDetails.join(", ")}`,
|
2024-05-27 22:26:21 +02:00
|
|
|
)
|
2024-01-12 00:33:05 +01:00
|
|
|
})
|
|
|
|
}
|
2024-01-10 00:01:13 +01:00
|
|
|
})
|
|
|
|
})
|