Skip to content

Commit 820a7c0

Browse files
dcshzjseaerchin
andauthored
fix: allow custom titles in index pages (#1002)
* fix: allow custom titles in index pages * chore: add tyeps --------- Co-authored-by: seaerchin <[email protected]>
1 parent 58cd646 commit 820a7c0

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const PAGE_RESOURCE_TYPES = [
2+
"Page",
3+
"CollectionPage",
4+
"CollectionLink",
5+
"IndexPage",
6+
"RootPage",
7+
] as const
8+
export type PageResourceType = (typeof PAGE_RESOURCE_TYPES)[number]
9+
10+
export const FOLDER_RESOURCE_TYPES = ["Folder", "Collection"] as const
11+
export type FolderResourceType = (typeof FOLDER_RESOURCE_TYPES)[number]

tooling/build/scripts/publishing/index.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { ResourceType } from "~generated/generatedEnums"
55
import * as dotenv from "dotenv"
66
import { 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"
914
import {
1015
GET_ALL_RESOURCES_WITH_FULL_PERMALINKS,
1116
GET_CONFIG,
@@ -33,14 +38,6 @@ const SITE_ID = Number(process.env.SITE_ID)
3338
const DANGLING_DIRECTORY_PAGE_ID = "-1"
3439
const INDEX_PAGE_PERMALINK = "_index"
3540
const 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

4542
const 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

198201
function 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

tooling/build/scripts/publishing/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Resource as DbResource } from "~generated/selectableTypes"
22

3+
import { PAGE_RESOURCE_TYPES } from "./constants"
4+
35
// NOTE: this needs the `omit` because the `parentId`
46
// we defined in studio
57
export interface Resource extends Omit<DbResource, "parentId"> {
@@ -30,3 +32,7 @@ export type SitemapEntry = Pick<
3032
children?: SitemapEntry[]
3133
tags?: Tag[]
3234
}
35+
36+
export type PageOnlySitemapEntry = Omit<SitemapEntry, "children"> & {
37+
type: (typeof PAGE_RESOURCE_TYPES)[number]
38+
}

0 commit comments

Comments
 (0)