Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
fix: fetch missing notion page references
Marc Seitz1f7d767f4424
diff --git a/lib/notion/utils.ts b/lib/notion/utils.tsindex 1bfb684e..c1572c5e 100644--- a/lib/notion/utils.ts+++ b/lib/notion/utils.ts@@ -1,8 +1,92 @@import { NotionAPI } from "notion-client";+import { ExtendedRecordMap } from "notion-types";import { getPageContentBlockIds, parsePageId } from "notion-utils";import notion from "./index";+/**+ * Extracts all page reference IDs from rich text decorations in the recordMap.+ * This handles the "p" decorator which is used for page mentions/links.+ */+function extractPageReferencesFromRichText(+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ value: any[] | undefined,+): string[] {+ if (!value || !Array.isArray(value)) return [];++ const pageIds: string[] = [];++ for (const segment of value) {+ if (!Array.isArray(segment)) continue;++ const decorations = segment[1];+ if (!decorations || !Array.isArray(decorations)) continue;++ for (const decoration of decorations) {+ if (!Array.isArray(decoration)) continue;++ // "p" decorator indicates a page reference+ if (decoration[0] === "p" && decoration[1]) {+ pageIds.push(decoration[1]);+ }++ // "‣" (U+2023) decorator can also reference pages+ if (decoration[0] === "\u2023" && Array.isArray(decoration[1])) {+ const [linkType, id] = decoration[1];+ // Skip user references ("u"), only handle page references+ if (linkType !== "u" && id) {+ pageIds.push(id);+ }+ }+ }+ }++ return pageIds;+}++/**+ * Fetches missing page blocks that are referenced in rich text but not in the recordMap.+ * This fixes the issue where tables with multiple page links only show the first one.+ */+export async function fetchMissingPageReferences(+ recordMap: ExtendedRecordMap,+): Promise<void> {+ const allPageReferenceIds = new Set<string>();++ // Iterate through all blocks to find page references in their properties+ for (const blockId of Object.keys(recordMap.block)) {+ const block = recordMap.block[blockId]?.value;+ if (!block?.properties) continue;++ // Check all properties for page references+ for (const propKey of Object.keys(block.properties)) {+ const propValue = block.properties[propKey];+ const pageIds = extractPageReferencesFromRichText(propValue);+ pageIds.forEach((id) => allPageReferenceIds.add(id));+ }+ }++ // Filter out page IDs that are already in the recordMap+ const missingPageIds = Array.from(allPageReferenceIds).filter(+ (id) => !recordMap.block[id],+ );++ if (missingPageIds.length === 0) return;++ // Fetch missing blocks in batches+ try {+ const newBlocks = await notion.getBlocks(missingPageIds);+ if (newBlocks?.recordMap?.block) {+ recordMap.block = {+ ...recordMap.block,+ ...newBlocks.recordMap.block,+ };+ }+ } catch (err) {+ console.warn("Failed to fetch missing page references:", err);+ }+}+export const addSignedUrls: NotionAPI["addSignedUrls"] = async ({recordMap,contentBlockIds,diff --git a/pages/api/file/notion/index.ts b/pages/api/file/notion/index.tsindex 4f6aa7ca..c6284c3d 100644--- a/pages/api/file/notion/index.ts+++ b/pages/api/file/notion/index.ts@@ -1,7 +1,7 @@import { NextApiRequest, NextApiResponse } from "next";import notion from "@/lib/notion";-import { addSignedUrls } from "@/lib/notion/utils";+import { addSignedUrls, fetchMissingPageReferences } from "@/lib/notion/utils";import { log } from "@/lib/utils";export default async function handle(@@ -25,14 +25,18 @@ export default async function handle(try {const recordMap = await notion.getPage(pageId, { signFileUrls: false });- // TODO: separately sign the file urls until PR merged and published; ref: https://github.com/NotionX/react-notion-x/issues/580#issuecomment-2542823817- await addSignedUrls({ recordMap });if (!recordMap) {res.status(500).json({ message: "Internal Server Error" });return;}+ // Fetch missing page references that are embedded in rich text (e.g., table cells with multiple page links)+ await fetchMissingPageReferences(recordMap);++ // TODO: separately sign the file urls until PR merged and published; ref: https://github.com/NotionX/react-notion-x/issues/580#issuecomment-2542823817+ await addSignedUrls({ recordMap });+res.status(200).json(recordMap);return;} catch (error) {diff --git a/pages/view/[linkId]/d/[documentId].tsx b/pages/view/[linkId]/d/[documentId].tsxindex 23529bc5..09a27c13 100644--- a/pages/view/[linkId]/d/[documentId].tsx+++ b/pages/view/[linkId]/d/[documentId].tsx@@ -12,7 +12,10 @@ import { parsePageId } from "notion-utils";import z from "zod";import notion from "@/lib/notion";-import { addSignedUrls } from "@/lib/notion/utils";+import {+ addSignedUrls,+ fetchMissingPageReferences,+} from "@/lib/notion/utils";import { CustomUser, LinkWithDataroomDocument, NotionTheme } from "@/lib/types";import LoadingSpinner from "@/components/ui/loading-spinner";@@ -217,6 +220,8 @@ export async function getStaticProps(context: GetStaticPropsContext) {pageId = notionPageId;recordMap = await notion.getPage(pageId, { signFileUrls: false });+ // Fetch missing page references that are embedded in rich text (e.g., table cells with multiple page links)+ await fetchMissingPageReferences(recordMap);// TODO: separately sign the file urls until PR merged and published; ref: https://github.com/NotionX/react-notion-x/issues/580#issuecomment-2542823817await addSignedUrls({ recordMap });}diff --git a/pages/view/[linkId]/index.tsx b/pages/view/[linkId]/index.tsxindex fdef4061..7ff9f1e3 100644--- a/pages/view/[linkId]/index.tsx+++ b/pages/view/[linkId]/index.tsx@@ -14,7 +14,10 @@ import z from "zod";import { getFeatureFlags } from "@/lib/featureFlags";import notion from "@/lib/notion";-import { addSignedUrls } from "@/lib/notion/utils";+import {+ addSignedUrls,+ fetchMissingPageReferences,+} from "@/lib/notion/utils";import {CustomUser,LinkWithDataroom,@@ -144,6 +147,8 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {pageId = notionPageId;recordMap = await notion.getPage(pageId, { signFileUrls: false });+ // Fetch missing page references that are embedded in rich text (e.g., table cells with multiple page links)+ await fetchMissingPageReferences(recordMap);await addSignedUrls({ recordMap });} catch (notionError) {console.error("Notion API error:", notionError);diff --git a/pages/view/domains/[domain]/[slug]/d/[documentId].tsx b/pages/view/domains/[domain]/[slug]/d/[documentId].tsxindex 0acaabbf..76d21782 100644--- a/pages/view/domains/[domain]/[slug]/d/[documentId].tsx+++ b/pages/view/domains/[domain]/[slug]/d/[documentId].tsx@@ -12,7 +12,10 @@ import { parsePageId } from "notion-utils";import z from "zod";import notion from "@/lib/notion";-import { addSignedUrls } from "@/lib/notion/utils";+import {+ addSignedUrls,+ fetchMissingPageReferences,+} from "@/lib/notion/utils";import { CustomUser, LinkWithDataroomDocument, NotionTheme } from "@/lib/types";import LoadingSpinner from "@/components/ui/loading-spinner";@@ -236,6 +239,8 @@ export async function getStaticProps(context: GetStaticPropsContext) {pageId = notionPageId;recordMap = await notion.getPage(pageId, { signFileUrls: false });+ // Fetch missing page references that are embedded in rich text (e.g., table cells with multiple page links)+ await fetchMissingPageReferences(recordMap);// TODO: separately sign the file urls until PR merged and published; ref: https://github.com/NotionX/react-notion-x/issues/580#issuecomment-2542823817await addSignedUrls({ recordMap });}diff --git a/pages/view/domains/[domain]/[slug]/index.tsx b/pages/view/domains/[domain]/[slug]/index.tsxindex 04387578..ea5296a8 100644--- a/pages/view/domains/[domain]/[slug]/index.tsx+++ b/pages/view/domains/[domain]/[slug]/index.tsx@@ -14,6 +14,10 @@ import z from "zod";import { getFeatureFlags } from "@/lib/featureFlags";import notion from "@/lib/notion";+import {+ addSignedUrls,+ fetchMissingPageReferences,+} from "@/lib/notion/utils";import {CustomUser,LinkWithDataroom,@@ -138,7 +142,10 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {}pageId = notionPageId;- recordMap = await notion.getPage(pageId);+ recordMap = await notion.getPage(pageId, { signFileUrls: false });+ // Fetch missing page references that are embedded in rich text (e.g., table cells with multiple page links)+ await fetchMissingPageReferences(recordMap);+ await addSignedUrls({ recordMap });}const { team, teamId, advancedExcelEnabled, ...linkDocument } =