Skip to content

Commit

Permalink
fix: allow custom titles in index pages (#1002)
Browse files Browse the repository at this point in the history
* fix: allow custom titles in index pages

* chore: add tyeps

---------

Co-authored-by: seaerchin <[email protected]>
  • Loading branch information
dcshzj and seaerchin authored Jan 21, 2025
1 parent 58cd646 commit 820a7c0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
11 changes: 11 additions & 0 deletions tooling/build/scripts/publishing/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const PAGE_RESOURCE_TYPES = [
"Page",
"CollectionPage",
"CollectionLink",
"IndexPage",
"RootPage",
] as const
export type PageResourceType = (typeof PAGE_RESOURCE_TYPES)[number]

export const FOLDER_RESOURCE_TYPES = ["Folder", "Collection"] as const
export type FolderResourceType = (typeof FOLDER_RESOURCE_TYPES)[number]
43 changes: 26 additions & 17 deletions tooling/build/scripts/publishing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { ResourceType } from "~generated/generatedEnums"
import * as dotenv from "dotenv"
import { Client } from "pg"

import type { Resource, SitemapEntry } from "./types"
import type { PageOnlySitemapEntry, Resource, SitemapEntry } from "./types"
import {
FOLDER_RESOURCE_TYPES,
PAGE_RESOURCE_TYPES,
PageResourceType,
} from "./constants"
import {
GET_ALL_RESOURCES_WITH_FULL_PERMALINKS,
GET_CONFIG,
Expand Down Expand Up @@ -33,14 +38,6 @@ const SITE_ID = Number(process.env.SITE_ID)
const DANGLING_DIRECTORY_PAGE_ID = "-1"
const INDEX_PAGE_PERMALINK = "_index"
const META_PERMALINK = "_meta"
const PAGE_RESOURCE_TYPES = [
"Page",
"CollectionPage",
"CollectionLink",
"IndexPage",
"RootPage",
]
const FOLDER_RESOURCE_TYPES = ["Folder", "Collection"]

const getConvertedPermalink = (fullPermalink: string) => {
// NOTE: If the full permalink ends with `_index`,
Expand Down Expand Up @@ -90,7 +87,7 @@ async function main() {
const resources = await getAllResourcesWithFullPermalinks(client)

// Construct an array of sitemap entries
const sitemapEntries: SitemapEntry[] = []
const sitemapEntries: PageOnlySitemapEntry[] = []

// Process each resource
for (const resource of resources) {
Expand All @@ -99,7 +96,10 @@ async function main() {
)

// Ensure the resource is a page (we don't need to write folders)
if (PAGE_RESOURCE_TYPES.includes(resource.type) && resource.content) {
if (
PAGE_RESOURCE_TYPES.find((t) => t === resource.type) &&
resource.content
) {
// Inject page type and title into content before writing to file
resource.content.page = {
...resource.content.page,
Expand All @@ -112,6 +112,9 @@ async function main() {
resource.parentId,
)

// NOTE: We remap the ID for _index pages to be the ID of the folder,
// as both will have the same permalink and the folder is recognized as
// the parent of all the children resources
const idOfFolder = resources.find(
(item) =>
resource.fullPermalink.endsWith(INDEX_PAGE_PERMALINK) &&
Expand All @@ -120,9 +123,9 @@ async function main() {
getConvertedPermalink(resource.fullPermalink),
)?.id

const sitemapEntry: SitemapEntry = {
const sitemapEntry: PageOnlySitemapEntry = {
id: idOfFolder ?? resource.id,
type: resource.type,
type: resource.type as PageResourceType,
title: resource.title,
permalink: `/${getConvertedPermalink(resource.fullPermalink)}`,
lastModified: resource.updatedAt.toISOString(),
Expand Down Expand Up @@ -197,7 +200,7 @@ async function main() {

function generateSitemapTree(
resources: Resource[],
sitemapEntries: SitemapEntry[],
sitemapEntries: PageOnlySitemapEntry[],
pathPrefix: string,
): SitemapEntry[] | undefined {
const pathPrefixWithoutLeadingSlash = pathPrefix.slice(1)
Expand Down Expand Up @@ -246,7 +249,13 @@ function generateSitemapTree(
)
.map((danglingDirectory) => {
const pageName = danglingDirectory.replace(/-/g, " ")
const title = pageName.charAt(0).toUpperCase() + pageName.slice(1)
const generatedTitle =
pageName.charAt(0).toUpperCase() + pageName.slice(1)
const folderResourceTitle = resources.find(
(resource) => resource.fullPermalink === danglingDirectory,
)?.title
const title = folderResourceTitle ?? generatedTitle

logDebug(
`Creating index page for dangling directory: ${danglingDirectory}`,
)
Expand All @@ -263,7 +272,7 @@ function generateSitemapTree(
(pathPrefixWithoutLeadingSlash.length === 0
? danglingDirectory
: `${pathPrefixWithoutLeadingSlash}/${danglingDirectory}`) &&
FOLDER_RESOURCE_TYPES.includes(resource.type),
FOLDER_RESOURCE_TYPES.find((t) => t === resource.type),
)

return {
Expand Down Expand Up @@ -340,7 +349,7 @@ function getFoldersAndCollections(
resources.some(
(resource) =>
resource.id === child.id &&
FOLDER_RESOURCE_TYPES.includes(resource.type),
FOLDER_RESOURCE_TYPES.find((t) => t === resource.type),
),
)

Expand Down
6 changes: 6 additions & 0 deletions tooling/build/scripts/publishing/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Resource as DbResource } from "~generated/selectableTypes"

import { PAGE_RESOURCE_TYPES } from "./constants"

// NOTE: this needs the `omit` because the `parentId`
// we defined in studio
export interface Resource extends Omit<DbResource, "parentId"> {
Expand Down Expand Up @@ -30,3 +32,7 @@ export type SitemapEntry = Pick<
children?: SitemapEntry[]
tags?: Tag[]
}

export type PageOnlySitemapEntry = Omit<SitemapEntry, "children"> & {
type: (typeof PAGE_RESOURCE_TYPES)[number]
}

0 comments on commit 820a7c0

Please sign in to comment.