From 652d84e2966263511829f88462f208ccebc285c9 Mon Sep 17 00:00:00 2001 From: Daniel Kluge Date: Thu, 3 Nov 2022 14:49:32 +0100 Subject: [PATCH] Asciidoc rendering in backend --- lib/content/generateBackend.ts | 64 ++++++++++++++++++++++++------- lib/content/generateBrowser.ts | 40 +++++++++---------- pages/api/contentRendering.ts | 18 +++++++++ pages/blog/diary/[did].tsx | 2 +- pages/blog/diary/[did]/[page].tsx | 2 +- pages/blog/project/[pid].tsx | 2 +- 6 files changed, 92 insertions(+), 36 deletions(-) create mode 100644 pages/api/contentRendering.ts diff --git a/lib/content/generateBackend.ts b/lib/content/generateBackend.ts index 2c97420..0fa88f5 100644 --- a/lib/content/generateBackend.ts +++ b/lib/content/generateBackend.ts @@ -17,6 +17,20 @@ hljs.registerLanguage("bash", bash); hljs.registerLanguage("console", bash); hljs.registerLanguage("shell", bash); +interface APISuccess { + type: "success"; + html: string; + date: string; + repoUrl: string; +} + +interface APIError { + type: "error"; + html: string; +} + +export type APIReturn = APISuccess | APIError; + export const projectEmpty = "
Kein Projekt ausgewählt.
"; const projectNotFoundHtml = `
Sorry! There is no data for this project. Please check back later to see if that changed!
`; const projectServerErrorHtml = `
Sorry! A server error happend when the project data was fetched!
`; @@ -48,18 +62,19 @@ export async function getContentList() { } } -export async function generateContent(content: Project|Diary, selectedPage?: number): Promise { - if(!content) return projectEmpty; +export async function generateContent(content: Project|Diary, selectedPage?: number, api: boolean = false): Promise { + if(!content) return api ? {type: "error", html: projectEmpty} : projectEmpty; + switch (content.type) { - case "project": return await generateProjectHTML(content); - case "diary": return await generateDiaryHTML(content, selectedPage); + case "project": return await generateProjectHTML(content, api); + case "diary": return await generateDiaryHTML(content, selectedPage, api); default: return projectNotFoundHtml; } } -async function generateProjectHTML(project: Project): Promise { +async function generateProjectHTML(project: Project, api: boolean = false): Promise { // First we test if the file exist - if(!projectFiles.find((f) => f.name === `${project.name}.adoc`)) return projectNotFoundHtml; + if(!projectFiles.find((f) => f.name === `${project.name}.adoc`)) return api ? { type: "error", html: projectNotFoundHtml } : projectNotFoundHtml; // Resolve the path const path = resolve(projectPath, `${project.name}.adoc`); @@ -72,8 +87,19 @@ async function generateProjectHTML(project: Project): Promise { const pathsCorrected = rawAd.replace(/(image[:]+)(.*\.[a-zA-Z]+)\[/g, "$1/content/projects/$2["); const adDoc = ad.load(pathsCorrected, { attributes: { showtitle: true } }); + // 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`, + }; + // Return and add the footer - return `${adDoc.convert(adDoc).toString()} + return `${converted}