-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmdsvex.options.js
More file actions
182 lines (172 loc) · 5.02 KB
/
mdsvex.options.js
File metadata and controls
182 lines (172 loc) · 5.02 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { escapeSvelte } from 'mdsvex';
import { h } from 'hastscript';
// Rehype Plugins:
import rehypeSlug from 'rehype-slug';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import rehypeToc from '@jsdevtools/rehype-toc';
import urls from 'rehype-urls';
// Shiki Highlighter:
import { createHighlighter, makeSingletonHighlighter } from 'shiki/bundle/web';
// Highlighter Singleton:
const getHighlighter = makeSingletonHighlighter(createHighlighter);
// Open external links in new tab:
function processUrl(url, node) {
if (node.tagName === 'a') {
node.properties.class = 'text-link';
if (!url.href.startsWith('/')) {
node.properties.target = '_blank';
node.properties.rel = 'noopener';
}
}
}
// Customize TOC:
const customizeTOC = (toc) => {
try {
const { children } = toc;
const childrenOfChildren = children?.[0]?.children;
if (!children?.length || !childrenOfChildren?.length) return null;
} catch (e) {
return null;
}
return {
type: 'element',
tagName: 'div',
properties: { className: 'mb-4 border-b border-neutral-300 dark:border-neutral-800' },
children: [
{
type: 'element',
tagName: 'span',
properties: { className: 'mt-0 mb-0 font-medium tracking-tight' },
children: [
{
type: 'text',
value: 'Table of Contents:'
}
]
},
...(toc.children || [])
]
};
};
// Copy to cliboard button:
function addCopyButton(options = {}) {
const toggleMs = options.toggle || 2000;
return {
name: 'shiki-copy-button',
pre(node) {
node.properties = node.properties || {};
let iconSize = 14;
let iconStrokeSize = 1.95;
const button = h(
'button',
{
class: 'shiki-copy-button-styles',
title: 'Copy code',
'data-code': this.source,
onclick: `
navigator.clipboard.writeText(this.dataset.code);
this.classList.add('copied');
setTimeout(() => this.classList.remove('copied'), ${toggleMs});
`
},
[
h('span', { class: 'icon-copy-container' }, [
h(
'svg',
{
xmlns: 'http://www.w3.org/2000/svg',
width: iconSize,
height: iconSize,
viewBox: '0 0 24 24',
fill: 'none',
title: 'Copy code',
stroke: 'currentColor',
'stroke-width': iconStrokeSize,
'stroke-linecap': 'round',
'stroke-linejoin': 'round',
class: 'icon-copy shiki-copy-icon-colors'
},
[
h('rect', {
width: '14',
height: '14',
x: '8',
y: '8',
rx: '2',
ry: '2'
}),
h('path', {
d: 'M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2'
})
]
)
]),
h('span', { class: 'icon-copied-container' }, [
h(
'svg',
{
xmlns: 'http://www.w3.org/2000/svg',
width: iconSize,
height: iconSize,
viewBox: '0 0 24 24',
title: 'Copied!',
fill: 'none',
stroke: 'currentColor',
'stroke-width': iconStrokeSize,
'stroke-linecap': 'round',
'stroke-linejoin': 'round',
class: 'icon-copied shiki-copy-icon-colors'
},
[h('path', { d: 'M18 6 7 17l-5-5' }), h('path', { d: 'm22 10-7.5 7.5L13 16' })]
)
])
]
);
const wrapper = h('div', { class: 'code-container', style: 'position: relative;' }, [
node,
button
]);
return wrapper;
}
};
}
/** @type {import('mdsvex').MdsvexOptions} */
export const mdsvexOptions = {
extensions: ['.md'],
layout: {
_: './src/components/mdsvex/md-components.svelte'
},
rehypePlugins: [
[urls, processUrl],
rehypeSlug,
[
rehypeAutolinkHeadings,
{
behavior: 'wrap',
properties: {
className: `before:content-['#'] before:absolute before:-ml-[1em] before:text-neutral-100/0 dark:hover:before:text-neutral-200/50 hover:before:text-neutral-900/50 pl-[1em] -ml-[1em]`
}
}
],
[rehypeToc, { customizeTOC }]
],
highlight: {
highlighter: async (code, lang = 'text') => {
const highlighter = await getHighlighter({
themes: ['github-light', 'github-dark'],
langs: ['javascript', 'typescript', 'bash', 'json', 'jsx', 'css', 'tsx']
});
const html = escapeSvelte(
highlighter.codeToHtml(code, {
lang,
themes: {
light: 'github-light',
dark: 'github-dark'
},
transformers: [addCopyButton()]
})
);
return `{@html \`${html}\` }`;
}
}
};