-
Notifications
You must be signed in to change notification settings - Fork 6
/
loadBlogPosts.ts
69 lines (58 loc) · 1.61 KB
/
loadBlogPosts.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
// import { defaultParsers, marky } from './deps/marky.ts'
// import { given } from './utils/misc.ts'
// const parsers = defaultParsers
import { MarkdownIt, meta, prism, anchor } from './deps/markdown-it.ts'
const markdownRenderer = new MarkdownIt({
html: true,
})
markdownRenderer.use(anchor, {
level: 2,
permalinkSymbol: '#',
permalink: true,
})
markdownRenderer.use(meta)
markdownRenderer.use(prism)
const getAllBlogPosts = () =>
Promise.all([...Deno.readDirSync('./blog')]
.filter(file => file.name.includes('.md'))
.map(readBlogPostFile))
const readBlogPostFile = (file: Deno.DirEntry) =>
Deno.readTextFile(`./blog/${file.name}`)
.then(md => markdownToBlogPost(file.name.split('.')[0], md))
const markdownToBlogPost = (slug: string, md: string): LocalPost => {
return {
kind: 'local',
html: markdownRenderer.render(md).replaceAll(/ aria-hidden="true"/gi, ''),
// @ts-ignore
meta: markdownRenderer.meta,
slug,
wordCount: wordCount(md)
}
}
const wordCount = (str: string) =>
str.split(/[\W]+/gi).length
export type Post = LocalPost | ExternalPost
export type LocalPost = {
kind: 'local',
html: string,
meta: {
title: string,
description?: string,
date: string,
tags: readonly string[],
test?: boolean
},
slug: string,
wordCount: number
}
export type ExternalPost = {
kind: 'external',
meta: {
title: string,
date: string,
tags: readonly string[],
href: string,
test?: boolean
},
}
export default getAllBlogPosts