spiegel-keyman/resources/tools/check-markdown/src/parse-files.ts
Marc Durdin 640d3e5096 test(developer): add markdown link check test for product documentation
Adds check-markdown package that parses .md files, looks for links and
images, and verifies that they are valid links (for internal links).
2024-09-25 15:51:32 -07:00

30 lines
758 B
TypeScript

import * as fs from 'node:fs';
import { posix as path } from 'node:path';
import { marked, Token } from 'marked';
import { type LinkRef } from './types.js';
let refLinks: any[] = [];
const walkTokens = (token: Token) => {
if (token.type === 'link' || token.type === 'image') {
refLinks.push(token);
}
};
marked.use( { walkTokens });
export function parseFiles(root: string, files: string[]): LinkRef[] {
const links: LinkRef[] = [];
for (const file of files) {
const fullPath = path.join(root, file);
if (fs.statSync(fullPath).isFile() && fullPath.endsWith('.md')) {
refLinks = [];
marked.parse(fs.readFileSync(fullPath, 'utf-8'));
links.push({ file, links: refLinks, messages: [] });
}
}
return links;
}