factor out renderGemtextToHtml()
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
---
|
---
|
||||||
import { type CollectionEntry, getCollection, render } from 'astro:content';
|
import { type CollectionEntry, getCollection, render } from 'astro:content';
|
||||||
import BlogPost from '../../layouts/BlogPost.astro';
|
import BlogPost from '../../layouts/BlogPost.astro';
|
||||||
import * as Gemtext from 'gemtext';
|
import { renderGemtextToHtml } from '../../renderGemtextToHtml';
|
||||||
|
|
||||||
export async function getStaticPaths() {
|
export async function getStaticPaths() {
|
||||||
const posts = await getCollection('blog');
|
const posts = await getCollection('blog');
|
||||||
@@ -12,57 +12,9 @@ export async function getStaticPaths() {
|
|||||||
}
|
}
|
||||||
type Props = CollectionEntry<'blog'>;
|
type Props = CollectionEntry<'blog'>;
|
||||||
|
|
||||||
function htmlEscape(str: string) {
|
|
||||||
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
||||||
}
|
|
||||||
|
|
||||||
const GemtextHTMLRenderer: Gemtext.Renderer<string> = {
|
|
||||||
preamble: function (): string {
|
|
||||||
return '';
|
|
||||||
},
|
|
||||||
postamble: function (): string {
|
|
||||||
return '';
|
|
||||||
},
|
|
||||||
text: function (content: string): string {
|
|
||||||
content = content.trim();
|
|
||||||
if (content === '') {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
return `<p>${htmlEscape(content)}</p>`
|
|
||||||
},
|
|
||||||
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 `<img src="${url}" alt="${alt}" />`;
|
|
||||||
}
|
|
||||||
return `<p>=> <a href="${url}">${alt}</a></p>`;
|
|
||||||
},
|
|
||||||
preformatted: function (content: string[], alt: string): string {
|
|
||||||
return `<pre alt="${alt}">${htmlEscape(content.join('\n'))}</pre>`
|
|
||||||
},
|
|
||||||
heading: function (level: number, text: string): string {
|
|
||||||
return `<h${level}>${htmlEscape(text)}</h${level}>`;
|
|
||||||
},
|
|
||||||
unorderedList: function (content: string[]): string {
|
|
||||||
return `<ul>${content.map(li=>`<li>${htmlEscape(li)}</li>`).join('')}</ul>`;
|
|
||||||
},
|
|
||||||
quote: function (content: string): string {
|
|
||||||
return `<blockquote>${htmlEscape(content)}</blockquote>`;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
function postProcessGemtextToHtml(html: string): string {
|
|
||||||
html = mergeAdjacentBlockquotes(html);
|
|
||||||
return html;
|
|
||||||
}
|
|
||||||
|
|
||||||
function mergeAdjacentBlockquotes(html: string): string {
|
|
||||||
return html.replaceAll(/\<\/blockquote\>\<blockquote\>/gm, '<br>');
|
|
||||||
}
|
|
||||||
|
|
||||||
const post = Astro.props;
|
const post = Astro.props;
|
||||||
const Content = post.filePath?.endsWith('.gmi')
|
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;
|
: (await render(post)).Content;
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -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<string> = {
|
||||||
|
preamble: function (): string {
|
||||||
|
return "";
|
||||||
|
},
|
||||||
|
|
||||||
|
postamble: function (): string {
|
||||||
|
return "";
|
||||||
|
},
|
||||||
|
|
||||||
|
text: function (content: string): string {
|
||||||
|
content = content.trim();
|
||||||
|
if (content === "") {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return `<p>${htmlEscape(content)}</p>`;
|
||||||
|
},
|
||||||
|
|
||||||
|
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 `<img src="${url}" alt="${alt}" />`;
|
||||||
|
}
|
||||||
|
return `<p>=> <a href="${url}">${alt}</a></p>`;
|
||||||
|
},
|
||||||
|
|
||||||
|
preformatted: function (content: string[], alt: string): string {
|
||||||
|
return `<pre alt="${alt}">${htmlEscape(content.join("\n"))}</pre>`;
|
||||||
|
},
|
||||||
|
|
||||||
|
heading: function (level: number, text: string): string {
|
||||||
|
return `<h${level}>${htmlEscape(text)}</h${level}>`;
|
||||||
|
},
|
||||||
|
|
||||||
|
unorderedList: function (content: string[]): string {
|
||||||
|
return `<ul>${content.map((li) => `<li>${htmlEscape(li)}</li>`).join("")}</ul>`;
|
||||||
|
},
|
||||||
|
|
||||||
|
quote: function (content: string): string {
|
||||||
|
return `<blockquote>${htmlEscape(content)}</blockquote>`;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
function htmlEscape(str: string) {
|
||||||
|
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
||||||
|
}
|
||||||
|
|
||||||
|
function postProcessGemtextToHtml(html: string): string {
|
||||||
|
html = mergeAdjacentBlockquotes(html);
|
||||||
|
return html;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mergeAdjacentBlockquotes(html: string): string {
|
||||||
|
return html.replaceAll(/\<\/blockquote\>\<blockquote\>/gm, "<br>");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user