-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
198 lines (163 loc) · 5.63 KB
/
build.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { pageGen } from '@/pagegen.tsx';
import { compile } from 'mdx2';
import { encode } from 'base64';
import rehypeHighlight from 'https://esm.sh/[email protected]';
import remarkFrontmatter from 'https://esm.sh/remark-frontmatter@4?bundle';
import { ensureDir, ensureFile } from 'https://deno.land/[email protected]/fs/mod.ts';
import gfm from 'https://esm.sh/[email protected]';
// 태그 파싱, 작업중
const metaTagParsing = (rawTexts: string[]) => {
let flag = false;
let tags: string[] = [];
let description = '';
rawTexts.map((text) => {
if (text === '---' || text.slice(0, 3) === '---') {
flag = !flag;
}
if (flag) {
const splitBlocks = text.split(':');
const keyword = splitBlocks[0];
if (keyword === 'tags') {
tags = splitBlocks[1]
.split('#')
.slice(1)
.map((a) => a.trim());
}
if (keyword === 'description') {
description = splitBlocks[1].trim();
}
}
});
return [tags, description];
};
const hardEnter = (rawTexts: string[]) => {
let isCodeBlock = false;
let alreadyCodeBlockDeclare = false;
return rawTexts
.map((text) => {
const reqForPasing = /```/;
if (reqForPasing.test(text)) {
if (!isCodeBlock) {
isCodeBlock = true;
const codeFileName = text.split(' ')[1];
// console.log(codeFileName);
let result = ``;
if (!alreadyCodeBlockDeclare) {
result +=
`import CopyCode from '@/islands/CopyCode.tsx';\n\n<CopyCode />\n\n${text}`;
alreadyCodeBlockDeclare = true;
} else result += `<CopyCode />\n\n${text}`;
return result;
}
if (isCodeBlock) isCodeBlock = false;
}
if (text.length === 0 && !isCodeBlock) return '\n';
else return `${text} `;
})
.join('\n');
};
//mdx파일을 읽어서
const removeExportCodeToComplied = (compiled: string) => {
return compiled
.split('\n')
.filter((line) => {
return line !== 'export default MDXContent;';
})
.join('\n');
};
interface newDB {
[key: string]: object;
}
export const buildMdx = async () => {
const baseDir = 'blog';
console.time('mdx build time ');
const dirs = ['posts', 'notes'];
await ensureFile(`./mdxIndex.json`);
let newDB: newDB = {};
const db = JSON.parse(await Deno.readTextFile('./mdxIndex.json'));
const fileNames: any = {
posts: [],
notes: [],
};
const promises = dirs.map(async (dir) => {
const path = `./${baseDir}/${dir}`;
for await (const dirEntry of Deno.readDir(path)) {
const readFileName = `${path}/${dirEntry.name}`;
const body = await Deno.readTextFile(readFileName);
const rawTexts = body.split('\n');
const [tags, description] = metaTagParsing(rawTexts);
console.log(tags, description);
const fileStat = await Deno.stat(`${path}/${dirEntry.name}`);
const hashedFileName = encode(dirEntry.name.split('.')[0]);
const forWriteFileName = `./routes/${dir}/${hashedFileName}.jsx`;
const fileInfo = {
title: dirEntry.name,
tags,
description,
path: forWriteFileName,
atime: fileStat.atime,
mtime: Date.parse(String(fileStat.mtime) || String(new Date())),
birthtime: fileStat.birthtime,
};
fileNames[dir].push(hashedFileName);
newDB = {
...newDB,
[dir]: { ...newDB[dir], [hashedFileName]: fileInfo },
};
// console.log(removeExportCodeToComplied(String(compiled.value)));
// await Deno.writeTextFile(`./routes/blog/posts/test.jsx`, page);
// console.log(evalFile.default);
// const encodeFileName = encode(dirEntry.name.split('.')[0]);
// const forWriteFileName = `./routes/blog/${dir}/${encodeFileName}.jsx`;
// console.log(hashFileName, string);
// console.log(Deno.statSync(`${path}/${dirEntry.name}`));
// console.log(dirEntry.name);
await ensureFile(forWriteFileName);
const existFileContent = await Deno.readFile(forWriteFileName);
// console.log(`./routes/blog/${dir}/${encodeFileName}.jsx`);
//mapping json 에 미리 값이 없기 문에 없다 라고 인식함
//즉 mdxindex파일이 만들어지기 전에 처음 렌더링이 돌때에 dir도 없고 hashedFileName도 없을것
// ㅇ
const existsFileEditedTime = db[dir]
? db[dir][hashedFileName]?.mtime || 0
: 0;
if (
String(existsFileEditedTime) !== String(fileInfo.mtime) ||
existFileContent.length === 0
) {
const enterBody = hardEnter(rawTexts);
const compiled = await compile(enterBody, {
jsxImportSource: 'preact',
remarkPlugins: [gfm, remarkFrontmatter],
rehypePlugins: [rehypeHighlight],
});
const page = pageGen(
removeExportCodeToComplied(String(compiled.value)),
);
await Deno.writeTextFile(forWriteFileName, page);
}
// // }
}
});
await Promise.all(promises);
const removeFilePromises = dirs.map(async (dir) => {
await ensureDir(`./routes/blog/${dir}`);
if (!db[dir] || !newDB[dir]) {
await Deno.remove(`./routes/blog/${dir}`, { recursive: true });
return;
}
Object.keys(db[dir]).map(async (key) => {
if (Object.keys(newDB[dir]).includes(key) === false) {
const name = db[dir][key].path;
await Deno.remove(name);
console.log(name);
}
});
});
await Deno.writeTextFile(`./mdxIndex.json`, JSON.stringify(newDB));
await Promise.all(removeFilePromises);
console.log('end');
console.timeEnd('mdx build time ');
};
// buildMdx();
const makeJSX = () => {};