-
Notifications
You must be signed in to change notification settings - Fork 1
/
astro.config.ts
107 lines (101 loc) · 3.11 KB
/
astro.config.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
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
import { type AstroIntegration, type AstroIntegrationLogger } from "astro"
import { defineConfig } from "astro/config"
import mdx from "@astrojs/mdx"
import sitemap from "@astrojs/sitemap"
import { rehypeHeadingIds } from "@astrojs/markdown-remark"
import { spawn } from "node:child_process"
import { readFile, writeFile } from "node:fs/promises"
import { dirname, relative } from "node:path"
import { fileURLToPath } from "node:url"
import { rehype } from "rehype"
import type { Options as RehypeAutolinkOptions } from "rehype-autolink-headings"
import rehypeAutolinkHeadings from "rehype-autolink-headings"
interface AstroAutolinkOptions {
paths: string[]
rehypeAutolinkOptions?: Readonly<RehypeAutolinkOptions> | null | undefined
}
const astroAutolinkHeadings = (
astroAutolinkOptions: AstroAutolinkOptions,
): AstroIntegration => {
const { paths, rehypeAutolinkOptions } = astroAutolinkOptions
const integrationName = "astro-autolink-headings"
return {
name: integrationName,
hooks: {
"astro:build:done": async ({
logger,
}: {
logger: AstroIntegrationLogger
}) => {
const integrationLogger = logger.fork(`${integrationName}/build`)
paths.forEach(async (path) => {
integrationLogger.info(`Processing ${path}`)
const fileContents = await readFile(path)
const processedFileContents = await rehype()
.use(rehypeHeadingIds)
.use(rehypeAutolinkHeadings, rehypeAutolinkOptions)
.process(fileContents)
await writeFile(path, String(processedFileContents))
})
},
},
}
}
const rehypeAutolinkOptions: RehypeAutolinkOptions = {
behavior: "prepend",
content: {
type: "element",
tagName: "span",
properties: {
className: ["anchor-icon"],
},
children: [],
},
headingProperties: { tabIndex: "-1", className: ["heading-element"] },
properties: { ariaLabel: "Link to self", className: ["anchor-link"] },
test: ["h2", "h3", "h4", "h5", "h6"],
}
const astroAutolinkOptions: AstroAutolinkOptions = {
paths: ["./dist/about/index.html"],
rehypeAutolinkOptions: rehypeAutolinkOptions,
}
const astroSearch = (): AstroIntegration => {
const integrationName = "astro-search"
return {
name: integrationName,
hooks: {
"astro:build:done": ({ dir }: { dir: URL }) => {
const targetDir = fileURLToPath(dir)
const cwd = dirname(fileURLToPath(import.meta.url))
const relativeDir = relative(cwd, targetDir)
return new Promise<void>((resolve) => {
spawn("pagefind", ["--site", relativeDir], {
stdio: "inherit",
shell: true,
cwd,
}).on("close", () => resolve())
})
},
},
}
}
export default defineConfig({
integrations: [
astroAutolinkHeadings(astroAutolinkOptions),
astroSearch(),
mdx(),
sitemap(),
],
markdown: {
rehypePlugins: [
rehypeHeadingIds,
[rehypeAutolinkHeadings, rehypeAutolinkOptions],
],
shikiConfig: {
theme: "dracula",
},
smartypants: false,
},
prefetch: true,
site: "https://www.bws.bio",
})