@@ -5,7 +5,12 @@ import { ResourceType } from "~generated/generatedEnums"
55import * as dotenv from "dotenv"
66import { Client } from "pg"
77
8- import type { Resource , SitemapEntry } from "./types"
8+ import type { PageOnlySitemapEntry , Resource , SitemapEntry } from "./types"
9+ import {
10+ FOLDER_RESOURCE_TYPES ,
11+ PAGE_RESOURCE_TYPES ,
12+ PageResourceType ,
13+ } from "./constants"
914import {
1015 GET_ALL_RESOURCES_WITH_FULL_PERMALINKS ,
1116 GET_CONFIG ,
@@ -33,14 +38,6 @@ const SITE_ID = Number(process.env.SITE_ID)
3338const DANGLING_DIRECTORY_PAGE_ID = "-1"
3439const INDEX_PAGE_PERMALINK = "_index"
3540const META_PERMALINK = "_meta"
36- const PAGE_RESOURCE_TYPES = [
37- "Page" ,
38- "CollectionPage" ,
39- "CollectionLink" ,
40- "IndexPage" ,
41- "RootPage" ,
42- ]
43- const FOLDER_RESOURCE_TYPES = [ "Folder" , "Collection" ]
4441
4542const getConvertedPermalink = ( fullPermalink : string ) => {
4643 // NOTE: If the full permalink ends with `_index`,
@@ -90,7 +87,7 @@ async function main() {
9087 const resources = await getAllResourcesWithFullPermalinks ( client )
9188
9289 // Construct an array of sitemap entries
93- const sitemapEntries : SitemapEntry [ ] = [ ]
90+ const sitemapEntries : PageOnlySitemapEntry [ ] = [ ]
9491
9592 // Process each resource
9693 for ( const resource of resources ) {
@@ -99,7 +96,10 @@ async function main() {
9996 )
10097
10198 // Ensure the resource is a page (we don't need to write folders)
102- if ( PAGE_RESOURCE_TYPES . includes ( resource . type ) && resource . content ) {
99+ if (
100+ PAGE_RESOURCE_TYPES . find ( ( t ) => t === resource . type ) &&
101+ resource . content
102+ ) {
103103 // Inject page type and title into content before writing to file
104104 resource . content . page = {
105105 ...resource . content . page ,
@@ -112,6 +112,9 @@ async function main() {
112112 resource . parentId ,
113113 )
114114
115+ // NOTE: We remap the ID for _index pages to be the ID of the folder,
116+ // as both will have the same permalink and the folder is recognized as
117+ // the parent of all the children resources
115118 const idOfFolder = resources . find (
116119 ( item ) =>
117120 resource . fullPermalink . endsWith ( INDEX_PAGE_PERMALINK ) &&
@@ -120,9 +123,9 @@ async function main() {
120123 getConvertedPermalink ( resource . fullPermalink ) ,
121124 ) ?. id
122125
123- const sitemapEntry : SitemapEntry = {
126+ const sitemapEntry : PageOnlySitemapEntry = {
124127 id : idOfFolder ?? resource . id ,
125- type : resource . type ,
128+ type : resource . type as PageResourceType ,
126129 title : resource . title ,
127130 permalink : `/${ getConvertedPermalink ( resource . fullPermalink ) } ` ,
128131 lastModified : resource . updatedAt . toISOString ( ) ,
@@ -197,7 +200,7 @@ async function main() {
197200
198201function generateSitemapTree (
199202 resources : Resource [ ] ,
200- sitemapEntries : SitemapEntry [ ] ,
203+ sitemapEntries : PageOnlySitemapEntry [ ] ,
201204 pathPrefix : string ,
202205) : SitemapEntry [ ] | undefined {
203206 const pathPrefixWithoutLeadingSlash = pathPrefix . slice ( 1 )
@@ -246,7 +249,13 @@ function generateSitemapTree(
246249 )
247250 . map ( ( danglingDirectory ) => {
248251 const pageName = danglingDirectory . replace ( / - / g, " " )
249- const title = pageName . charAt ( 0 ) . toUpperCase ( ) + pageName . slice ( 1 )
252+ const generatedTitle =
253+ pageName . charAt ( 0 ) . toUpperCase ( ) + pageName . slice ( 1 )
254+ const folderResourceTitle = resources . find (
255+ ( resource ) => resource . fullPermalink === danglingDirectory ,
256+ ) ?. title
257+ const title = folderResourceTitle ?? generatedTitle
258+
250259 logDebug (
251260 `Creating index page for dangling directory: ${ danglingDirectory } ` ,
252261 )
@@ -263,7 +272,7 @@ function generateSitemapTree(
263272 ( pathPrefixWithoutLeadingSlash . length === 0
264273 ? danglingDirectory
265274 : `${ pathPrefixWithoutLeadingSlash } /${ danglingDirectory } ` ) &&
266- FOLDER_RESOURCE_TYPES . includes ( resource . type ) ,
275+ FOLDER_RESOURCE_TYPES . find ( ( t ) => t === resource . type ) ,
267276 )
268277
269278 return {
@@ -340,7 +349,7 @@ function getFoldersAndCollections(
340349 resources . some (
341350 ( resource ) =>
342351 resource . id === child . id &&
343- FOLDER_RESOURCE_TYPES . includes ( resource . type ) ,
352+ FOLDER_RESOURCE_TYPES . find ( ( t ) => t === resource . type ) ,
344353 ) ,
345354 )
346355
0 commit comments