limpiar código

This commit is contained in:
Cat /dev/Nulo 2023-06-12 18:14:14 -03:00
parent d6bb7c83d5
commit b51411a68b
3 changed files with 24 additions and 91 deletions

3
.prettierrc.json Normal file
View file

@ -0,0 +1,3 @@
{
"printWidth": 100
}

View file

@ -1,14 +1,5 @@
import { import { copyFile, mkdir, opendir, readFile, readdir, writeFile } from "fs/promises";
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 { promisify } from "util";
import * as commonmark from "commonmark"; import * as commonmark from "commonmark";
import { import {
a, a,
@ -34,8 +25,6 @@ import {
img, img,
} from "@nulo/html.js"; } from "@nulo/html.js";
const execFile = promisify(execFileCallback);
const reader = new commonmark.Parser({ smart: true }); const reader = new commonmark.Parser({ smart: true });
const writer = new commonmark.HtmlRenderer({ safe: false, smart: true }); const writer = new commonmark.HtmlRenderer({ safe: false, smart: true });
@ -68,19 +57,7 @@ for (const entry of dir) {
const { name } = entry; const { name } = entry;
const extension = extname(name); const extension = extname(name);
if ( if ([".ts", ".md", ".css", ".js", ".png", ".jpg", ".mp4", ".svg", ".html"].includes(extension)) {
[
".ts",
".md",
".css",
".js",
".png",
".jpg",
".mp4",
".svg",
".html",
].includes(extension)
) {
await copyFile(join(config.sourcePath, name), join(config.buildPath, name)); await copyFile(join(config.sourcePath, name), join(config.buildPath, name));
} }
@ -100,10 +77,7 @@ async function compilePage(config: Config, sourceFileName: string) {
let contentHtml, image; let contentHtml, image;
if (extname(sourceFileName) === ".md") { if (extname(sourceFileName) === ".md") {
({ html: contentHtml, image } = await compileMarkdownHtml( ({ html: contentHtml, image } = await compileMarkdownHtml(config, sourceFileName));
config,
sourceFileName
));
} else if (sourceFileName.endsWith(".gen.js")) } else if (sourceFileName.endsWith(".gen.js"))
contentHtml = await compileJavascript(config, sourceFileName); contentHtml = await compileJavascript(config, sourceFileName);
else throw false; else throw false;
@ -112,14 +86,7 @@ async function compilePage(config: Config, sourceFileName: string) {
...generateHead(title, name), ...generateHead(title, name),
article( article(
{ itemscope: "", itemtype: "https://schema.org/Article" }, { itemscope: "", itemtype: "https://schema.org/Article" },
...(isIndex ...(isIndex ? [] : generateHeader(name, sourceFileName, fileConnections.length > 0, image)),
? []
: generateHeader(
name,
sourceFileName,
fileConnections.length > 0,
image
)),
main({ itemprop: "articleBody" }, raw(contentHtml)), main({ itemprop: "articleBody" }, raw(contentHtml)),
...generateConnectionsSection(fileConnections) ...generateConnectionsSection(fileConnections)
) )
@ -142,10 +109,7 @@ async function compileMarkdownHtml(
config: Config, config: Config,
sourceFileName: string sourceFileName: string
): Promise<{ html: string; image?: Image }> { ): Promise<{ html: string; image?: Image }> {
let markdown = await readFile( let markdown = await readFile(join(config.sourcePath, sourceFileName), "utf-8");
join(config.sourcePath, sourceFileName),
"utf-8"
);
let image; let image;
if (markdown.startsWith("!!")) { if (markdown.startsWith("!!")) {
@ -153,8 +117,7 @@ async function compileMarkdownHtml(
const imageNode = node.firstChild?.firstChild; const imageNode = node.firstChild?.firstChild;
if (!imageNode || !imageNode.destination) if (!imageNode || !imageNode.destination)
throw new Error("Intenté parsear un ^!! pero no era una imágen"); throw new Error("Intenté parsear un ^!! pero no era una imágen");
if (!imageNode.firstChild?.literal) if (!imageNode.firstChild?.literal) console.warn(`El ^!! de ${sourceFileName} no tiene alt`);
console.warn(`El ^!! de ${sourceFileName} no tiene alt`);
image = { image = {
src: imageNode.destination, src: imageNode.destination,
@ -171,10 +134,7 @@ async function compileMarkdownHtml(
return { html: contentHtml, image }; return { html: contentHtml, image };
} }
async function compileJavascript( async function compileJavascript(config: Config, sourceFileName: string): Promise<string> {
config: Config,
sourceFileName: string
): Promise<string> {
const fn = await import("./" + join(config.sourcePath, sourceFileName)); const fn = await import("./" + join(config.sourcePath, sourceFileName));
return await fn.default(); return await fn.default();
} }
@ -192,34 +152,13 @@ function generateHead(titlee: string, outputName: string): Renderable[] {
name: "viewport", name: "viewport",
content: "width=device-width, initial-scale=1.0", content: "width=device-width, initial-scale=1.0",
}), }),
meta({ meta({ name: "author", content: "Nulo" }),
name: "author", meta({ property: "og:title", content: titlee }),
content: "Nulo", meta({ property: "og:type", content: "website" }),
}), meta({ property: "og:url", content: `https://nulo.ar/${outputName}.html` }),
meta({ meta({ property: "og:image", content: "cowboy.svg" }),
property: "og:title", link({ rel: "stylesheet", href: "drip.css" }),
content: titlee, link({ rel: "icon", href: "cowboy.svg" }),
}),
meta({
property: "og:type",
content: "website",
}),
meta({
property: "og:url",
content: `https://nulo.ar/${outputName}.html`,
}),
meta({
property: "og:image",
content: "cowboy.svg",
}),
link({
rel: "stylesheet",
href: "drip.css",
}),
link({
rel: "icon",
href: "cowboy.svg",
}),
title(titlee), title(titlee),
]; ];
} }
@ -239,10 +178,7 @@ interface Dateish {
day: number; day: number;
} }
function dateishToString({ year, month, day }: Dateish): string { function dateishToString({ year, month, day }: Dateish): string {
return `${year}-${String(month).padStart(2, "0")}-${String(day).padStart( return `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
2,
"0"
)}`;
} }
type TitleMetadata = type TitleMetadata =
@ -253,8 +189,7 @@ type TitleMetadata =
} }
| { date: Dateish }; | { date: Dateish };
function parseName(name: string): TitleMetadata { function parseName(name: string): TitleMetadata {
const titleWithDate = const titleWithDate = /^((?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}))? ?(?<title>.*)$/;
/^((?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}))? ?(?<title>.*)$/;
const found = name.match(titleWithDate); const found = name.match(titleWithDate);
if (!found || !found.groups) throw new Error("Algo raro pasó"); if (!found || !found.groups) throw new Error("Algo raro pasó");
@ -338,16 +273,12 @@ function generateHeader(
}, },
"Historial" "Historial"
), ),
...(linkConexiones ...(linkConexiones ? [" / ", a({ href: "#conexiones" }, "Conexiones")] : [])
? [" / ", a({ href: "#conexiones" }, "Conexiones")]
: [])
), ),
]; ];
} }
function generateConnectionsSection( function generateConnectionsSection(fileConnections: Connection[]): Renderable[] {
fileConnections: Connection[]
): Renderable[] {
return fileConnections.length > 0 return fileConnections.length > 0
? [ ? [
section( section(
@ -408,9 +339,7 @@ async function hackilyTransformHtml(html: string): Promise<string> {
html = html html = html
.replaceAll("<a h", '<a rel="noopener noreferrer" h') .replaceAll("<a h", '<a rel="noopener noreferrer" h')
.replaceAll(wikilinkExp, (_, l) => render(internalLink(l))); .replaceAll(wikilinkExp, (_, l) => render(internalLink(l)));
for (const [match, archivo] of html.matchAll( for (const [match, archivo] of html.matchAll(/<nulo-sitio-reemplazar-con archivo="(.+?)" \/>/g)) {
/<nulo-sitio-reemplazar-con archivo="(.+?)" \/>/g
)) {
if (!archivo.endsWith(".gen.js")) throw false; if (!archivo.endsWith(".gen.js")) throw false;
html = html.replace(match, await compileJavascript(config, archivo)); html = html.replace(match, await compileJavascript(config, archivo));
} }

View file

@ -7,6 +7,7 @@
"esModuleInterop": true, "esModuleInterop": true,
"strict": true, "strict": true,
"allowJs": true, "allowJs": true,
"checkJs": true "checkJs": true,
"noEmit": true
} }
} }