diff --git a/package-lock.json b/package-lock.json index 3c01d49..caf726d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "0.0.0-development", "license": "MIT", "dependencies": { - "markdown-it": "14.1.0" + "markdown-it": "14.1.0", + "mime": "4.1.0" }, "devDependencies": { "@types/markdown-it": "14.1.2", @@ -3637,7 +3638,6 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/mime/-/mime-4.1.0.tgz", "integrity": "sha512-X5ju04+cAzsojXKes0B/S4tcYtFAJ6tTMuSPBEn9CPGlrWr8Fiw7qYeLT0XyH80HSoAoqWCaz+MWKh22P7G1cw==", - "dev": true, "funding": [ "https://github.com/sponsors/broofa" ], diff --git a/package.json b/package.json index 4f26306..17caa96 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,8 @@ "release": "semantic-release" }, "dependencies": { - "markdown-it": "14.1.0" + "markdown-it": "14.1.0", + "mime": "4.1.0" }, "devDependencies": { "@types/markdown-it": "14.1.2", diff --git a/src/index.js b/src/index.js index bb191b3..7a27e2e 100644 --- a/src/index.js +++ b/src/index.js @@ -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") { diff --git a/test/fixtures/invalid/invalid-image.md b/test/fixtures/invalid/invalid-image.md new file mode 100644 index 0000000..af7434d --- /dev/null +++ b/test/fixtures/invalid/invalid-image.md @@ -0,0 +1,5 @@ +# Invalid + +![Image](../not-an-image.txt) + +![Image 2](mailto:not-an-image@pictures.com) diff --git a/test/fixtures/not-an-image.txt b/test/fixtures/not-an-image.txt new file mode 100644 index 0000000..283e5e9 --- /dev/null +++ b/test/fixtures/not-an-image.txt @@ -0,0 +1 @@ +not an image diff --git a/test/index.test.js b/test/index.test.js index ce931ca..1cadcc8 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -147,6 +147,14 @@ test("ensure the rule validates correctly", async (t) => { }, }, }, + { + name: "should be invalid with file with an image link that is not an image", + fixturePath: "test/fixtures/invalid/invalid-image.md", + errors: [ + '"../not-an-image.txt" should be an image', + '"mailto:not-an-image@pictures.com" should exist in the file system', + ], + }, ] for (const { name, fixturePath, errors, config = defaultConfig } of testCases) {