This commit is contained in:
2025-06-19 13:40:31 +02:00
parent 24d03302b6
commit 60b66ef57c
9 changed files with 138 additions and 28 deletions

View File

@@ -6,6 +6,7 @@ import DOMPurify from 'dompurify';
import { JSDOM } from 'jsdom';
import chokidar from 'chokidar';
import type { FSWatcher } from 'chokidar';
import hljs from 'highlight.js';
export interface Post {
slug: string;
@@ -26,6 +27,22 @@ function getFileCreationDate(filePath: string): Date {
return stats.birthtime;
}
const renderer = new marked.Renderer();
renderer.code = (code, infostring, escaped) => {
const lang = (infostring || '').match(/\S*/)?.[0];
const highlighted = lang && hljs.getLanguage(lang)
? hljs.highlight(code, { language: lang }).value
: hljs.highlightAuto(code).value;
const langClass = lang ? `language-${lang}` : '';
return `<pre><code class="hljs ${langClass}">${highlighted}</code></pre>`;
};
marked.setOptions({
gfm: true,
breaks: true,
renderer,
});
export async function getPostBySlug(slug: string): Promise<Post> {
const realSlug = slug.replace(/\.md$/, '');
const fullPath = path.join(postsDirectory, `${realSlug}.md`);
@@ -35,10 +52,6 @@ export async function getPostBySlug(slug: string): Promise<Post> {
let processedContent = '';
try {
marked.setOptions({
gfm: true,
breaks: true
});
const rawHtml = marked.parse(content);
const window = new JSDOM('').window;
const purify = DOMPurify(window);