@@ -7,12 +7,12 @@ import { ProcessedContent, QuartzPluginData, defaultProcessedContent } from "../
77import { FullPageLayout } from "../../cfg"
88import path from "path"
99import {
10- FullSlug ,
11- SimpleSlug ,
12- stripSlashes ,
13- joinSegments ,
14- pathToRoot ,
15- simplifySlug ,
10+ FullSlug ,
11+ SimpleSlug ,
12+ stripSlashes ,
13+ joinSegments ,
14+ pathToRoot ,
15+ simplifySlug ,
1616} from "../../util/path"
1717import { defaultListPageLayout , sharedPageComponents } from "../../../quartz.layout"
1818import { FolderContent } from "../../components"
@@ -21,150 +21,151 @@ import { i18n, TRANSLATIONS } from "../../i18n"
2121import { BuildCtx } from "../../util/ctx"
2222import { StaticResources } from "../../util/resources"
2323interface FolderPageOptions extends FullPageLayout {
24- sort ?: ( f1 : QuartzPluginData , f2 : QuartzPluginData ) => number
24+ sort ?: ( f1 : QuartzPluginData , f2 : QuartzPluginData ) => number
2525}
2626
2727async function * processFolderInfo (
28- ctx : BuildCtx ,
29- folderInfo : Record < SimpleSlug , ProcessedContent > ,
30- allFiles : QuartzPluginData [ ] ,
31- opts : FullPageLayout ,
32- resources : StaticResources ,
28+ ctx : BuildCtx ,
29+ folderInfo : Record < SimpleSlug , ProcessedContent > ,
30+ allFiles : QuartzPluginData [ ] ,
31+ opts : FullPageLayout ,
32+ resources : StaticResources ,
3333) {
34- for ( const [ folder , folderContent ] of Object . entries ( folderInfo ) as [
35- SimpleSlug ,
36- ProcessedContent ,
37- ] [ ] ) {
38- const slug = joinSegments ( folder , "index" ) as FullSlug
39- const [ tree , file ] = folderContent
40- const cfg = ctx . cfg . configuration
41- const externalResources = pageResources ( pathToRoot ( slug ) , resources )
42- const componentData : QuartzComponentProps = {
43- ctx,
44- fileData : file . data ,
45- externalResources,
46- cfg,
47- children : [ ] ,
48- tree,
49- allFiles,
50- }
34+ for ( const [ folder , folderContent ] of Object . entries ( folderInfo ) as [
35+ SimpleSlug ,
36+ ProcessedContent ,
37+ ] [ ] ) {
38+ const slug = joinSegments ( folder , "index" ) as FullSlug
39+ const [ tree , file ] = folderContent
40+ const cfg = ctx . cfg . configuration
41+ const externalResources = pageResources ( pathToRoot ( slug ) , resources )
42+ const componentData : QuartzComponentProps = {
43+ ctx,
44+ fileData : file . data ,
45+ externalResources,
46+ cfg,
47+ children : [ ] ,
48+ tree,
49+ allFiles,
50+ }
5151
52- const content = renderPage ( cfg , slug , componentData , opts , externalResources )
53- yield write ( {
54- ctx,
55- content,
56- slug,
57- ext : ".html" ,
58- } )
59- }
52+ const content = renderPage ( cfg , slug , componentData , opts , externalResources )
53+ yield write ( {
54+ ctx,
55+ content,
56+ slug,
57+ ext : ".html" ,
58+ } )
59+ }
6060}
6161
6262function computeFolderInfo (
63- folders : Set < SimpleSlug > ,
64- content : ProcessedContent [ ] ,
65- locale : keyof typeof TRANSLATIONS ,
63+ folders : Set < SimpleSlug > ,
64+ content : ProcessedContent [ ] ,
65+ locale : keyof typeof TRANSLATIONS ,
6666) : Record < SimpleSlug , ProcessedContent > {
67- // Create default folder descriptions
68- const folderInfo : Record < SimpleSlug , ProcessedContent > = Object . fromEntries (
69- [ ...folders ] . map ( ( folder ) => [
70- folder ,
71- defaultProcessedContent ( {
72- slug : joinSegments ( folder , "index" ) as FullSlug ,
73- frontmatter : {
74- title : `${ i18n ( locale ) . pages . folderContent . folder } : ${ folder } ` ,
75- tags : [ ] ,
76- } ,
77- } ) ,
78- ] ) ,
79- )
67+ // Create default folder descriptions
68+ const folderInfo : Record < SimpleSlug , ProcessedContent > = Object . fromEntries (
69+ [ ...folders ] . map ( ( folder ) => [
70+ folder ,
71+ defaultProcessedContent ( {
72+ slug : joinSegments ( folder , "index" ) as FullSlug ,
73+ frontmatter : {
74+ title : `${ i18n ( locale ) . pages . folderContent . folder } : ${ folder } ` ,
75+ tags : [ ] ,
76+ comments : false ,
77+ } ,
78+ } ) ,
79+ ] ) ,
80+ )
8081
81- // Update with actual content if available
82- for ( const [ tree , file ] of content ) {
83- const slug = stripSlashes ( simplifySlug ( file . data . slug ! ) ) as SimpleSlug
84- if ( folders . has ( slug ) ) {
85- folderInfo [ slug ] = [ tree , file ]
82+ // Update with actual content if available
83+ for ( const [ tree , file ] of content ) {
84+ const slug = stripSlashes ( simplifySlug ( file . data . slug ! ) ) as SimpleSlug
85+ if ( folders . has ( slug ) ) {
86+ folderInfo [ slug ] = [ tree , file ]
87+ }
8688 }
87- }
8889
89- return folderInfo
90+ return folderInfo
9091}
9192
9293function _getFolders ( slug : FullSlug ) : SimpleSlug [ ] {
93- var folderName = path . dirname ( slug ?? "" ) as SimpleSlug
94- const parentFolderNames = [ folderName ]
94+ var folderName = path . dirname ( slug ?? "" ) as SimpleSlug
95+ const parentFolderNames = [ folderName ]
9596
96- while ( folderName !== "." ) {
97- folderName = path . dirname ( folderName ?? "" ) as SimpleSlug
98- parentFolderNames . push ( folderName )
99- }
100- return parentFolderNames
97+ while ( folderName !== "." ) {
98+ folderName = path . dirname ( folderName ?? "" ) as SimpleSlug
99+ parentFolderNames . push ( folderName )
100+ }
101+ return parentFolderNames
101102}
102103
103104export const FolderPage : QuartzEmitterPlugin < Partial < FolderPageOptions > > = ( userOpts ) => {
104- const opts : FullPageLayout = {
105- ...sharedPageComponents ,
106- ...defaultListPageLayout ,
107- pageBody : FolderContent ( { sort : userOpts ?. sort } ) ,
108- ...userOpts ,
109- }
105+ const opts : FullPageLayout = {
106+ ...sharedPageComponents ,
107+ ...defaultListPageLayout ,
108+ pageBody : FolderContent ( { sort : userOpts ?. sort } ) ,
109+ ...userOpts ,
110+ }
110111
111- const { head : Head , header, beforeBody, pageBody, afterBody, left, right, footer : Footer } = opts
112- const Header = HeaderConstructor ( )
113- const Body = BodyConstructor ( )
112+ const { head : Head , header, beforeBody, pageBody, afterBody, left, right, footer : Footer } = opts
113+ const Header = HeaderConstructor ( )
114+ const Body = BodyConstructor ( )
114115
115- return {
116- name : "FolderPage" ,
117- getQuartzComponents ( ) {
118- return [
119- Head ,
120- Header ,
121- Body ,
122- ...header ,
123- ...beforeBody ,
124- pageBody ,
125- ...afterBody ,
126- ...left ,
127- ...right ,
128- Footer ,
129- ]
130- } ,
131- async * emit ( ctx , content , resources ) {
132- const allFiles = content . map ( ( c ) => c [ 1 ] . data )
133- const cfg = ctx . cfg . configuration
116+ return {
117+ name : "FolderPage" ,
118+ getQuartzComponents ( ) {
119+ return [
120+ Head ,
121+ Header ,
122+ Body ,
123+ ...header ,
124+ ...beforeBody ,
125+ pageBody ,
126+ ...afterBody ,
127+ ...left ,
128+ ...right ,
129+ Footer ,
130+ ]
131+ } ,
132+ async * emit ( ctx , content , resources ) {
133+ const allFiles = content . map ( ( c ) => c [ 1 ] . data )
134+ const cfg = ctx . cfg . configuration
134135
135- const folders : Set < SimpleSlug > = new Set (
136- allFiles . flatMap ( ( data ) => {
137- return data . slug
138- ? _getFolders ( data . slug ) . filter (
139- ( folderName ) => folderName !== "." && folderName !== "tags" ,
140- )
141- : [ ]
142- } ) ,
143- )
136+ const folders : Set < SimpleSlug > = new Set (
137+ allFiles . flatMap ( ( data ) => {
138+ return data . slug
139+ ? _getFolders ( data . slug ) . filter (
140+ ( folderName ) => folderName !== "." && folderName !== "tags" ,
141+ )
142+ : [ ]
143+ } ) ,
144+ )
144145
145- const folderInfo = computeFolderInfo ( folders , content , cfg . locale )
146- yield * processFolderInfo ( ctx , folderInfo , allFiles , opts , resources )
147- } ,
148- async * partialEmit ( ctx , content , resources , changeEvents ) {
149- const allFiles = content . map ( ( c ) => c [ 1 ] . data )
150- const cfg = ctx . cfg . configuration
146+ const folderInfo = computeFolderInfo ( folders , content , cfg . locale )
147+ yield * processFolderInfo ( ctx , folderInfo , allFiles , opts , resources )
148+ } ,
149+ async * partialEmit ( ctx , content , resources , changeEvents ) {
150+ const allFiles = content . map ( ( c ) => c [ 1 ] . data )
151+ const cfg = ctx . cfg . configuration
151152
152- // Find all folders that need to be updated based on changed files
153- const affectedFolders : Set < SimpleSlug > = new Set ( )
154- for ( const changeEvent of changeEvents ) {
155- if ( ! changeEvent . file ) continue
156- const slug = changeEvent . file . data . slug !
157- const folders = _getFolders ( slug ) . filter (
158- ( folderName ) => folderName !== "." && folderName !== "tags" ,
159- )
160- folders . forEach ( ( folder ) => affectedFolders . add ( folder ) )
161- }
153+ // Find all folders that need to be updated based on changed files
154+ const affectedFolders : Set < SimpleSlug > = new Set ( )
155+ for ( const changeEvent of changeEvents ) {
156+ if ( ! changeEvent . file ) continue
157+ const slug = changeEvent . file . data . slug !
158+ const folders = _getFolders ( slug ) . filter (
159+ ( folderName ) => folderName !== "." && folderName !== "tags" ,
160+ )
161+ folders . forEach ( ( folder ) => affectedFolders . add ( folder ) )
162+ }
162163
163- // If there are affected folders, rebuild their pages
164- if ( affectedFolders . size > 0 ) {
165- const folderInfo = computeFolderInfo ( affectedFolders , content , cfg . locale )
166- yield * processFolderInfo ( ctx , folderInfo , allFiles , opts , resources )
167- }
168- } ,
169- }
164+ // If there are affected folders, rebuild their pages
165+ if ( affectedFolders . size > 0 ) {
166+ const folderInfo = computeFolderInfo ( affectedFolders , content , cfg . locale )
167+ yield * processFolderInfo ( ctx , folderInfo , allFiles , opts , resources )
168+ }
169+ } ,
170+ }
170171}
0 commit comments