-
Notifications
You must be signed in to change notification settings - Fork 507
/
Copy pathfrontmatter.ts
48 lines (40 loc) · 1.28 KB
/
frontmatter.ts
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
import getReadingTime from 'reading-time';
import { toString } from 'mdast-util-to-string';
import { visit } from 'unist-util-visit';
import type { MarkdownAstroData, RehypePlugin, RemarkPlugin } from '@astrojs/markdown-remark';
export const readingTimeRemarkPlugin: RemarkPlugin = () => {
return function (tree, file) {
const textOnPage = toString(tree);
const readingTime = Math.ceil(getReadingTime(textOnPage).minutes);
(file.data.astro as MarkdownAstroData).frontmatter.readingTime = readingTime;
};
};
export const responsiveTablesRehypePlugin: RehypePlugin = () => {
return function (tree) {
if (!tree.children) return;
for (let i = 0; i < tree.children.length; i++) {
const child = tree.children[i];
if (child.type === 'element' && child.tagName === 'table') {
tree.children[i] = {
type: 'element',
tagName: 'div',
properties: {
style: 'overflow:auto',
},
children: [child],
};
i++;
}
}
};
};
export const lazyImagesRehypePlugin: RehypePlugin = () => {
return function (tree) {
if (!tree.children) return;
visit(tree, 'element', function (node) {
if (node.tagName === 'img') {
node.properties.loading = 'lazy';
}
});
};
};