feat: add root_path config option (#14)

Fixes #12
This commit is contained in:
Aleksandr Mezin
2025-05-27 09:47:03 +03:00
committed by GitHub
parent 9bb5ffe0ae
commit 8a449ad181
6 changed files with 83 additions and 19 deletions

View File

@ -0,0 +1,3 @@
# Valid
![Absolute Path](/test/fixtures/image.png)

View File

@ -5,17 +5,22 @@ import * as markdownlint from "markdownlint/promise"
import relativeLinksRule, { markdownIt } from "../src/index.js"
const defaultConfig = {
"relative-links": true,
}
/**
*
* @param {string} fixtureFile
* @param {Object} config
* @returns
*/
const validateMarkdownLint = async (fixtureFile) => {
const validateMarkdownLint = async (fixtureFile, config = defaultConfig) => {
const lintResults = await markdownlint.lint({
files: [fixtureFile],
config: {
default: false,
"relative-links": true,
...config,
},
customRules: [relativeLinksRule],
markdownItFactory: () => {
@ -146,11 +151,27 @@ test("ensure the rule validates correctly", async (t) => {
fixturePath: "test/fixtures/invalid/non-existing-image.md",
errors: ['"./image.png" should exist in the file system'],
},
{
name: "should be invalid with incorrect absolute paths",
fixturePath: "test/fixtures/config-dependent/absolute-paths.md",
errors: ['"/test/fixtures/image.png" should exist in the file system'],
config: {
"relative-links": {
root_path: "test",
},
},
},
]
for (const { name, fixturePath, errors } of testCases) {
for (const {
name,
fixturePath,
errors,
config = defaultConfig,
} of testCases) {
await t.test(name, async () => {
const lintResults = (await validateMarkdownLint(fixturePath)) ?? []
const lintResults =
(await validateMarkdownLint(fixturePath, config)) ?? []
const errorsDetails = lintResults.map((result) => {
assert.deepEqual(result.ruleNames, relativeLinksRule.names)
assert.deepEqual(
@ -219,7 +240,7 @@ test("ensure the rule validates correctly", async (t) => {
fixturePath: "test/fixtures/valid/existing-image.md",
},
{
name: "should ignore absolute paths",
name: "should ignore absolute paths if root_path is not set",
fixturePath: "test/fixtures/valid/ignore-absolute-paths.md",
},
{
@ -231,11 +252,21 @@ test("ensure the rule validates correctly", async (t) => {
fixturePath:
"test/fixtures/valid/ignore-fragment-checking-in-own-file.md",
},
{
name: "should be valid with correct absolute paths if root_path is set",
fixturePath: "test/fixtures/config-dependent/absolute-paths.md",
config: {
"relative-links": {
root_path: ".",
},
},
},
]
for (const { name, fixturePath } of testCases) {
for (const { name, fixturePath, config = defaultConfig } of testCases) {
await t.test(name, async () => {
const lintResults = (await validateMarkdownLint(fixturePath)) ?? []
const lintResults =
(await validateMarkdownLint(fixturePath, config)) ?? []
const errorsDetails = lintResults.map((result) => {
return result.errorDetail
})