forked from importantimport/urara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdsvex.config.js
83 lines (80 loc) · 2.82 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
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'
const remarkUraraFm =
() =>
(tree, { data, filename }) => {
const filepath = filename.split('/src/routes')[1]
let { dir, name } = parse(filepath)
if (!data.fm) data.fm = {}
data.fm.slug = filepath
data.fm.path = join(dir, `/${name}`.replace('/index', '').replace('.svelte', ''))
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
}
if (!data.fm.date || !data.fm.lastmod) {
const { ctime, mtime } = statSync(new URL(`./urara${filepath}`, import.meta.url))
if (!data.fm.date) data.fm.date = ctime
if (!data.fm.lastmod) data.fm.lastmod = 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
})
/** @type {Parameters<typeof import("mdsvex").mdsvex>[0]} */
export const mdsvexConfig = {
extensions: ['.svelte.md', '.md'],
smartypants: {
dashes: 'oldschool'
},
layout: {
_flex: './src/lib/components/layout_flex.svelte',
_card: './src/lib/components/layout_card.svelte',
_: './src/lib/components/layout_post.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 \`${escapeSvelte(renderCodeToHTML(code, lang, fence ?? {}, {}, await createShikiHighlighter({ theme: 'material-default' }), twoslash))}\` }`
}
},
remarkPlugins: [remarkUraraFm, remarkUraraSpoiler],
rehypePlugins: [
rehypeSlug,
[rehypeAutolinkHeadings, { behavior: 'wrap' }],
[
rehypeExternalLinks,
{
rel: ['nofollow', 'noopener', 'noreferrer', 'external'],
target: '_blank'
}
]
]
}