-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbundleMarkdown.ts
More file actions
118 lines (101 loc) · 3.29 KB
/
bundleMarkdown.ts
File metadata and controls
118 lines (101 loc) · 3.29 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
import parseFrontMatter from 'front-matter';
import { readdir, readFile } from 'fs/promises';
import { bundleMDX } from 'mdx-bundler';
import { join } from 'path';
import readingTime from 'reading-time';
import remarkAutolinkHeadings from 'rehype-autolink-headings';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import rehypeMathjax from 'rehype-mathjax';
import rehypePresetMinify from 'rehype-preset-minify';
import rehypePrismPlus from 'rehype-prism-plus';
import rehypeRaw from 'rehype-raw';
import rehypeSlug from 'rehype-slug';
import remarkBreaks from 'remark-breaks';
import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import { formatDate } from './formatDate';
import { remarkCodeTitles } from './remark/remark-code-title';
import { remarkInlineCodeLanguageCreator } from './remark/remark-inline-code-language';
import type { FrontMatter, FrontMatterMeta } from './types';
export type MarkdownAttributes = {
title: string;
};
const root = process.cwd();
type Matter = ReturnType<typeof bundleMDX>;
export async function bundleMarkdown(markdownPath: string): Promise<Matter> {
const source = await readFile(markdownPath, 'utf-8');
const remarkInlineCodeLanguage = await remarkInlineCodeLanguageCreator();
process.env.ESBUILD_BINARY_PATH = join(root, 'node_modules', 'esbuild', 'bin', 'esbuild');
process.env.NODE_ENV = 'production';
const post = await bundleMDX({
source,
mdxOptions(options) {
options.remarkPlugins = [
...(options.remarkPlugins ?? []),
// remarkMdxImages,
remarkBreaks,
remarkCodeTitles,
remarkInlineCodeLanguage,
[remarkAutolinkHeadings, { behavior: 'wrap' }],
rehypeSlug,
remarkGfm,
remarkMath,
];
options.rehypePlugins = [
...(options.rehypePlugins ?? []),
rehypeMathjax,
[rehypePrismPlus, { ignoreMissing: true }],
rehypePresetMinify,
[
rehypeRaw,
{
passThrough: [
'mdxjsEsm',
'mdxFlowExpression',
'mdxTextExpression',
'mdxJsxFlowElement',
'mdxJsxTextElement',
],
},
],
];
return options;
},
}).catch((e) => {
console.error(e);
throw e;
});
return {
...post,
frontmatter: {
meta: {
readingTime: readingTime(post.code),
...post.frontmatter.meta,
},
},
};
}
export type PostData = FrontMatterMeta & { formattedDate: string };
export async function getPosts(postsRootPath: string): Promise<PostData[]> {
const postsPath = await readdir(postsRootPath, {
withFileTypes: true,
});
const posts: PostData[] = [];
for (const dirent of postsPath) {
const file = await readFile(join(postsRootPath, dirent.name, 'index.md'));
const {
attributes: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
meta: { date, slug, ...meta },
},
} = parseFrontMatter(file.toString()) as { attributes: FrontMatter };
posts.push({
slug: dirent.name.replace(/\.mdx/, ''),
date: new Date(date).toISOString(),
formattedDate: formatDate(date),
...meta,
});
}
return posts.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
}