-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdsvex.config.js
67 lines (63 loc) · 1.52 KB
/
mdsvex.config.js
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
import { visit } from 'unist-util-visit'
import autolinkHeadings from 'rehype-autolink-headings'
import slugPlugin from 'rehype-slug'
import relativeImages from 'mdsvex-relative-images'
import remarkHeadings from '@vcarl/remark-headings'
export default {
extensions: ['.svx', '.md'],
smartypants: {
dashes: 'oldschool'
},
remarkPlugins: [videos, relativeImages, headings],
rehypePlugins: [
slugPlugin,
[
autolinkHeadings,
{
behavior: 'wrap'
}
]
]
}
/**
* Adds support to video files in markdown image links
*/
function videos() {
const extensions = ['mp4', 'webm']
return function transformer(tree) {
visit(tree, 'image', (node) => {
if (extensions.some((ext) => node.url.endsWith(ext))) {
node.type = 'html'
node.value = `
<video
src="${node.url}"
autoplay
muted
playsinline
loop
title="${node.alt}"
/>
`
}
})
}
}
/**
* Parses headings and includes the result in metadata
*/
function headings() {
return function transformer(tree, vfile) {
// run remark-headings plugin
remarkHeadings()(tree, vfile)
// include the headings data in mdsvex frontmatter
vfile.data.fm ??= {}
vfile.data.fm.headings = vfile.data.headings.map((heading) => ({
...heading,
// slugify heading.value
id: heading.value
.toLowerCase()
.replace(/\s/g, '-')
.replace(/[^a-z0-9-]/g, '')
}))
}
}