detectar links internos muertos

This commit is contained in:
Cat /dev/Nulo 2023-04-16 16:16:17 -03:00
parent 4475c1a3f5
commit 3823c2546f

View file

@ -1,4 +1,11 @@
import { copyFile, mkdir, opendir, readFile, writeFile } from "fs/promises"; import {
copyFile,
mkdir,
opendir,
readFile,
readdir,
writeFile,
} from "fs/promises";
import { basename, extname, join } from "path"; import { basename, extname, join } from "path";
import { execFile as execFileCallback } from "child_process"; import { execFile as execFileCallback } from "child_process";
import { promisify } from "util"; import { promisify } from "util";
@ -54,19 +61,13 @@ const connections = await scanForConnections(config.sourcePath);
await mkdir(config.buildPath, { recursive: true }); await mkdir(config.buildPath, { recursive: true });
const dir = await opendir(config.sourcePath); const dir = await readdir(config.sourcePath, { withFileTypes: true });
let pageList: string[] = []; let pageList: { src: string }[] = [];
let promises: Promise<void>[] = []; for (const entry of dir) {
for await (const entry of dir) {
if (!entry.isFile()) continue; if (!entry.isFile()) continue;
promises.push(compileFile(entry.name)); const { name } = entry;
}
await Promise.all(promises);
await compilePageList(config, pageList);
async function compileFile(name: string) {
const extension = extname(name); const extension = extname(name);
if ( if (
[ [
".ts", ".ts",
@ -82,11 +83,14 @@ async function compileFile(name: string) {
) { ) {
await copyFile(join(config.sourcePath, name), join(config.buildPath, name)); await copyFile(join(config.sourcePath, name), join(config.buildPath, name));
} }
if ([".md"].includes(extension) || name.endsWith(".gen.js")) { if ([".md"].includes(extension) || name.endsWith(".gen.js")) {
pageList.push(basename(name, extension)); pageList.push({ src: name });
await compilePage(config, name);
} }
} }
await Promise.all(pageList.map(({ src }) => compilePage(config, src)));
await compilePageList(config, pageList);
async function compilePage(config: Config, sourceFileName: string) { async function compilePage(config: Config, sourceFileName: string) {
const name = basename(sourceFileName, extname(sourceFileName)); const name = basename(sourceFileName, extname(sourceFileName));
@ -162,6 +166,7 @@ async function compileMarkdownHtml(
const parsed = reader.parse(markdown); const parsed = reader.parse(markdown);
const markdownHtml = writer.render(parsed); const markdownHtml = writer.render(parsed);
checkLinks(sourceFileName, markdownHtml);
const contentHtml = await hackilyTransformHtml(markdownHtml); const contentHtml = await hackilyTransformHtml(markdownHtml);
return { html: contentHtml, image }; return { html: contentHtml, image };
} }
@ -354,7 +359,7 @@ function generateConnectionsSection(
: []; : [];
} }
async function compilePageList(config: Config, pageList: string[]) { async function compilePageList(config: Config, pageList: { src: string }[]) {
const name = "Lista de páginas"; const name = "Lista de páginas";
const outputPath = join(config.buildPath, name + ".html"); const outputPath = join(config.buildPath, name + ".html");
const html = render( const html = render(
@ -362,6 +367,7 @@ async function compilePageList(config: Config, pageList: string[]) {
...generateHeader(name, "compilar.ts"), ...generateHeader(name, "compilar.ts"),
ul( ul(
...pageList ...pageList
.map(({ src: name }) => basename(name, extname(name)))
.sort((a, b) => a.localeCompare(b, "es", { sensitivity: "base" })) .sort((a, b) => a.localeCompare(b, "es", { sensitivity: "base" }))
.map((name) => li(internalLink(name))) .map((name) => li(internalLink(name)))
) )
@ -411,6 +417,17 @@ async function hackilyTransformHtml(html: string): Promise<string> {
return html; return html;
} }
function checkLinks(srcName: string, html: string) {
const matches = html.matchAll(wikilinkExp);
const list = pageList.map(({ src }) => basename(src, extname(src)));
if (!matches) return;
for (const match of matches) {
if (!list.some((n) => n === match[1])) {
console.warn(`${srcName} linkea a ${match[1]}, pero no existe`);
}
}
}
// ============================================== // ==============================================
// Linking // Linking
// ============================================== // ==============================================