diff --git a/website/src/pages/blog/[...slug].astro b/website/src/pages/blog/[...slug].astro index c362dfc..54cf9ac 100644 --- a/website/src/pages/blog/[...slug].astro +++ b/website/src/pages/blog/[...slug].astro @@ -1,7 +1,7 @@ --- import { type CollectionEntry, getCollection, render } from 'astro:content'; import BlogPost from '../../layouts/BlogPost.astro'; -import * as Gemtext from 'gemtext'; +import { renderGemtextToHtml } from '../../renderGemtextToHtml'; export async function getStaticPaths() { const posts = await getCollection('blog'); @@ -12,57 +12,9 @@ export async function getStaticPaths() { } type Props = CollectionEntry<'blog'>; -function htmlEscape(str: string) { - return str.replace(/&/g, '&').replace(//g, '>'); -} - -const GemtextHTMLRenderer: Gemtext.Renderer = { - preamble: function (): string { - return ''; - }, - postamble: function (): string { - return ''; - }, - text: function (content: string): string { - content = content.trim(); - if (content === '') { - return ''; - } - return `

${htmlEscape(content)}

` - }, - link: function (url: string, alt: string): string { - const imgExtensions: (string | undefined)[] = ['webp', 'png', 'svg', 'gif', 'jpg', 'jpeg', 'apng'] - if (imgExtensions.includes(url.split('.').at(-1)?.toLowerCase())) { - return `${alt}`; - } - return `

=> ${alt}

`; - }, - preformatted: function (content: string[], alt: string): string { - return `
${htmlEscape(content.join('\n'))}
` - }, - heading: function (level: number, text: string): string { - return `${htmlEscape(text)}`; - }, - unorderedList: function (content: string[]): string { - return ``; - }, - quote: function (content: string): string { - return `
${htmlEscape(content)}
`; - } -}; - -function postProcessGemtextToHtml(html: string): string { - html = mergeAdjacentBlockquotes(html); - return html; -} - -function mergeAdjacentBlockquotes(html: string): string { - return html.replaceAll(/\<\/blockquote\>\/gm, '
'); -} - const post = Astro.props; const Content = post.filePath?.endsWith('.gmi') - ? postProcessGemtextToHtml(Gemtext.parse((post.body ?? '').replace(`# ${post.data.title}`, '')).generate(GemtextHTMLRenderer)) + ? renderGemtextToHtml(post.body ?? '', post.data.title) : (await render(post)).Content; --- diff --git a/website/src/renderGemtextToHtml.ts b/website/src/renderGemtextToHtml.ts new file mode 100644 index 0000000..f934f1e --- /dev/null +++ b/website/src/renderGemtextToHtml.ts @@ -0,0 +1,72 @@ +import * as Gemtext from "gemtext"; + +export function renderGemtextToHtml(gemtext: string, title: string): string { + return postProcessGemtextToHtml( + Gemtext.parse(gemtext.replace(`# ${title}`, "")).generate( + GemtextHTMLRenderer, + ), + ); +} + +const GemtextHTMLRenderer: Gemtext.Renderer = { + preamble: function (): string { + return ""; + }, + + postamble: function (): string { + return ""; + }, + + text: function (content: string): string { + content = content.trim(); + if (content === "") { + return ""; + } + return `

${htmlEscape(content)}

`; + }, + + link: function (url: string, alt: string): string { + const imgExtensions: (string | undefined)[] = [ + "webp", + "png", + "svg", + "gif", + "jpg", + "jpeg", + "apng", + ]; + if (imgExtensions.includes(url.split(".").at(-1)?.toLowerCase())) { + return `${alt}`; + } + return `

=> ${alt}

`; + }, + + preformatted: function (content: string[], alt: string): string { + return `
${htmlEscape(content.join("\n"))}
`; + }, + + heading: function (level: number, text: string): string { + return `${htmlEscape(text)}`; + }, + + unorderedList: function (content: string[]): string { + return `
    ${content.map((li) => `
  • ${htmlEscape(li)}
  • `).join("")}
`; + }, + + quote: function (content: string): string { + return `
${htmlEscape(content)}
`; + }, +}; + +function htmlEscape(str: string) { + return str.replace(/&/g, "&").replace(//g, ">"); +} + +function postProcessGemtextToHtml(html: string): string { + html = mergeAdjacentBlockquotes(html); + return html; +} + +function mergeAdjacentBlockquotes(html: string): string { + return html.replaceAll(/\<\/blockquote\>\/gm, "
"); +}