-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.ts
68 lines (60 loc) · 2.13 KB
/
index.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
import allPlugins from '@hashicorp/platform-remark-plugins/all'
import highlight from '@mapbox/rehype-prism'
import { Pluggable } from 'unified'
import remarkMath from 'remark-math'
import rehypeKatex from 'rehype-katex'
import rehypeSurfaceCodeNewlines from '@hashicorp/platform-code-highlighting/rehype-surface-code-newlines'
interface MarkdownDefaults {
remarkPlugins: Pluggable[]
rehypePlugins: Pluggable[]
}
export interface ContentPluginsOptions {
// TODO: implement this once our @hashicorp/remark-plugins package is typed (https://app.asana.com/0/1100423001970639/1199650430485548)
pluginOptions?: $TSFixMe
addRemarkPlugins?: Pluggable[]
addRehypePlugins?: Pluggable[]
resolveIncludes?: string
enableMath?: boolean
}
export default function markdownDefaults(
options: ContentPluginsOptions = {}
): MarkdownDefaults {
const res = {} as MarkdownDefaults
// Set default remark/rehype plugins
// Add user-provided remark plugins if present
const remarkDefaults: Pluggable[] = allPlugins(options.pluginOptions)
res.remarkPlugins = options.addRemarkPlugins
? [...remarkDefaults, ...options.addRemarkPlugins]
: remarkDefaults
const rehypeDefaults: Pluggable[] = [
[highlight, { ignoreMissing: true }],
rehypeSurfaceCodeNewlines,
]
res.rehypePlugins = options.addRehypePlugins
? [...rehypeDefaults, ...options.addRehypePlugins]
: rehypeDefaults
// Convenience option to replace `{ pluginOptions: { includeMarkdown: { resolveFrom: '<PATH>' } } }`
// with simply `{ resolveIncludes: '<PATH>' }`
if (options.resolveIncludes) {
res.remarkPlugins = res.remarkPlugins.map((entry) => {
const [plugin, opts] = Array.isArray(entry) ? entry : [entry, undefined]
if (
typeof plugin === 'function' &&
plugin.name === 'includeMarkdownPlugin'
) {
return [
plugin,
{ resolveMdx: true, resolveFrom: options.resolveIncludes, ...opts },
]
} else {
return entry
}
})
}
// Add math plugins if enabled
if (options.enableMath) {
res.remarkPlugins.push(remarkMath)
res.rehypePlugins.push(rehypeKatex)
}
return res
}