frontpage/lib/content/generateBackend.ts

174 lines
7.0 KiB
TypeScript
Raw Permalink Normal View History

2022-10-07 23:03:39 +02:00
// This file is used to generate the HTML for the projects and diaries in the backend.
// We can use fs and stuff here.
2022-10-08 13:54:29 +02:00
import { Dirent, readdirSync } from "fs";
2022-10-07 23:03:39 +02:00
import { readFile } from "node:fs/promises";
import { resolve } from "path";
import { JSDOM } from "jsdom";
2022-10-07 23:03:39 +02:00
import type { Project, Diary } from "./types";
import asciidoctor from "asciidoctor";
// Code Highlighting
import hljs from "highlight.js";
import rust from "highlight.js/lib/languages/rust";
import bash from "highlight.js/lib/languages/shell";
hljs.registerLanguage("rust", rust);
hljs.registerLanguage("bash", bash);
hljs.registerLanguage("console", bash);
hljs.registerLanguage("shell", bash);
2022-11-03 14:49:32 +01:00
interface APISuccess {
type: "success";
html: string;
date: string;
repoUrl: string;
}
interface APIError {
type: "error";
html: string;
}
export type APIReturn = APISuccess | APIError;
2022-10-07 23:03:39 +02:00
export const projectEmpty = "<div>Kein Projekt ausgewählt.</div>";
const projectNotFoundHtml = `<div class="${"error"}">Sorry! There is no data for this project. Please check back later to see if that changed!</div>`;
const projectServerErrorHtml = `<div class="${"error"}">Sorry! A server error happend when the project data was fetched!</div>`;
const ad = asciidoctor();
2022-10-08 13:28:16 +02:00
const listPath = resolve("./public", "content", "list.json");
2022-10-07 23:03:39 +02:00
const projectPath = resolve("./public", "content", "projects");
const diaryPath = resolve("./public", "content", "diaries");
2022-10-08 13:54:29 +02:00
// Error catching as this is evaluated at build time
let pf: Dirent[] = [];
let df: Dirent[] = [];
try { pf = readdirSync(projectPath, { withFileTypes: true }).filter((f) => f.isFile() && f.name.endsWith(".adoc")) }
catch {}
2022-10-07 23:03:39 +02:00
// As we need the diaries too, no filter here
2022-10-08 13:54:29 +02:00
try { df = readdirSync(diaryPath, { withFileTypes: true }) }
catch {}
const projectFiles = pf;
const diaryFiles = df;
2022-10-07 23:03:39 +02:00
2022-10-08 13:28:16 +02:00
export async function getContentList() {
try {
const list = await readFile(listPath, { encoding: "utf-8" });
return JSON.parse(list);
} catch (e) {
console.error(e);
return [];
}
}
2022-11-03 14:49:32 +01:00
export async function generateContent(content: Project|Diary, selectedPage?: number, api: boolean = false): Promise<string|APIReturn> {
if(!content) return api ? {type: "error", html: projectEmpty} : projectEmpty;
2022-10-07 23:03:39 +02:00
switch (content.type) {
2022-11-03 14:49:32 +01:00
case "project": return await generateProjectHTML(content, api);
case "diary": return await generateDiaryHTML(content, selectedPage, api);
2022-10-07 23:03:39 +02:00
default: return projectNotFoundHtml;
}
}
2022-11-03 14:49:32 +01:00
async function generateProjectHTML(project: Project, api: boolean = false): Promise<string|APIReturn> {
2022-10-07 23:03:39 +02:00
// First we test if the file exist
2022-11-03 14:49:32 +01:00
if(!projectFiles.find((f) => f.name === `${project.name}.adoc`)) return api ? { type: "error", html: projectNotFoundHtml } : projectNotFoundHtml;
2022-10-07 23:03:39 +02:00
// Resolve the path
const path = resolve(projectPath, `${project.name}.adoc`);
try {
// Read the file
const rawAd = await readFile(path, { encoding: "utf-8" });
// Correct the paths so that the images are loaded correctly
const pathsCorrected = rawAd.replace(/(image[:]+)(.*\.[a-zA-Z]+)\[/g, "$1/content/projects/$2[");
const adDoc = ad.load(pathsCorrected, { attributes: { showtitle: true } });
2022-11-03 14:49:32 +01:00
// Convert to HTML
const converted = adDoc.convert(adDoc).toString();
// For the API we want the HTML and the date only
if (api) return {
type: "success",
html: converted,
date: new Date(adDoc.getAttribute("docdatetime")).toISOString(),
repoUrl: `https://git.c0ntroller.de/c0ntroller/frontpage-content/src/branch/${process.env.IS_DEV ? "dev" : "senpai"}/projects/${project.name}.adoc`,
};
2022-10-07 23:03:39 +02:00
// Return and add the footer
2022-11-03 14:49:32 +01:00
return `${converted}
2022-10-07 23:03:39 +02:00
<hr>
<div id="footer">
<div id="footer-text">
Last updated: ${new Date(adDoc.getAttribute("docdatetime")).toLocaleString()} | <a href="https://git.c0ntroller.de/c0ntroller/frontpage-content/src/branch/${process.env.IS_DEV ? "dev" : "senpai"}/projects/${project.name}.adoc" target="_blank">Document source</a>
</div>
</div>`;
} catch (e) {
// Something gone wrong
console.error(e);
2022-11-03 14:49:32 +01:00
return api ? { type: "error", html: projectServerErrorHtml } : projectServerErrorHtml;
2022-10-07 23:03:39 +02:00
}
}
2022-11-03 14:49:32 +01:00
async function generateDiaryHTML(diary: Diary, selectedPage?: number, api: boolean = false): Promise<string|APIReturn> {
2022-10-07 23:03:39 +02:00
// First we test if the file exist
2022-11-03 14:49:32 +01:00
if(!diaryFiles.find((f) => f.isFile() && f.name === `${diary.name}.adoc`)) return api ? { type: "error", html: projectNotFoundHtml } : projectNotFoundHtml;
2022-10-07 23:03:39 +02:00
// First we need the page number and the path to load
const page: number = Number.parseInt(selectedPage?.toString() || "0") - 1;
// If the page number is not -1, a directory must exist
2022-11-03 14:49:32 +01:00
if (page !== -1 && !diaryFiles.find((f) => f.isDirectory() && f.name === diary.name)) return api ? { type: "error", html: projectNotFoundHtml } : projectNotFoundHtml;
2022-10-07 23:03:39 +02:00
// Next we load the correct path
const path = page === -1 ? resolve(diaryPath, `${diary.name}.adoc`) : resolve(diaryPath, diary.name, `${diary.entries[page].filename}.adoc`);
try {
// Read the file
const rawAd = await readFile(path, { encoding: "utf-8" });
// Correct the paths so that the images are loaded correctly
const pathsCorrected = rawAd.replace(/(image[:]{1,2})(.*\.[a-zA-Z]+)\[/g, "$1/content/diaries/$2[");
const adDoc = ad.load(pathsCorrected, { attributes: { showtitle: true } });
const gitfile = page === -1 ? `${diary.name}.adoc` : `${diary.name}/${diary.entries[page].filename}.adoc`;
2022-11-03 14:49:32 +01:00
// Convert to HTML
const converted = adDoc.convert(adDoc).toString();
// For the API we want the HTML and the date only
if (api) return {
type: "success",
html: converted,
date: new Date(adDoc.getAttribute("docdatetime")).toISOString(),
repoUrl: `https://git.c0ntroller.de/c0ntroller/frontpage-content/src/branch/${process.env.IS_DEV ? "dev" : "senpai"}/diaries/${gitfile}`,
};
2022-10-07 23:03:39 +02:00
// Return and add the footer
2022-11-03 14:49:32 +01:00
return `${converted}
2022-10-07 23:03:39 +02:00
<hr>
<div id="footer">
<div id="footer-text">
Last updated: ${new Date(adDoc.getAttribute("docdatetime")).toLocaleString()} | <a href="https://git.c0ntroller.de/c0ntroller/frontpage-content/src/branch/${process.env.IS_DEV ? "dev" : "senpai"}/diaries/${gitfile}" target="_blank">Document source</a>
</div>
</div>`;
} catch (e) {
// Something gone wrong
console.error(e);
2022-11-03 14:49:32 +01:00
return api ? { type: "error", html: projectServerErrorHtml } : projectServerErrorHtml;
2022-10-07 23:03:39 +02:00
}
}
2022-10-18 16:46:33 +02:00
export function prepareDOM(html: string) {
const dom = (new JSDOM(html)).window.document;
dom.querySelectorAll("pre code").forEach((block) => {
hljs.highlightElement(block as HTMLElement);
});
2022-10-18 16:46:33 +02:00
dom.querySelectorAll("a[href^='#']").forEach((link) => {
(link as HTMLAnchorElement).href = `/blog/${(link as HTMLAnchorElement).href.split("#")[1]}`;
});
return dom.body.innerHTML;
2022-10-07 23:03:39 +02:00
}