-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathsearch-index.astro
52 lines (42 loc) · 1.69 KB
/
search-index.astro
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 { splitTextByHeadings } from "../scripts/split-text-by-headings"
import { MarkdownInstance } from "astro"
import fs from "node:fs"
const pagesGlob = import.meta.glob("./**/*.astro", { eager: true })
const pages = Object.values(pagesGlob)
const postsMdxGlob = import.meta.glob<MarkdownInstance<Record<string, any>>>("../content/**/*.mdx", { eager: true })
const postsMdx = Object.values(postsMdxGlob).filter((post) => !post.frontmatter.draft)
const postsMdxWithContent = postsMdx.map((post) => {
const url = new URL(post.file, import.meta.url)
const content = fs.readFileSync(url, "utf-8")
const splittedContent = splitTextByHeadings(content)
const chunks = splittedContent.map((chunk) => ({
...post,
rawContent: () => chunk,
}))
return chunks
})
const markDownFiles = postsMdxWithContent.flat()
const transformMarkdownInstancesToIndexes = async (instances: MarkdownInstance<Record<string, any>>[]) => {
return Promise.all(
instances.map(async (instance) => {
const { title, section, metadata } = instance.frontmatter
const headings = await instance.getHeadings()
const content = instance.rawContent ? await instance.rawContent() : undefined
const href = instance.url?.replace("src/content", "").replace(".mdx", "")
return {
title,
headings,
url: href,
section,
description: metadata?.description ?? undefined,
content,
}
})
)
}
const index = await transformMarkdownInstancesToIndexes(markDownFiles)
const stringifiedJson = JSON.stringify({ index }, null, 2)
fs.writeFileSync(`${process.cwd()}/public/search-index.json`, stringifiedJson)
---
<meta http-equiv="refresh" content="0; url=/" />