Skip to content

Commit 22a8d2c

Browse files
committed
fix some async code
1 parent 066d5d1 commit 22a8d2c

File tree

7 files changed

+47
-75
lines changed

7 files changed

+47
-75
lines changed

main.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ async function createConfig() {
2424
new URL('./util/footer.html', baseConfig.src),
2525
)
2626

27-
return {
28-
...baseConfig,
29-
header,
30-
footer,
31-
}
27+
return { ...baseConfig, header, footer }
3228
}
3329

3430
const config = await createConfig()

src/plugins/asserts.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ export const assertPlugin = {
1818
const __dist_p = new URL(`./public/${entry.name}`, dist)
1919
if (entry.name === 'JavaScript') {
2020
await ensureFile(
21-
new URL(`./${entry.name}/index.${config.version}.js`, __dist_p),
21+
new URL(`./${entry.name}/index.${version}.js`, __dist_p),
2222
)
23-
await copy(
23+
copy(
2424
new URL(`./${entry.name}/index.js`, __src_p),
2525
new URL(`./${entry.name}/index.${version}.js`, __dist_p),
2626
{ overwrite: true },
2727
)
2828
continue
2929
}
30-
await copy(__src_p, __dist_p, { overwrite: true })
30+
copy(__src_p, __dist_p, { overwrite: true })
3131
}
3232

3333
// compile the css
@@ -39,7 +39,7 @@ export const assertPlugin = {
3939
const path = new URL(`../public/css/${entry.name}`, src)
4040
const code = await compileCss(path)
4141
const fileName = entry.name.split('.').join(`.${config.version}.`)
42-
await Deno.writeFile(new URL(fileName, __css_d), code)
42+
Deno.writeFile(new URL(fileName, __css_d), code)
4343
}
4444

4545
// about me
@@ -51,7 +51,7 @@ export const assertPlugin = {
5151
{ version },
5252
await Deno.readTextFile(__src_about),
5353
)
54-
await Deno.writeTextFile(__dist_about, about)
54+
Deno.writeTextFile(__dist_about, about)
5555
},
5656
)
5757
},

src/plugins/feed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const feedPlugin = {
5353
[__robots_dist, robotsContent],
5454
].forEach(async ([path, content]) => {
5555
if (!await exists(path)) await ensureFile(path)
56-
await Deno.writeTextFile(path, content)
56+
Deno.writeTextFile(path, content)
5757
})
5858
},
5959
)

src/plugins/pages.js

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { ensureDir } from 'fs'
2-
import { generatePage, replaceBody } from '../util/utils.js'
2+
import {
3+
convertToUSA,
4+
generatePage,
5+
generateTags,
6+
handleUTC,
7+
replaceBody,
8+
} from '../util/utils.js'
39
import { templateBox, templateProcess } from '../util/template.js'
4-
import { convertToUSA, handleUTC } from '../util/utils.js'
510

611
export const pagesPlugin = {
712
name: 'pages',
@@ -12,37 +17,24 @@ export const pagesPlugin = {
1217
core.addhook(
1318
'afterBuild',
1419
async (/**@type {import("../util/type.js").Config} */ config) => {
15-
const { dist, version, author, header, footer, src } = config
20+
const { dist, version, header, footer, src } = config
1621

1722
// Handle the archive
1823
const groupArchive = Object.groupBy(
1924
meta,
2025
(item) => new Date(item.date).getFullYear(),
2126
)
22-
await generatePage({
23-
group: groupArchive,
24-
basePath: 'archive',
25-
dist,
26-
header,
27-
footer,
28-
version,
29-
author,
30-
})
3127

3228
// Handle the tags
3329
const groupTags = Object.groupBy(
3430
meta.flatMap((item) => item.tags.map((tag) => ({ tag, ...item }))),
3531
(item) => item.tag,
3632
)
37-
await generatePage({
38-
group: groupTags,
39-
basePath: 'tags',
40-
dist,
41-
header,
42-
footer,
43-
version,
44-
author,
45-
})
33+
34+
Promise.all([
35+
generatePage({ group: groupArchive, basePath: 'archive', ...config }),
36+
generatePage({ group: groupTags, basePath: 'tags', ...config }),
37+
])
4638

4739
// Handle the home
4840
const POST_PER_PAGE = 8
@@ -53,27 +45,17 @@ export const pagesPlugin = {
5345
)
5446
const totalPage = groupMetaData.length
5547

56-
/**@param {string[]} */
57-
const generateTags = (/**@type {string[]} */ tags) =>
58-
tags.reduce(
59-
(acc, tag) =>
60-
acc +
61-
`<a class="tag" href="/./tags/${tag}/"><i class="fa-solid fa-tag"></i> ${tag}</a>`,
62-
'',
63-
)
6448
const generateBox = (
6549
/**@type {import("../util/type.js").MetaData[]} */ meta,
66-
) => {
67-
return meta.map(({ date, title, summary, tags }) => {
68-
return templateBox({
50+
) =>
51+
meta.map(({ date, tags, ...args }) =>
52+
templateBox({
6953
place: `/./posts/${handleUTC(date)}/`,
70-
title,
71-
summary,
7254
time: convertToUSA(date),
7355
tags: generateTags(tags),
56+
...args,
7457
})
75-
})
76-
}
58+
)
7759

7860
const indexPage = replaceBody(header, footer, version, src)
7961
for (const [index, metaData] of groupMetaData.entries()) {
@@ -91,7 +73,7 @@ export const pagesPlugin = {
9173
if (index !== 0) {
9274
await ensureDir(new URL(`./home/${index + 1}/`, dist))
9375
}
94-
await Deno.writeTextFile(url, home)
76+
Deno.writeTextFile(url, home)
9577
}
9678
},
9779
)

src/plugins/posts.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ensureFile, exists } from 'fs'
66

77
export const postPlugin = {
88
name: 'post',
9+
/**@type {import("../util/type.js").MetaData[]} */
910
metaData: [],
1011
/**@param {import("./core.js").Core} core */
1112
apply(core) {
@@ -34,15 +35,11 @@ export const postPlugin = {
3435
)
3536
if (!await exists(postDist)) await ensureFile(postDist)
3637

37-
const head = await replaceHead({
38-
keywords: tags.join(', '),
39-
description: summary,
40-
title,
41-
version,
42-
})
38+
const keywords = tags.join(', ')
39+
const head = await replaceHead({ keywords, summary, title, version })
4340
const content = templateArticle({
44-
title,
4541
content: await markdown(md),
42+
title,
4643
giscus,
4744
})
4845
const post = `${head}${header}${content}${footer}`

src/util/type.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@
3939
* @property {string} [Yaml.date]
4040
*/
4141

42-
/**
43-
* @typedef {Object} HeadMetaData
44-
* @property {string} [keywords]
45-
* @property {string} [description]
46-
* @property {string} [title]
47-
* @property {string} [version]
48-
*/
49-
5042
/**
5143
* @typedef {Object} GeneratePageOptions
5244
* @property {Record<string, import("../plugins/core.js").MetaData>} group

src/util/utils.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ export const convertToUSA = (date) => {
3131
}
3232

3333
/**
34-
* @param {HeadMetaData} metaData
34+
* @param {import("../util/type.js").MetaData} metaData
3535
* @param {string} [content]
3636
*/
3737
export const replaceHead = async (metaData, content) => {
3838
const res = content ??
3939
await Deno.readTextFile(new URL('head.html', import.meta.url))
40-
const { keywords, description, title, version } = metaData
40+
const { keywords, summary, title, version } = metaData
4141

4242
return res
4343
.replace('<!-- keywords -->', keywords)
44-
.replace('<!-- description -->', description)
44+
.replace('<!-- description -->', summary)
4545
.replace('<!-- title -->', title)
4646
.replace('<!-- base.css -->', `/public/css/base.${version}.css`)
4747
.replace('<!-- index.css -->', `/public/css/index.${version}.css`)
@@ -66,29 +66,34 @@ export const replaceBody = (header, footer, version, src) => {
6666
}
6767

6868
/**
69-
* @param {import("./type.js").GeneratePageOptions}
69+
* @param {string[]} tags
70+
* @param {string} basePath
7071
*/
72+
export const generateTags = (/**@type {string[]} */ tags, basePath = 'tags') =>
73+
tags.reduce(
74+
(acc, tag) =>
75+
acc +
76+
`<a class="tag" href="/./${basePath}/${tag}/"><i class="fa-solid fa-tag"></i> ${tag}</a>`,
77+
'',
78+
)
79+
80+
/**@param {import("./type.js").GeneratePageOptions}*/
7181
export async function generatePage(
7282
{ group, basePath, dist, header, footer, version, author },
7383
) {
7484
const url = new URL(`./${basePath}/index.html`, dist)
7585
const keys = Object.keys(group)
7686
const head = await replaceHead({
7787
keywords: keys.join(', '),
78-
description: `${author} ~ ${basePath}`,
88+
summary: `${author} ~ ${basePath}`,
7989
title: `${author} ~ ${basePath}`,
8090
version,
8191
})
8292

8393
// a tags
8494
const body = templateArticle({
8595
title: basePath,
86-
content: keys.reduce(
87-
(acc, tag) =>
88-
acc +
89-
`<a class="tag" href="/./${basePath}/${tag}/"><i class="fa-solid fa-tag"></i> ${tag}</a>`,
90-
'',
91-
),
96+
content: generateTags(keys, basePath),
9297
})
9398
const article = `${head}${header}${body}${footer}`
9499
if (!await exists(url)) await ensureFile(url)
@@ -98,7 +103,7 @@ export async function generatePage(
98103
const itemUrl = new URL(`./${basePath}/${key}/index.html`, dist)
99104
const itemHead = await replaceHead({
100105
keywords: [...new Set(items.flatMap((item) => item.tags))],
101-
description: `${author} ~ ${key}`,
106+
summary: `${author} ~ ${key}`,
102107
title: `${author} ~ ${key}`,
103108
version,
104109
})

0 commit comments

Comments
 (0)