@storm-software/markdownlint
Version:
An opinionated collection of markdownlint rules used by Storm Software.
64 lines (61 loc) • 2.59 kB
JavaScript
import {
__commonJS
} from "./chunk-UJCSKKID.mjs";
// src/rules/no-default-alt-text.ts
var require_no_default_alt_text = __commonJS({
"src/rules/no-default-alt-text.ts"(exports, module) {
var defaultScreenshotRegex = `(?:screen|clean) ?(?:shot|cast) \\d{4}-\\d{2}-\\d{2}[^'"\\]]*`;
var imageRegex = "image";
var combinedRegex = `(${[defaultScreenshotRegex, imageRegex].join("|")})`;
var markdownAltRegex = new RegExp(`!\\[${combinedRegex}\\]\\(.*\\)`, "gid");
var htmlAltRegex = new RegExp(`alt=["']${combinedRegex}["']`, "gid");
module.exports = {
names: ["SSW01", "no-default-alt-text"],
description: "Images should have meaningful alternative text (alt text)",
information: new URL(
"https://github.com/github/markdownlint-github/blob/main/docs/rules/GH001-no-default-alt-text.md"
),
tags: ["accessibility", "images"],
function: function SS001(params, onError) {
const htmlTagsWithImages = params.parsers.markdownit.tokens.filter(
(token) => {
return token.type === "html_block" && token.content.includes("<img") || token.type === "inline" && token.content.includes("<img") && token.children.some((child) => child.type === "html_inline");
}
);
const inlineImages = params.parsers.markdownit.tokens.filter(
(token) => token.type === "inline" && token.children.some((child) => child.type === "image")
);
for (const token of [...htmlTagsWithImages, ...inlineImages]) {
const lineRange = token.map;
const lineNumber = token.lineNumber;
const lines = params.lines.slice(lineRange[0], lineRange[1]);
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
let matches;
if (token.type === "inline") {
if (token.children.some((child) => child.type === "html_inline")) {
matches = line.matchAll(htmlAltRegex);
} else {
matches = line.matchAll(markdownAltRegex);
}
} else {
matches = line.matchAll(htmlAltRegex);
}
for (const match of matches) {
const altText = match[1];
const [startIndex] = match.indices[1];
onError({
lineNumber: lineNumber + i,
range: [startIndex + 1, altText.length],
detail: `Flagged alt: ${altText}`
});
}
}
}
}
};
}
});
export {
require_no_default_alt_text
};