mirror of
https://github.com/keymanapp/keyman.git
synced 2026-10-21 05:17:43 +00:00
Adds check-markdown package that parses .md files, looks for links and images, and verifies that they are valid links (for internal links).
30 lines
758 B
TypeScript
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;
|
|
}
|