Asciidoc rendering in backend

This commit is contained in:
2022-11-03 14:49:32 +01:00
parent 55d57d6a80
commit 652d84e296
6 changed files with 92 additions and 36 deletions

View File

@@ -2,16 +2,12 @@
// It is used by the terminal.
import type { Project, Diary } from "./types";
import asciidoctor from "asciidoctor";
import type { APIReturn } from "./generateBackend";
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();
const isDev = typeof window !== "undefined" ? window.location.host.startsWith("dev") : false;
export async function generateContent(content: Project|Diary, selectedPage?: number): Promise<string> {
if(!content) return projectEmpty;
switch (content.type) {
@@ -22,33 +18,37 @@ export async function generateContent(content: Project|Diary, selectedPage?: num
}
async function generateProjectHTML(project: Project): Promise<string> {
const resp = await fetch(`/content/projects/${project.name}.adoc`);
const resp = await fetch(`/api/contentRendering?name=${project.name}`);
if (resp.status !== 200) return projectServerErrorHtml;
const rawAd = await resp.text();
const pathsCorrected = rawAd.replace(/(image[:]+)(.*\.[a-zA-Z]+)\[/g, "$1/content/projects/$2[");
const adDoc = ad.load(pathsCorrected, { attributes: { showtitle: true } });
return `${adDoc.convert(adDoc).toString()}
const response = await resp.json() as APIReturn;
if (!response || !response.type) return projectServerErrorHtml;
if (response.type === "error") return response.html;
else {
return `${response.html}
<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/${isDev ? "dev" : "senpai"}/projects/${project.name}.adoc" target="_blank">Document source</a>
Last updated: ${new Date(response.date).toLocaleString()} | <a href="${response.repoUrl}" target="_blank">Document source</a>
</div>
</div>`;
}
}
async function generateDiaryHTML(diary: Diary, selectedPage?: number): Promise<string> {
const page: number = Number.parseInt(selectedPage?.toString() || "0") - 1;
const resp = page === -1 ? await fetch(`/content/diaries/${diary.name}.adoc`) : await fetch(`/content/diaries/${diary.name}/${diary.entries[page].filename}.adoc`);
if (resp.status !== 200) return projectServerErrorHtml;
const rawAd = await resp.text();
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`;
return `${adDoc.convert(adDoc).toString()}
const url = `/api/contentRendering?name=${diary.name}${selectedPage ? `&page=${selectedPage}` : ""}`;
const resp = await fetch(url);
const response = await resp.json() as APIReturn;
if (!response || !response.type) return projectServerErrorHtml;
if (response.type === "error") return response.html;
return `${response.html}
<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/${isDev ? "dev" : "senpai"}/diaries/${gitfile}" target="_blank">Document source</a>
Last updated: ${new Date(response.date).toLocaleString()} | <a href="${response.repoUrl}" target="_blank">Document source</a>
</div>
</div>`;
}