diff --git a/website/src/pages/blog/[...slug].astro b/website/src/pages/blog/[...slug].astro index 0754ea7..f5d9763 100644 --- a/website/src/pages/blog/[...slug].astro +++ b/website/src/pages/blog/[...slug].astro @@ -1,6 +1,7 @@ --- import { type CollectionEntry, getCollection, render } from 'astro:content'; import BlogPost from '../../layouts/BlogPost.astro'; +import * as Gemtext from 'gemtext'; export async function getStaticPaths() { const posts = await getCollection('blog'); @@ -11,10 +12,47 @@ 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 { + 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)}
`; + } +}; + const post = Astro.props; -const { Content } = await render(post); +const Content = post.filePath?.endsWith('.gmi') + ? Gemtext.parse(post.body ?? '').generate(GemtextHTMLRenderer) + : (await render(post)).Content; --- - + { post.filePath?.endsWith('.gmi') ? : }