-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
+server.ts
70 lines (61 loc) 路 1.47 KB
/
+server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { json } from '@sveltejs/kit'
import matter from 'gray-matter'
export const prerender = true
const patterns: Record<string, RegExp> = {
frontmatter: /---.*?---/gs,
code: /```.*?\n|```/gs,
inline: /`([^`]*)`/g,
heading: /^#{1,6}\s.*$/gm,
link: /\[([^\]]+)\]\(([^)]+)\)/g,
image: /\!\[.*?\]\(.*?\)/g,
blockquote: /> /gm,
bold: /\*\*/g,
italic: /\b_([^_]+)_(?!\w)/g,
special: /{%.*?%}/g,
tags: /[<>]/g,
}
const htmlEntities: Record<string, string> = {
'<': '<',
'>': '>',
}
function stripMarkdown(markdown: string) {
for (const pattern in patterns) {
switch (pattern) {
case 'inline':
markdown = markdown.replace(patterns[pattern], '$1')
break
case 'tags':
markdown = markdown.replace(
patterns[pattern],
(match) => htmlEntities[match]
)
break
case 'link':
markdown = markdown.replace(patterns[pattern], '$2')
break
case 'italic':
markdown = markdown.replace(patterns[pattern], '$1')
break
default:
markdown = markdown.replace(patterns[pattern], '')
}
}
return markdown
}
export async function GET() {
const paths = import.meta.glob('/posts/**/*.md', { as: 'raw', eager: true })
const posts = Object.entries(paths)
.map(([_, content]) => {
const frontmatter = matter(content)
if (frontmatter.data.draft) {
return null
}
return {
title: frontmatter.data.title,
slug: frontmatter.data.slug,
content: stripMarkdown(content),
}
})
.filter(Boolean)
return json(posts)
}