1
1
import { ensureDir } from 'fs'
2
- import { replaceBody , generatePage } from '../util/utils.js'
2
+ import { generatePage , replaceBody } from '../util/utils.js'
3
3
import { templateBox , templateProcess } from '../util/template.js'
4
4
import { convertToUSA , handleUTC } from '../util/utils.js'
5
5
6
-
7
6
export const pagesPlugin = {
8
7
name : 'pages' ,
9
8
/**@param {import("./core.js").Core } core */
@@ -20,65 +19,79 @@ export const pagesPlugin = {
20
19
meta ,
21
20
( item ) => new Date ( item . date ) . getFullYear ( ) ,
22
21
)
23
- await generatePage ( { group : groupArchive , basePath : 'archive' , dist, header, footer, version, author } )
22
+ await generatePage ( {
23
+ group : groupArchive ,
24
+ basePath : 'archive' ,
25
+ dist,
26
+ header,
27
+ footer,
28
+ version,
29
+ author,
30
+ } )
24
31
25
32
// Handle the tags
26
33
const groupTags = Object . groupBy (
27
34
meta . flatMap ( ( item ) => item . tags . map ( ( tag ) => ( { tag, ...item } ) ) ) ,
28
35
( item ) => item . tag ,
29
36
)
30
- await generatePage ( { group : groupTags , basePath : 'tags' , dist, header, footer, version, author } )
37
+ await generatePage ( {
38
+ group : groupTags ,
39
+ basePath : 'tags' ,
40
+ dist,
41
+ header,
42
+ footer,
43
+ version,
44
+ author,
45
+ } )
31
46
32
47
// Handle the home
33
- const homeDest = new URL ( './home/' , dist )
34
- let indexPage = await Deno . readTextFile ( new URL ( '../index.html' , src ) )
35
- indexPage = replaceBody ( indexPage , header , footer , version )
36
-
37
- const mLength = meta . length
38
- const lastPage = Math . ceil ( mLength / 8 )
39
- let content = ''
48
+ const POST_PER_PAGE = 8
49
+ const groupMetaData = Array . from (
50
+ { length : Math . ceil ( meta . length / POST_PER_PAGE ) } ,
51
+ ( _ , index ) =>
52
+ meta . slice ( index * POST_PER_PAGE , ( index + 1 ) * POST_PER_PAGE ) ,
53
+ )
54
+ const totalPage = groupMetaData . length
40
55
41
- for ( let index = 0 ; index < mLength ; index ++ ) {
42
- const { date , title , summary , tags } = meta [ index ]
43
- const aTags = tags . reduce (
56
+ /** @param { string[] } */
57
+ const generateTags = ( /** @type { string[] } */ tags ) =>
58
+ tags . reduce (
44
59
( acc , tag ) =>
45
60
acc +
46
61
`<a class="tag" href="/./tags/${ tag } /"><i class="fa-solid fa-tag"></i> ${ tag } </a>` ,
47
62
'' ,
48
63
)
49
- content += templateBox ( {
50
- place : `/./posts/${ handleUTC ( date ) } /` ,
51
- title,
52
- summary,
53
- time : convertToUSA ( date ) ,
54
- tags : aTags ,
55
- } )
56
-
57
- if ( ( index + 1 ) % 8 === 0 || index + 1 === mLength ) {
58
- const cur = index + 1 === mLength
59
- ? lastPage
60
- : Math . floor ( ( index + 1 ) / 8 )
61
- const process = templateProcess ( {
62
- before : cur > 2 ? `/./home/${ cur - 1 } /` : '/' ,
63
- page : `${ cur } / ${ lastPage } ` ,
64
- after : index === mLength ? '#' : `/./home/${ cur + 1 } /` ,
64
+ const generateBox = (
65
+ /**@type {import("../util/type.js").MetaData[] } */ meta ,
66
+ ) => {
67
+ return meta . map ( ( { date, title, summary, tags } ) => {
68
+ return templateBox ( {
69
+ place : `/./posts/${ handleUTC ( date ) } /` ,
70
+ title,
71
+ summary,
72
+ time : convertToUSA ( date ) ,
73
+ tags : generateTags ( tags ) ,
65
74
} )
66
- const home = indexPage . replace (
67
- '<!-- Template -->' ,
68
- content + process ,
69
- )
70
-
71
- // Reset the content
72
- content = ''
75
+ } )
76
+ }
73
77
74
- // Generate the home dir
75
- if ( cur !== 1 ) await ensureDir ( new URL ( `${ cur } /` , homeDest ) )
76
- const url = cur === 1
77
- ? new URL ( './index.html' , dist )
78
- : new URL ( `${ cur } /index.html` , homeDest )
78
+ const indexPage = replaceBody ( header , footer , version , src )
79
+ for ( const [ index , metaData ] of groupMetaData . entries ( ) ) {
80
+ const content = generateBox ( metaData ) . join ( '' )
81
+ const process = templateProcess ( {
82
+ before : index <= 1 ? '/' : `/./home/${ index } /` ,
83
+ page : `${ index + 1 } / ${ totalPage } ` ,
84
+ after : index === totalPage - 1 ? '#' : `/./home/${ index + 2 } /` ,
85
+ } )
86
+ const home = indexPage . replace ( '<!-- Template -->' , content + process )
87
+ const url = index === 0
88
+ ? new URL ( './index.html' , dist )
89
+ : new URL ( `./home/${ index + 1 } /index.html` , dist )
79
90
80
- await Deno . writeTextFile ( url , home )
91
+ if ( index !== 0 ) {
92
+ await ensureDir ( new URL ( `./home/${ index + 1 } /` , dist ) )
81
93
}
94
+ await Deno . writeTextFile ( url , home )
82
95
}
83
96
} ,
84
97
)
0 commit comments