This commit is contained in:
Travis Fischer
2022-04-07 14:53:08 -04:00
parent 679131ad22
commit 45b20d9622
10 changed files with 196 additions and 86 deletions

View File

@@ -8,6 +8,7 @@
import { parsePageId } from 'notion-utils'
import posthog from 'posthog-js'
import { getEnv, getSiteConfig } from './get-config-value'
import { NavigationLink } from './site-config'
import {
PageUrlOverridesInverseMap,
PageUrlOverridesMap,
@@ -93,6 +94,14 @@ export const navigationStyle: NavigationStyle = getSiteConfig(
'default'
)
export const navigationLinks: Array<NavigationLink | null> = getSiteConfig(
'navigationLinks',
null
)
// Optional site search
export const isSearchEnabled: boolean = getSiteConfig('isSearchEnabled', true)
// ----------------------------------------------------------------------------
// Optional redis instance for persisting preview images

View File

@@ -12,11 +12,13 @@ const uuid = !!includeNotionIdInUrls
export const mapPageUrl =
(site: Site, recordMap: ExtendedRecordMap, searchParams: URLSearchParams) =>
(pageId = '') => {
if (uuidToId(pageId) === site.rootNotionPageId) {
const pageUuid = parsePageId(pageId, { uuid: true })
if (uuidToId(pageUuid) === site.rootNotionPageId) {
return createUrl('/', searchParams)
} else {
return createUrl(
`/${getCanonicalPageId(pageId, recordMap, { uuid })}`,
`/${getCanonicalPageId(pageUuid, recordMap, { uuid })}`,
searchParams
)
}

View File

@@ -1,15 +1,46 @@
import pMap from 'p-map'
import { ExtendedRecordMap, SearchParams, SearchResults } from 'notion-types'
import { mergeRecordMaps } from 'notion-utils'
import { notion } from './notion-api'
import { getPreviewImageMap } from './preview-images'
import { getTweetAstMap } from './tweet-embeds'
import {
isPreviewImageSupportEnabled,
isTweetEmbedSupportEnabled
isTweetEmbedSupportEnabled,
navigationStyle,
navigationLinks
} from './config'
export async function getPage(pageId: string): Promise<ExtendedRecordMap> {
const recordMap = await notion.getPage(pageId)
let recordMap = await notion.getPage(pageId)
if (navigationStyle !== 'default') {
const navigationLinkPageIds = (navigationLinks || [])
.map((link) => link.pageId)
.filter(Boolean)
if (navigationLinkPageIds.length) {
const navigationLinkRecordMaps: ExtendedRecordMap[] = await pMap(
navigationLinkPageIds,
async (navigationLinkPageId) =>
notion.getPage(navigationLinkPageId, {
fetchMissingBlocks: false,
fetchCollections: false,
signFileUrls: false
}),
{
concurrency: 4
}
)
recordMap = navigationLinkRecordMaps.reduce(
(map, navigationLinkRecordMap) =>
mergeRecordMaps(map, navigationLinkRecordMap),
recordMap
)
}
}
if (isPreviewImageSupportEnabled) {
const previewImageMap = await getPreviewImageMap(recordMap)

View File

@@ -21,12 +21,20 @@ export interface SiteConfig {
isPreviewImageSupportEnabled?: boolean
isTweetEmbedSupportEnabled?: boolean
isRedisEnabled?: boolean
isSearchEnabled?: boolean
includeNotionIdInUrls?: boolean
pageUrlOverrides?: types.PageUrlOverridesMap
pageUrlAdditions?: types.PageUrlOverridesMap
navigationStyle?: types.NavigationStyle
navigationLinks?: Array<NavigationLink>
}
export interface NavigationLink {
title: string
pageId?: string
url?: string
}
export const siteConfig = (config: SiteConfig): SiteConfig => {

View File

@@ -67,20 +67,3 @@ export interface PageUrlOverridesInverseMap {
// (this overrides the built-in URL path generation for these pages)
[pageId: string]: string
}
export interface PreviewImage {
url: string
originalWidth: number
originalHeight: number
width: number
height: number
type: string
dataURIBase64: string
error?: string
statusCode?: number
}
export interface PreviewImageMap {
[url: string]: PreviewImage
}