From 52e874b071a155e8a17ded633ad6b132be01f4b4 Mon Sep 17 00:00:00 2001 From: Nulo Date: Tue, 20 Dec 2022 16:13:46 -0300 Subject: [PATCH] =?UTF-8?q?compilar:=20Permitir=20inyectar=20contenido=20d?= =?UTF-8?q?e=20otra=20p=C3=A1gina?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compilar.ts | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/compilar.ts b/compilar.ts index 4ceed74..2e5ca8d 100644 --- a/compilar.ts +++ b/compilar.ts @@ -66,10 +66,20 @@ async function scanForConnections(sourcePath: string) { return connections; } -function hackilyTransformHtml(html: string) { - return html +async function hackilyTransformHtml(html: string): Promise { + html = html .replaceAll("$1`); + for (const [match, archivo] of html.matchAll( + //g + )) { + if (!promises[archivo]) + throw new Error( + ` no existe!` + ); + html = html.replace(match, await promises[archivo]); + } + return html; } const connections = await scanForConnections(config.sourcePath); @@ -78,16 +88,16 @@ await mkdir(config.buildPath, { recursive: true }); const dir = await opendir(config.sourcePath); let pageList: string[] = []; -let promises = []; +let promises: { [key: string]: Promise } = {}; for await (const entry of dir) { if (!entry.isFile()) continue; - promises.push(compileFile(entry.name)); + promises[entry.name] = compileFile(entry.name); } -await Promise.all(promises); +await Promise.all(Object.values(promises)); await compilePageList(config, pageList); -async function compileFile(name: string) { +async function compileFile(name: string): Promise { const extension = extname(name); if ( [".js", ".md", ".css", ".png", ".jpg", ".mp4", ".svg", ".html"].includes( @@ -100,8 +110,9 @@ async function compileFile(name: string) { pageList.push(basename(name, extension)); } - if (extension === ".md") await compileMarkdown(config, name); - else if (extension === ".gen") await compileExecutable(config, name); + if (extension === ".md") return await compileMarkdown(config, name); + else if (extension === ".gen") return await compileExecutable(config, name); + return ""; } async function compilePageList(config: Config, pageList: string[]) { @@ -118,7 +129,10 @@ async function compilePageList(config: Config, pageList: string[]) { `; await writeFile(outputPath, html); } -async function compileMarkdown(config: Config, sourceFileName: string) { +async function compileMarkdown( + config: Config, + sourceFileName: string +): Promise { const name = basename(sourceFileName, ".md"); const markdown = await readFile( join(config.sourcePath, sourceFileName), @@ -130,10 +144,11 @@ async function compileMarkdown(config: Config, sourceFileName: string) { const isIndex = sourceFileName === "index.md"; const title = isIndex ? "nulo.in" : name; + const contentHtml = await hackilyTransformHtml(markdownHtml); const html = head(title, sourceFileName) + (isIndex ? "" : header(title, sourceFileName, fileConnections.length > 0)) + - hackilyTransformHtml(markdownHtml) + + contentHtml + (fileConnections.length > 0 ? `
@@ -151,9 +166,13 @@ async function compileMarkdown(config: Config, sourceFileName: string) { basename(sourceFileName, ".md") + ".html" ); await writeFile(outputPath, html); + return contentHtml; } -async function compileExecutable(config: Config, sourceFileName: string) { +async function compileExecutable( + config: Config, + sourceFileName: string +): Promise { const name = basename(sourceFileName, ".gen"); const { stdout, stderr } = await execFile( @@ -168,6 +187,7 @@ async function compileExecutable(config: Config, sourceFileName: string) { basename(sourceFileName, ".gen") + ".html" ); await writeFile(outputPath, html); + return stdout; } // ==============================================