webapp/mdx.config.ts

35 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-09-09 11:53:35 +00:00
import type { AstroUserConfig } from 'astro'
2022-10-04 11:38:57 +00:00
import type { RehypePlugin } from '@astrojs/markdown-remark'
2022-09-09 11:53:35 +00:00
import { selectAll } from 'hast-util-select'
2022-10-04 11:38:57 +00:00
import rehypeToc from '@jsdevtools/rehype-toc'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypeSlug from 'rehype-slug'
import remarkCodeTitles from 'remark-code-titles'
2022-09-09 11:53:35 +00:00
const write =
(cl) =>
({ properties }) => {
properties.className = properties.className ? properties.className + ' ' + cl : cl
}
2022-10-04 11:38:57 +00:00
2022-09-09 11:53:35 +00:00
const adder = ([selector, className]) => {
const writer = write(className)
return (node) => selectAll(selector, node).forEach((el) => writer(el as any))
}
2022-10-04 11:38:57 +00:00
2022-09-09 11:53:35 +00:00
const addClasses = (additions) => {
const adders = Object.entries(additions).map((entry) => adder(entry))
return (node) => adders.forEach((a) => a(node))
}
export const markdownOptions: AstroUserConfig['markdown'] = {
2022-10-04 11:38:57 +00:00
remarkPlugins: [remarkCodeTitles],
2022-09-09 11:53:35 +00:00
rehypePlugins: [
2022-10-04 11:38:57 +00:00
rehypeSlug,
2022-10-04 12:16:07 +00:00
[rehypeToc as RehypePlugin<any>, { headings: ['h1', 'h2', 'h3'] }],
2022-10-04 11:38:57 +00:00
[rehypeAutolinkHeadings, { behavior: 'prepend' }],
2022-09-09 11:53:35 +00:00
[addClasses, { 'h1,h2,h3': 'title' }]
2022-10-04 11:38:57 +00:00
],
extendDefaultPlugins: true
2022-09-09 11:53:35 +00:00
}