forked from metrue/astro-air-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
astro.config.mjs
176 lines (158 loc) · 5.38 KB
/
astro.config.mjs
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
import { defineConfig } from 'astro/config';
import { visit } from 'unist-util-visit'
import md5 from 'md5';
import sitemap from '@astrojs/sitemap';
import { SITE_URL } from './src/consts';
function pipeline() {
return [
() => (tree) => {
visit(tree, 'element', (node, index) => {
if (node.tagName === 'p' && node.children[0].tagName === 'img') {
node.tagName = 'figure';
let img = node.children[0];
let sign = md5(img.properties.src);
let data = img.properties.alt.split("|");
let alt = data[0];
let size = "big";
if (data.length > 1) {
size = data[1];
}
let classes = ['image component image-fullbleed body-copy-wide nr-scroll-animation nr-scroll-animation--on'];
classes.push(`image-${size}`);
node.properties.className = classes;
node.children = [
{
type: 'element',
tagName: 'div',
properties: { className: ['component-content'] },
children: [
{
type: 'element',
tagName: 'div',
properties: { className: ['image-sharesheet'] },
children: [
{
type: 'element',
tagName: 'div',
properties: { className: [`image image-load image-asset image-${sign}`], id: `lht${sign}` },
children: [
{
type: 'element',
tagName: 'picture',
properties: { className: ['picture'] },
children: [
{
type: 'element',
tagName: 'img',
properties: {
'data-src': img.properties.src,
alt: alt,
className: ['picture-image'],
}
}
]
}
]
}
]
},
{
type: 'element',
tagName: 'div',
properties: { className: ['image-description'] },
children: [
{
type: 'element',
tagName: 'div',
properties: { className: ['image-caption'] },
children: [
{
type: 'text',
value: alt
}
]
}
]
}
]
}
]
}
})
},
() => (tree) => {
tree.children.forEach((node) => {
if (node.type === "raw") {
node.value = `<div class="pagebody code component"><div class="component-content code"> ${node.value} </div></div>`
// node.value = node.value.replace(/astro-code/g, 'astro-code')
}
});
},
() => (tree) => {
for (let i = 0; i < tree.children.length; i++) {
let node = tree.children[i];
if (node.type === "element" && ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', "ul", "ol","blockquote"].includes(node.tagName)) {
let next = tree.children[i + 1];
let nodes = [node];
while (next && !['figure'].includes(next.tagName) && next.type != "raw") {
nodes.push(next);
next = tree.children[tree.children.indexOf(next) + 1];
}
if (nodes.length > 1) {
// rename label
nodes.forEach((node) => {
if (node.tagName === "p") {
node.properties.className = ['pagebody-copy'];
node.tagName = "div";
}
if (['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(node.tagName)) {
node.properties.className = ['pagebody-header'];
}
});
tree.children.splice(i, nodes.length, {
type: 'element',
tagName: 'div',
properties: { className: ['pagebody text component'] },
children: [
{
type: 'element',
tagName: 'div',
properties: { className: ['component-content'] },
children: nodes
}
]
});
}
}
}
},
() => (tree) => {
let len = tree.children.length;
for (let index = 0; index < len; index++) {
let node = tree.children[index];
if (node.type === "element" && node.tagName === "figure") {
tree.children.splice(index, 0, {
type: 'element',
tagName: 'div',
properties: { className: ['tertiarynav component'] },
children: [{
type: 'element',
tagName: 'div',
properties: { className: ['component-content'] },
}]
})
index++;
}
}
}
]
}
// https://astro.build/config
export default defineConfig({
site: SITE_URL,
markdown: {
rehypePlugins: pipeline(),
syntaxHighlight: 'prism',
},
integrations: [sitemap()],
});