-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
iles.config.ts
145 lines (132 loc) · 4.25 KB
/
iles.config.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
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
/* eslint-disable no-param-reassign */
import { stat } from 'fs/promises';
import { defineConfig } from 'iles';
import excerpt from '@islands/excerpt';
import feed from '@islands/feed';
import headings from '@islands/headings';
import images, { formatPreset } from '@islands/images';
import { simpleGit } from 'simple-git';
import got from 'got';
import { all as m2hAll } from 'mdast-util-to-hast';
import { remarkDefinitionList, defListHastHandlers } from 'remark-definition-list';
import remarkMermaid from './src/remark/remarkMermaid';
import remarkShiki from './src/remark/remarkShiki';
import addHeadingForCard from './src/remark/addHeadingForCard';
import { remarkMojikumi, remarkRehypeMojikumi } from './src/remark/mojikumi';
import setHeadingLevels from './src/remark/setHeadingLevels';
import checkRelativeLinks from './src/remark/checkRelativeLinks';
import breakLongCode from './src/rehype/breakLongCode';
import wrapTableOverflowAuto from './src/rehype/wrapTableOverflowAuto';
import generateFonts from './src/misc/font';
import generateSitemap from './src/misc/sitemap';
export default defineConfig({
siteUrl: 'https://ouuan.moe/',
turbo: false,
modules: [
excerpt(),
feed(),
headings(),
images({
normal: formatPreset({
formats: {
webp: { quality: 80 },
original: {},
},
inferDimensions: true,
}),
}),
],
async extendFrontmatter(frontmatter, filename) {
if (filename.includes('/post/')) {
frontmatter.layout ||= 'post';
} else if (filename.includes('/hidden/')) {
frontmatter.layout ||= 'hidden';
}
frontmatter.tags = frontmatter.tags?.length
? Array.from(new Set(frontmatter.tags)).filter((tag) => tag) : null;
const { image } = frontmatter;
if (typeof image === 'string' && image && !image.includes('/')) {
frontmatter.image = filename
.replace('src/pages/post/', '/images/')
.replace(/[^/]+$/, () => image);
}
const log = (await simpleGit().log({
file: filename,
strictDate: true,
})).all.filter((commit) => !commit.body.includes('[log skip]'));
const latestCommit = log[0];
const firstCommit = log.at(-1);
if (latestCommit && firstCommit) {
frontmatter.lastUpdated = new Date(latestCommit.date);
frontmatter.published = new Date(firstCommit.date);
} else {
const { birthtime, mtime } = await stat(filename);
frontmatter.lastUpdated = mtime;
frontmatter.published = birthtime;
}
const postHref = filename.match(/^src\/pages\/(.*)\.mdx?$/)?.[1]?.toLowerCase();
if (process.env.NODE_ENV === 'production' && postHref) {
const data = await got.get(
new URL(
`/api/visitors/${encodeURIComponent(postHref)}`,
'https://blog-visitor-count.ouuan.moe',
).href,
).json<{ visitors: number; }>();
frontmatter.visitor = data.visitors;
} else {
frontmatter.visitor = 0;
}
},
markdown: {
withImageSrc(src, file) {
const prefix = /^[@~]/.test(src) ? '' : file.path
.replace(__dirname, '')
.replace(/\/src\/pages\/(post|hidden)\//, '@/images/')
.replace(/[^/]+$/, '');
const suffix = src.includes('?') ? '' : '?preset=normal';
return `${prefix}${src}${suffix}`;
},
remarkPlugins: [
'remark-math',
'remark-gfm',
remarkDefinitionList,
checkRelativeLinks,
remarkMojikumi,
// the following order matters!
remarkMermaid,
remarkShiki,
setHeadingLevels,
addHeadingForCard,
],
remarkRehypeOptions: {
handlers: {
emphasis(h, node) {
return h(node, 'i', m2hAll(h, node));
},
delete(h, node) {
return h(node, 's', m2hAll(h, node));
},
heading(h, node) {
return h(node, `h${node.depth}`, [h(node, 'span', m2hAll(h, node))]);
},
...remarkRehypeMojikumi,
...defListHastHandlers,
},
},
rehypePlugins: [
breakLongCode,
wrapTableOverflowAuto,
'rehype-plugin-image-native-lazy-loading',
'rehype-katex',
],
},
ssg: {
sitemap: false,
async onSiteRendered({ pages }) {
await Promise.all([
generateFonts(pages),
generateSitemap(pages),
]);
},
},
});