-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.up.ts
More file actions
182 lines (176 loc) · 5.02 KB
/
db.up.ts
File metadata and controls
182 lines (176 loc) · 5.02 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import fs, { readFileSync, writeFileSync } from 'fs'
import { relative, resolve, parse } from 'path'
import { log } from 'isomorphic-git'
import { scanAsync, type Dree } from 'dree'
import { fromMarkdown } from 'mdast-util-from-markdown'
import { select, selectAll } from 'unist-util-select'
import { toString } from 'mdast-util-to-string'
import { frontmatter } from 'micromark-extension-frontmatter'
import { frontmatterFromMarkdown } from 'mdast-util-frontmatter'
import toml from 'toml'
import { z } from 'zod'
import { util } from 'webpack'
import lunr from 'lunr'
import { isURL } from '@/lib/utils'
type ExtendedDree = Omit<Dree, 'children'> & {
title: string
image: string
authors: string[]
tags: string[]
description: string
content: string
children?: ExtendedDree[]
date: Date
}
type Tree = {
route: string
parent: string
children: Tree[]
title: string
image: string
authors: string[]
tags: string[]
description: string
content: string
date: Date
}
async function dreelize(root: string): Promise<ExtendedDree | null> {
const dree = await scanAsync<ExtendedDree>(
root,
{
size: false,
sizeInBytes: false,
hash: false,
matches: '**/page.{md,mdx}',
extensions: ['md', 'mdx'],
},
async (node) => {
const file = readFileSync(node.path)
const md = fromMarkdown(Uint8Array.from(file), {
extensions: [frontmatter(['yaml', 'toml'])],
mdastExtensions: [frontmatterFromMarkdown(['yaml', 'toml'])],
})
const matter = select('root > toml', md)
const heading = select('root > heading', md) || {}
const paragraph = select('root > paragraph', md) || {}
const text = selectAll('heading, paragraph', md)
const images = selectAll('image', md)
const [image = ''] = images.map((image) => {
try {
const { url } = Object.assign({ url: '' }, image)
if (isURL(url)) return url
const { dir } = parse(node.path)
const { name, ext } = parse(url)
const img = readFileSync(resolve(dir, url))
const hash = util
.createHash('xxhash64')
.update(img)
.digest('hex')
.toString()
.substring(0, 8)
const out = `/_next/static/media/${name}.${hash}${ext}`
return out
} catch {
return ''
}
})
const commits = await log({
fs,
dir: './',
filepath: relative('./', node.path),
force: true,
follow: true,
})
const authors = commits
.map(({ commit: { author } }) => author.name)
.filter((e, i, a) => a.indexOf(e) === i)
const { tags, date } = z
.object({
tags: z
.string()
.default('')
.transform((tags) =>
tags
.split(',')
.map((e) => e.trim())
.filter((e) => !!e),
),
date: z.coerce.date().default(new Date()),
})
.parse(toml.parse(toString(matter)))
node.title = toString(heading)
node.image = image
node.authors = authors
node.tags = tags
node.date = date
node.description = toString(paragraph)
node.content = text
.map((e) => toString(e))
.join(' ')
.replaceAll('\n', ' ')
},
)
return dree
}
function trielize(parent: string, { name, children = [] }: ExtendedDree): Tree {
const route = `${parent}/${name}`
const index = children.findIndex(({ type }) => type === 'file')
const [
only = {
title: '',
image: '',
authors: [],
tags: [],
description: '',
value: '',
content: '',
date: new Date(),
},
] = index >= 0 ? children.splice(index, 1) : []
return {
route,
parent,
children: children
.map((child) => trielize(route, child))
.sort((a, b) => {
if (a.date > b.date) return -1
if (b.date > a.date) return 1
if (a.title > b.title) return -1
if (b.title > a.title) return 1
return 0
}),
title: only.title,
image: only.image,
authors: only.authors,
tags: only.tags,
description: only.description,
content: only.content || '',
date: only.date,
}
}
function flatten({ children = [], ...node }: Tree): Blog[] {
return [
{ ...node, children: children.map(({ route }) => route) },
...children.map((child) => flatten(child)).flat(),
]
}
async function migrate() {
// Parse data
const root = resolve(process.cwd(), './src/app/blog')
const dree = await dreelize(root)
if (!dree) throw new Error('Empty contents')
const tree = trielize('', dree)
// Write table
const table = flatten(tree)
writeFileSync('src/db/table.json', JSON.stringify(table, null, 2))
// Write index
const document = lunr(function () {
this.ref('route')
this.field('title')
this.field('description')
this.field('content')
table.forEach((doc) => this.add(doc))
})
writeFileSync('src/db/index.json', JSON.stringify(document, null, 2))
}
migrate()