1
mirror of https://github.com/importantimport/urara.git synced 2024-09-16 17:18:40 +08:00
urara/mdsvex.config.js

119 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

import { lex, parse as parseFence } from 'fenceparser'
import Slugger from 'github-slugger'
import { toString } from 'mdast-util-to-string'
import { escapeSvelte } from 'mdsvex'
import { join, parse } from 'node:path'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
2021-11-04 15:34:01 +08:00
import rehypeExternalLinks from 'rehype-external-links'
import rehypeSlug from 'rehype-slug'
2022-11-16 13:55:10 +08:00
import remarkFFF from 'remark-fff'
import remarkFootnotes from 'remark-footnotes'
import { createShikiHighlighter, renderCodeToHTML, runTwoSlash } from 'shiki-twoslash'
import { visit } from 'unist-util-visit'
const remarkUraraFm
= () =>
(tree, { data, filename }) => {
const filepath = filename ? filename.split('/src/routes')[1] : 'unknown'
const { dir, name } = parse(filepath)
if (!data.fm)
data.fm = {}
// Generate slug & path
data.fm.slug = filepath
data.fm.path = join(dir, `/${name}`.replace('/+page', '').replace('.svelte', ''))
// Generate ToC
if (data.fm.toc !== false) {
const [slugs, toc] = [new Slugger(), []]
visit(tree, 'heading', (node) => {
toc.push({
depth: node.depth,
slug: slugs.slug(toString(node), false),
title: toString(node),
})
2021-12-26 21:33:36 +08:00
})
if (toc.length > 0)
data.fm.toc = toc
else data.fm.toc = false
}
2021-12-26 21:33:36 +08:00
}
// Better type definitions needed
2023-08-28 18:04:51 +08:00
const remarkUraraSpoiler = () => tree =>
visit(tree, 'paragraph', (node) => {
const { children } = node
const text = children[0].value
const re = /\|\|(.+?)\|\|/g
if (re.test(children[0].value)) {
children[0].type = 'html'
2023-08-28 17:52:45 +08:00
children[0].value = text.replace(re, (_match, p1) => `<span class="spoiler">${p1}</span>`)
}
return node
})
2023-08-28 17:52:45 +08:00
/** @type {import("mdsvex").MdsvexOptions} */
2023-05-01 23:32:11 +08:00
export default {
2022-01-02 13:44:45 +08:00
extensions: ['.svelte.md', '.md'],
2021-11-04 15:34:01 +08:00
highlight: {
highlighter: async (code, lang, meta) => {
2023-08-28 17:52:45 +08:00
let fence, twoslash
2022-03-11 21:40:48 +08:00
try {
fence = parseFence(lex([lang, meta].filter(Boolean).join(' ')))
}
catch (error) {
2022-03-11 21:40:48 +08:00
throw new Error(`Could not parse the codefence for this code sample \n${code}`)
}
if (fence?.twoslash === true)
twoslash = runTwoSlash(code, lang)
2022-03-11 21:40:48 +08:00
return `{@html \`${escapeSvelte(
renderCodeToHTML(
code,
2023-08-28 17:52:45 +08:00
lang,
fence ?? {},
{ themeName: 'material-default' },
await createShikiHighlighter({ theme: 'material-default' }),
twoslash,
),
2022-03-11 21:40:48 +08:00
)}\` }`
},
2021-11-04 15:34:01 +08:00
},
layout: {
_: './src/lib/components/post_layout.svelte',
},
rehypePlugins: [
rehypeSlug,
[rehypeAutolinkHeadings, { behavior: 'wrap' }],
[
rehypeExternalLinks,
{
rel: ['nofollow', 'noopener', 'noreferrer', 'external'],
target: '_blank',
},
],
],
2022-11-07 13:43:11 +08:00
remarkPlugins: [
2022-11-16 13:55:10 +08:00
[
2023-05-27 21:39:52 +08:00
remarkFFF,
2022-11-16 13:55:10 +08:00
{
autofill: {
path: path => path.replace('/src/routes/', '/urara/'),
2022-11-16 13:55:10 +08:00
provider: 'fs',
2023-05-27 21:39:52 +08:00
},
presets: [],
2023-05-27 21:39:52 +08:00
strict: {
media: {
array: false,
2023-05-27 21:39:52 +08:00
type: 'string',
},
},
target: 'mdsvex',
},
2022-11-16 13:55:10 +08:00
],
2022-11-07 13:43:11 +08:00
remarkUraraFm,
remarkUraraSpoiler,
[remarkFootnotes, { inlineNotes: true }],
2022-11-07 13:43:11 +08:00
],
smartypants: {
dashes: 'oldschool',
},
2023-08-28 17:52:45 +08:00
}