-
Notifications
You must be signed in to change notification settings - Fork 4
/
mdsvex.config.js
97 lines (94 loc) · 3.28 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
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
import rehypeSlug from 'rehype-slug'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypeExternalLinks from 'rehype-external-links'
import { escapeSvelte } from 'mdsvex'
import { lex, parse as parseFence } from 'fenceparser'
import { renderCodeToHTML, runTwoSlash, createShikiHighlighter } from 'shiki-twoslash'
import { statSync } from 'fs'
import { parse, join } from 'path'
import { visit } from 'unist-util-visit'
import { toString } from 'mdast-util-to-string'
import Slugger from 'github-slugger'
import remarkFootnotes from 'remark-footnotes'
const remarkUraraFm =
() =>
(tree, { data, filename }) => {
const filepath = filename.split('/src/routes')[1]
let { dir, name } = parse(filepath)
if (!data.fm) data.fm = {}
// Generate slug & path
data.fm.slug = filepath
data.fm.path = join(dir, `/${name}`.replace('/index', '').replace('.svelte', ''))
// Auto-set layout as article
if (!data.fm.layout) data.fm.layout = 'article'
// Generate ToC
if (data.fm.toc !== false) {
let [slugs, toc] = [new Slugger(), []]
visit(tree, 'heading', node => {
toc.push({
depth: node.depth,
title: toString(node),
slug: slugs.slug(toString(node))
})
})
data.fm.toc = toc
}
// Auto-read created & updated
if (!data.fm.created || !data.fm.updated) {
const { ctime, mtime } = statSync(new URL(`./urara${filepath}`, import.meta.url))
if (!data.fm.created) data.fm.created = ctime
if (!data.fm.updated) data.fm.updated = mtime
}
}
const remarkUraraSpoiler = () => tree =>
visit(tree, 'paragraph', node => {
const { children } = node
const text = children[0].value
const re = /\|\|(.{1,}?)\|\|/g
if (re.test(children[0].value)) {
children[0].type = 'html'
children[0].value = text.replace(re, (_match, p1) => `<span class="spoiler">${p1}</span>`)
}
return node
})
export default /** @type {Parameters<typeof import("mdsvex").mdsvex>[0]} */ {
extensions: ['.svelte.md', '.md'],
smartypants: {
dashes: 'oldschool'
},
layout: {
article: './src/lib/components/layouts/article.svelte',
note: './src/lib/components/layouts/note.svelte',
photo: './src/lib/components/layouts/photo.svelte',
reply: './src/lib/components/layouts/reply.svelte',
_: './src/lib/components/layouts/article.svelte'
},
highlight: {
highlighter: async (code, lang, meta) => {
let fence, twoslash
try {
fence = parseFence(lex([lang, meta].filter(Boolean).join(' ')))
} catch (error) {
throw new Error(`Could not parse the codefence for this code sample \n${code}`)
}
if (fence?.twoslash === true) twoslash = runTwoSlash(code, lang)
return `{@html String.raw\`${escapeSvelte(
`<codecopy>` +
renderCodeToHTML(code, lang, fence ?? {}, {}, await createShikiHighlighter({ theme: 'github-dark' }), twoslash) +
`</codecopy>`
)}\` }`
}
},
remarkPlugins: [remarkUraraFm, remarkUraraSpoiler, [remarkFootnotes, { inlineNotes: true }]],
rehypePlugins: [
rehypeSlug,
[rehypeAutolinkHeadings, { behavior: 'wrap' }],
[
rehypeExternalLinks,
{
rel: ['nofollow', 'noopener', 'noreferrer', 'external'],
target: '_blank'
}
]
]
}