feat!: stricter validation of image links (check if the file is an image)

BREAKING CHANGE: links like ![Image](./not-an-image.txt) will now report
an error
This commit is contained in:
2026-02-05 17:46:14 +01:00
parent 12a22980ce
commit 211c71d75b
6 changed files with 30 additions and 5 deletions
+12 -2
View File
@@ -1,5 +1,6 @@
import { pathToFileURL } from "node:url"
import { fileURLToPath, pathToFileURL } from "node:url"
import fs from "node:fs"
import mime from "mime"
import { filterTokens } from "./markdownlint-rule-helpers/helpers.js"
import {
@@ -69,7 +70,7 @@ const relativeLinksRule = {
url = new URL(hrefSrc, pathToFileURL(params.name))
}
if (url.protocol !== "file:") {
if (url.protocol !== "file:" && type !== "image") {
continue
}
@@ -83,6 +84,15 @@ const relativeLinksRule = {
continue
}
const mimeType = mime.getType(fileURLToPath(url))
if (type === "image" && (mimeType == null || !mimeType.startsWith("image/"))) {
onError({
lineNumber,
detail: `${detail} should be an image`,
})
continue
}
if (url.hash.length <= 0) {
if (hrefSrc.includes("#")) {
if (type === "image") {