mirror of
https://github.com/theoludwig/markdownlint-rule-relative-links.git
synced 2025-05-27 11:37:24 +02:00
Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
bf9403ad84
|
|||
9675c7a275
|
|||
5af131b840
|
|||
f332c833ca
|
|||
e20ee54b05
|
|||
5c39afbe20
|
|||
cc9a1cf6a2
|
|||
1095647d41
|
2
.github/workflows/lint.yml
vendored
2
.github/workflows/lint.yml
vendored
@ -13,7 +13,7 @@ jobs:
|
||||
- uses: "actions/checkout@v4.1.1"
|
||||
|
||||
- name: "Setup Node.js"
|
||||
uses: "actions/setup-node@v4.0.1"
|
||||
uses: "actions/setup-node@v4.0.2"
|
||||
with:
|
||||
node-version: "lts/*"
|
||||
cache: "npm"
|
||||
|
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
@ -19,7 +19,7 @@ jobs:
|
||||
persist-credentials: false
|
||||
|
||||
- name: "Setup Node.js"
|
||||
uses: "actions/setup-node@v4.0.1"
|
||||
uses: "actions/setup-node@v4.0.2"
|
||||
with:
|
||||
node-version: "lts/*"
|
||||
cache: "npm"
|
||||
|
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@ -13,7 +13,7 @@ jobs:
|
||||
- uses: "actions/checkout@v4.1.1"
|
||||
|
||||
- name: "Setup Node.js"
|
||||
uses: "actions/setup-node@v4.0.1"
|
||||
uses: "actions/setup-node@v4.0.2"
|
||||
with:
|
||||
node-version: "lts/*"
|
||||
cache: "npm"
|
||||
|
@ -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"]
|
||||
}
|
||||
|
3889
package-lock.json
generated
3889
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
34
package.json
34
package.json
@ -37,35 +37,35 @@
|
||||
"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"
|
||||
},
|
||||
"dependencies": {
|
||||
"markdown-it": "14.0.0"
|
||||
"markdown-it": "14.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "18.4.4",
|
||||
"@commitlint/config-conventional": "18.4.4",
|
||||
"@types/markdown-it": "13.0.7",
|
||||
"@types/node": "20.11.0",
|
||||
"editorconfig-checker": "5.1.2",
|
||||
"@commitlint/cli": "19.1.0",
|
||||
"@commitlint/config-conventional": "19.1.0",
|
||||
"@types/markdown-it": "14.0.0",
|
||||
"@types/node": "20.12.5",
|
||||
"editorconfig-checker": "5.1.5",
|
||||
"eslint": "8.56.0",
|
||||
"eslint-config-conventions": "13.1.0",
|
||||
"eslint-config-conventions": "14.1.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": "50.0.1",
|
||||
"husky": "8.0.3",
|
||||
"lint-staged": "15.2.0",
|
||||
"markdownlint": "0.33.0",
|
||||
"markdownlint-cli2": "0.11.0",
|
||||
"eslint-plugin-unicorn": "51.0.1",
|
||||
"husky": "9.0.11",
|
||||
"lint-staged": "15.2.2",
|
||||
"markdownlint": "0.34.0",
|
||||
"markdownlint-cli2": "0.13.0",
|
||||
"pinst": "3.0.0",
|
||||
"prettier": "3.1.1",
|
||||
"semantic-release": "22.0.12",
|
||||
"typescript": "5.3.3"
|
||||
"prettier": "3.2.5",
|
||||
"semantic-release": "23.0.7",
|
||||
"typescript": "5.4.4"
|
||||
}
|
||||
}
|
||||
|
42
src/index.js
42
src/index.js
@ -8,6 +8,9 @@ const {
|
||||
convertHeadingToHTMLFragment,
|
||||
getMarkdownHeadings,
|
||||
getMarkdownIdOrAnchorNameFragments,
|
||||
isValidIntegerString,
|
||||
getNumberOfLines,
|
||||
getLineNumberStringFromFragment,
|
||||
} = require("./utils.js")
|
||||
|
||||
/** @typedef {import('markdownlint').Rule} MarkdownLintRule */
|
||||
@ -19,6 +22,7 @@ const customRule = {
|
||||
names: ["relative-links"],
|
||||
description: "Relative links should be valid",
|
||||
tags: ["links"],
|
||||
parser: "markdownit",
|
||||
function: (params, onError) => {
|
||||
filterTokens(params, "inline", (token) => {
|
||||
const children = token.children ?? []
|
||||
@ -72,7 +76,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 +93,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 +101,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 +125,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`,
|
||||
|
@ -15,7 +15,7 @@
|
||||
* @returns {void}
|
||||
*/
|
||||
const filterTokens = (params, type, handler) => {
|
||||
for (const token of params.tokens) {
|
||||
for (const token of params.parsers.markdownit.tokens) {
|
||||
if (token.type === type) {
|
||||
handler(token)
|
||||
}
|
||||
|
45
src/utils.js
45
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.
|
||||
@ -24,7 +26,7 @@ const convertHeadingToHTMLFragment = (inlineText) => {
|
||||
"",
|
||||
)
|
||||
.replace(/ /gu, "-"),
|
||||
)
|
||||
).toLowerCase()
|
||||
)
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
|
3
test/fixtures/valid/existing-heading-with-accents/awesome.md
vendored
Normal file
3
test/fixtures/valid/existing-heading-with-accents/awesome.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Awesome
|
||||
|
||||
## Développement
|
3
test/fixtures/valid/existing-heading-with-accents/existing-heading-with-accents.md
vendored
Normal file
3
test/fixtures/valid/existing-heading-with-accents/existing-heading-with-accents.md
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Valid
|
||||
|
||||
[Link fragment](./awesome.md#développement)
|
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)
|
@ -26,67 +26,80 @@ test("ensure the rule validates correctly", async (t) => {
|
||||
await t.test("should be invalid", async (t) => {
|
||||
const testCases = [
|
||||
{
|
||||
name: "with an empty id fragment",
|
||||
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',
|
||||
},
|
||||
{
|
||||
name: "with a name fragment other than for an anchor",
|
||||
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:
|
||||
'"./awesome.md#name-should-be-ignored" should have a valid fragment identifier',
|
||||
},
|
||||
{
|
||||
name: "with a non-existing id fragment (data-id !== id)",
|
||||
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:
|
||||
'"./awesome.md#not-an-id-should-be-ignored" should have a valid fragment identifier',
|
||||
},
|
||||
{
|
||||
name: "with a non-existing anchor name fragment",
|
||||
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',
|
||||
},
|
||||
{
|
||||
name: "should be invalid 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: "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:
|
||||
'"./awesome.md#non-existing-anchor-name-fragment" should have a valid fragment identifier',
|
||||
},
|
||||
{
|
||||
name: "with a non-existing element id fragment",
|
||||
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:
|
||||
'"./awesome.md#non-existing-element-id-fragment" should have a valid fragment identifier',
|
||||
},
|
||||
{
|
||||
name: "with a non-existing heading fragment",
|
||||
name: "should be invalid with a non-existing heading fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md",
|
||||
error:
|
||||
'"./awesome.md#non-existing-heading" should have a valid fragment identifier',
|
||||
},
|
||||
{
|
||||
name: "with a link to an image with a empty fragment",
|
||||
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:
|
||||
'"../image.png#" should not have a fragment identifier as it is an image',
|
||||
},
|
||||
{
|
||||
name: "with a link to an image with a fragment",
|
||||
name: "should be invalid with a link to an image with a fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/invalid/ignore-fragment-checking-for-image.md",
|
||||
error:
|
||||
'"../image.png#non-existing-fragment" should not have a fragment identifier as it is an image',
|
||||
},
|
||||
{
|
||||
name: "with a non-existing file",
|
||||
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',
|
||||
},
|
||||
{
|
||||
name: "with a non-existing image",
|
||||
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',
|
||||
},
|
||||
@ -109,26 +122,51 @@ test("ensure the rule validates correctly", async (t) => {
|
||||
await t.test("should be valid", async (t) => {
|
||||
const testCases = [
|
||||
{
|
||||
name: "with an existing anchor name fragment",
|
||||
name: "should be valid with an existing anchor name fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/valid/existing-anchor-name-fragment/existing-anchor-name-fragment.md",
|
||||
},
|
||||
{
|
||||
name: "with an existing element id fragment",
|
||||
name: "should be valid with an existing element id fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/valid/existing-element-id-fragment/existing-element-id-fragment.md",
|
||||
},
|
||||
{
|
||||
name: "with an existing heading fragment",
|
||||
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:
|
||||
"test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md",
|
||||
},
|
||||
{
|
||||
name: "with an existing file",
|
||||
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",
|
||||
},
|
||||
{
|
||||
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: 'should be valid with valid heading "like" line number fragment',
|
||||
fixturePath:
|
||||
"test/fixtures/valid/valid-heading-like-number-fragment/valid-heading-like-number-fragment.md",
|
||||
},
|
||||
{
|
||||
name: "should be valid with valid line number fragment",
|
||||
fixturePath:
|
||||
"test/fixtures/valid/valid-line-number-fragment/valid-line-number-fragment.md",
|
||||
},
|
||||
{
|
||||
name: "should be valid with an existing file",
|
||||
fixturePath: "test/fixtures/valid/existing-file.md",
|
||||
},
|
||||
{
|
||||
name: "with an existing image",
|
||||
name: "should be valid with an existing image",
|
||||
fixturePath: "test/fixtures/valid/existing-image.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")
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user