mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-06-01 15:11:00 +00:00
feat: add support for page URL overrides
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
import { parsePageId } from 'notion-utils'
|
||||
import { getSiteConfig, getEnv } from './get-config-value'
|
||||
import { PageUrlOverridesMap, PageUrlOverridesInverseMap } from './types'
|
||||
|
||||
export const rootNotionPageId: string = parsePageId(
|
||||
getSiteConfig('rootNotionPageId'),
|
||||
@@ -23,6 +24,15 @@ export const rootNotionSpaceId: string | null = parsePageId(
|
||||
{ uuid: true }
|
||||
)
|
||||
|
||||
export const pageUrlOverrides = cleanPageUrlOverrides(
|
||||
getSiteConfig('pageUrlOverrides', {}) || {}
|
||||
)
|
||||
|
||||
export const inversePageUrlOverrides = invertPageUrlOverrides(pageUrlOverrides)
|
||||
|
||||
console.log('pageUrlOverrides', pageUrlOverrides)
|
||||
console.log('inversePageUrlOverrides', inversePageUrlOverrides)
|
||||
|
||||
// general site config
|
||||
export const name: string = getSiteConfig('name')
|
||||
export const author: string = getSiteConfig('author')
|
||||
@@ -146,3 +156,44 @@ function getGoogleApplicationCredentials() {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
function cleanPageUrlOverrides(
|
||||
pageUrlOverrides: PageUrlOverridesMap
|
||||
): PageUrlOverridesMap {
|
||||
return Object.keys(pageUrlOverrides).reduce((acc, pageId) => {
|
||||
const uuid = parsePageId(pageId, { uuid: false })
|
||||
const uri = pageUrlOverrides[pageId]
|
||||
|
||||
if (!uuid) {
|
||||
throw new Error(`Invalid pageUrlOverrides page id "${pageId}"`)
|
||||
}
|
||||
|
||||
if (!uri) {
|
||||
throw new Error(`Missing pageUrlOverrides value for page "${pageId}"`)
|
||||
}
|
||||
|
||||
if (!uri.startsWith('/')) {
|
||||
throw new Error(
|
||||
`Invalid pageUrlOverrides value for page "${pageId}": value "${uri}" should be a relative URI that starts with "/"`
|
||||
)
|
||||
}
|
||||
|
||||
const path = uri.slice(1)
|
||||
|
||||
return {
|
||||
...acc,
|
||||
[uuid]: path
|
||||
}
|
||||
}, {})
|
||||
}
|
||||
|
||||
function invertPageUrlOverrides(
|
||||
pageUrlOverrides: PageUrlOverridesMap
|
||||
): PageUrlOverridesInverseMap {
|
||||
return Object.keys(pageUrlOverrides).reduce((acc, pageId) => {
|
||||
return {
|
||||
...acc,
|
||||
[pageUrlOverrides[pageId]]: pageId
|
||||
}
|
||||
}, {})
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import pMemoize from 'p-memoize'
|
||||
import { getAllPagesInSpace, getCanonicalPageId } from 'notion-utils'
|
||||
import { getAllPagesInSpace } from 'notion-utils'
|
||||
|
||||
import * as types from './types'
|
||||
import { includeNotionIdInUrls } from './config'
|
||||
import { getPage } from './notion'
|
||||
import { getCanonicalPageId } from './get-canonical-page-id'
|
||||
|
||||
const uuid = !!includeNotionIdInUrls
|
||||
|
||||
|
||||
29
lib/get-canonical-page-id.ts
Normal file
29
lib/get-canonical-page-id.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { ExtendedRecordMap } from 'notion-types'
|
||||
import {
|
||||
parsePageId,
|
||||
getCanonicalPageId as getCanonicalPageIdImpl
|
||||
} from 'notion-utils'
|
||||
|
||||
import { pageUrlOverrides } from './config'
|
||||
|
||||
export function getCanonicalPageId(
|
||||
pageId: string,
|
||||
recordMap: ExtendedRecordMap,
|
||||
{ uuid = true }: { uuid?: boolean } = {}
|
||||
): string | null {
|
||||
const cleanPageId = parsePageId(pageId, { uuid: false })
|
||||
if (!cleanPageId) {
|
||||
return null
|
||||
}
|
||||
|
||||
console.log('getCanonicalPageId', pageId)
|
||||
|
||||
const override = pageUrlOverrides[cleanPageId]
|
||||
if (override) {
|
||||
return override
|
||||
} else {
|
||||
return getCanonicalPageIdImpl(pageId, recordMap, {
|
||||
uuid
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { parsePageId } from 'notion-utils'
|
||||
import * as acl from './acl'
|
||||
import * as types from './types'
|
||||
import { parsePageId } from 'notion-utils'
|
||||
import { inversePageUrlOverrides } from './config'
|
||||
import { getPage } from './notion'
|
||||
import { getSiteMaps } from './get-site-maps'
|
||||
import { getSiteForDomain } from './get-site-for-domain'
|
||||
@@ -13,6 +14,14 @@ export async function resolveNotionPage(domain: string, rawPageId?: string) {
|
||||
if (rawPageId && rawPageId !== 'index') {
|
||||
pageId = parsePageId(rawPageId)
|
||||
|
||||
if (!pageId) {
|
||||
const override = inversePageUrlOverrides[rawPageId]
|
||||
|
||||
if (override) {
|
||||
pageId = parsePageId(override)
|
||||
}
|
||||
}
|
||||
|
||||
if (pageId) {
|
||||
const resources = await Promise.all([
|
||||
getSiteForDomain(domain),
|
||||
|
||||
12
lib/types.ts
12
lib/types.ts
@@ -54,6 +54,18 @@ export interface CanonicalPageMap {
|
||||
[canonicalPageId: string]: string
|
||||
}
|
||||
|
||||
export interface PageUrlOverridesMap {
|
||||
// maps from a notion page id to the URL path the page should be resolved to
|
||||
// (this overrides the built-in URL path generation for these pages)
|
||||
[pageId: string]: string
|
||||
}
|
||||
|
||||
export interface PageUrlOverridesInverseMap {
|
||||
// maps from a URL path to the notion page id the page should be resolved to
|
||||
// (this overrides the built-in URL path generation for these pages)
|
||||
[pageId: string]: string
|
||||
}
|
||||
|
||||
export interface PreviewImage {
|
||||
url: string
|
||||
originalWidth: number
|
||||
|
||||
Reference in New Issue
Block a user