-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmdsvex.config.js
107 lines (98 loc) · 2.44 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
98
99
100
101
102
103
104
105
106
107
import { defineMDSveXConfig as defineConfig } from 'mdsvex'
import relativeImages from 'mdsvex-relative-images'
import path from 'node:path'
import autolinkHeadings from 'rehype-autolink-headings'
import rehypeExternalLinks from 'rehype-external-links'
import slugPlugin from 'rehype-slug'
import preview, { htmlFormatter, textFormatter } from 'remark-preview'
import { visit } from 'unist-util-visit'
const config = defineConfig({
extensions: ['.svelte.md', '.md', '.svx'],
smartypants: {
dashes: 'oldschool',
},
remarkPlugins: [
// Add a text preview snippet (no formatting) so we can use it in the meta description tag
preview(textFormatter({ length: 250, maxBlocks: 2 })),
// Add an HTML preview snippet (formatted) so we can use it when displaying all posts
preview(
htmlFormatter({
length: 250,
maxBlocks: 2,
}),
{
attribute: 'previewHtml',
},
),
posts,
videos,
relativeImages,
],
rehypePlugins: [
slugPlugin,
[
autolinkHeadings,
{
behavior: 'wrap',
},
],
[
rehypeExternalLinks,
{ target: '_blank', rel: 'noopener noreferrer' },
],
],
})
export default config
/**
* Add slug to metadata and convert `date` timezone to UTC
*/
function posts() {
return (_, file) => {
const parsed = path.parse(file.filename)
const slug =
parsed.name === 'index'
? path.parse(file.filename).dir.split('/').pop()
: parsed.name
// Calculate reading time
const content = file.contents.toString()
const words = content.split(/\s+/).length
const reading_time_minutes = Math.ceil(words / 230) // 230 words per minute
file.data.fm = {
...file.data.fm,
slug,
reading_time: {
minutes: reading_time_minutes,
text: `${reading_time_minutes} min read`,
time: reading_time_minutes * 60 * 1000, // milliseconds
words: words,
},
// remove timezone from parsed date
date: file.data.fm.date
? new Date(file.data.fm.date).toLocaleDateString()
: undefined,
}
}
}
/**
* 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}"
/>
`
}
})
}
}