-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-agolia-index.mjs
executable file
·52 lines (45 loc) · 1.21 KB
/
generate-agolia-index.mjs
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
import { writeFileSync, readFileSync } from 'fs'
import { globby } from 'globby'
import matter from 'gray-matter'
const parseHeading = (content) => {
const [heading, ...body] = content.split('\n')
const parsedHeading = heading.trim()
const parsedBody = body.join(' ').trim()
return { heading: parsedHeading, body: parsedBody }
}
/*
"content": "string",
"hierarchy": {
"lvl0": "string",
"lvl1": "string",
"lvl2": "string"
},
"type": "lvl2",
"url": "string"
*/
const pages = await globby(['src/pages/**/*.mdx', 'src/pages/**/*.md'])
const objects = pages.flatMap((page) => {
const {
data: { title, description },
content
} = matter(readFileSync(page, 'utf8'))
const path = page
.replace('src/pages/', '/')
.replace('index', '')
.replace('.md', '')
.replace('.mdx', '')
const parsed = content
.split('#')
.map(parseHeading)
.filter((content) => content.heading !== '' && content.body !== '')
return parsed.map((block) => ({
url: path,
hierarchy: {
lvl0: title,
lvl1: block.heading
},
content: block.body,
type: 'lvl1'
}))
})
writeFileSync('public/agolia.json', JSON.stringify(objects))