Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
Fix Notion gallery card covers and Drive links
Marc Seitz933da799862b
diff --git a/components/view/viewer/notion-page.tsx b/components/view/viewer/notion-page.tsxindex 70d2622c..2829b626 100644--- a/components/view/viewer/notion-page.tsx+++ b/components/view/viewer/notion-page.tsx@@ -12,6 +12,7 @@ import { NotionRenderer } from "react-notion-x";// core styles shared by all of react-notion-x (required)import "react-notion-x/src/styles.css";+import { getExternalRelationLinks } from "@/lib/notion/external-relation-links";import { useSafePageViewTracker } from "@/lib/tracking/safe-page-view-tracker";import { getTrackingOptions } from "@/lib/tracking/tracking-config";import { NotionTheme } from "@/lib/types";@@ -500,6 +501,31 @@ export const NotionPage = ({Collection,Code,PageLink: PageLinkComponent,+ propertyRelationValue: (+ props: { data?: any[] },+ defaultValueFn: () => React.ReactNode,+ ) => {+ const externalLinks = getExternalRelationLinks(props.data);++ if (!externalLinks) {+ return defaultValueFn();+ }++ return externalLinks.map((link, index) => (+ <React.Fragment key={`${link.url}-${index}`}>+ {index > 0 ? ", " : null}+ <a+ className="notion-link"+ href={link.url}+ target="_blank"+ rel="noreferrer noopener"+ onClick={(event) => event.stopPropagation()}+ >+ {link.text}+ </a>+ </React.Fragment>+ ));+ },}),[PageLinkComponent],);diff --git a/lib/notion/external-relation-links.ts b/lib/notion/external-relation-links.tsnew file mode 100644index 00000000..ac384293--- /dev/null+++ b/lib/notion/external-relation-links.ts@@ -0,0 +1,55 @@+import { Decoration } from "notion-types";++export type ExternalRelationLink = {+ text: string;+ url: string;+};++export function getExternalRelationLinks(+ data?: Decoration[],+): ExternalRelationLink[] | null {+ if (!Array.isArray(data)) return null;++ const links: ExternalRelationLink[] = [];++ for (const segment of data) {+ if (!Array.isArray(segment)) return null;++ const [text, decorations] = segment;+ const isSeparator = text === "," || text === " ";++ if (!Array.isArray(decorations)) {+ if (isSeparator) continue;+ return null;+ }++ const linkDecorator = decorations.find(+ (decoration: any) =>+ Array.isArray(decoration) &&+ decoration[0] === "a" &&+ typeof decoration[1] === "string",+ );++ if (!linkDecorator) return null;++ const linkUrl = linkDecorator[1];+ if (typeof linkUrl !== "string") return null;++ try {+ const url = new URL(linkUrl);++ if (url.protocol !== "http:" && url.protocol !== "https:") {+ return null;+ }++ links.push({+ text: typeof text === "string" && text ? text : url.href,+ url: url.href,+ });+ } catch {+ return null;+ }+ }++ return links.length > 0 ? links : null;+}diff --git a/lib/notion/utils.ts b/lib/notion/utils.tsindex 08d6cd02..237eab63 100644--- a/lib/notion/utils.ts+++ b/lib/notion/utils.ts@@ -8,9 +8,7 @@ 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(- value: any[] | undefined,-): string[] {+function extractPageReferencesFromRichText(value: any[] | undefined): string[] {if (!value || !Array.isArray(value)) return [];const pageIds: string[] = [];@@ -43,6 +41,83 @@ function extractPageReferencesFromRichText(return pageIds;}+function getExternalObjectLinkTarget(block: any): {+ title: string;+ url: string;+} | null {+ if (block?.type !== "external_object_instance_page") return null;++ const url = block.format?.uri;+ if (typeof url !== "string" || !url) return null;++ const title = block.format?.attributes?.find(+ (attribute: any) => attribute?.id === "title",+ )?.values?.[0];++ return {+ title: typeof title === "string" && title ? title : url,+ url,+ };+}++function getLinkedExternalObjectTarget(+ decorator: any[],+ recordMap: ExtendedRecordMap,+): ReturnType<typeof getExternalObjectLinkTarget> {+ if (decorator[0] === "p" && decorator[1]) {+ return getExternalObjectLinkTarget(+ (recordMap.block[decorator[1]] as any)?.value,+ );+ }++ if (decorator[0] === "\u2023" && Array.isArray(decorator[1])) {+ const [, id] = decorator[1];+ if (id) {+ return getExternalObjectLinkTarget((recordMap.block[id] as any)?.value);+ }+ }++ return null;+}++function linkExternalObjectMentions(recordMap: ExtendedRecordMap): void {+ for (const blockEntry of Object.values(recordMap.block)) {+ const block = blockEntry?.value as any;+ if (!block?.properties) continue;++ for (const propKey of Object.keys(block.properties)) {+ const propValue = block.properties[propKey];+ if (!Array.isArray(propValue)) continue;++ block.properties[propKey] = propValue.map((segment: any) => {+ if (!Array.isArray(segment)) return segment;++ const [text, decorations] = segment;+ if (!Array.isArray(decorations)) return segment;++ const linkTarget = decorations+ .map((decoration) =>+ Array.isArray(decoration)+ ? getLinkedExternalObjectTarget(decoration, recordMap)+ : null,+ )+ .find(Boolean);++ if (!linkTarget) return segment;++ const nextDecorations = decorations+ .filter((decoration) => {+ if (!Array.isArray(decoration)) return true;+ return !getLinkedExternalObjectTarget(decoration, recordMap);+ })+ .concat([["a", linkTarget.url]]);++ return [text === "\u2023" ? linkTarget.title : text, nextDecorations];+ });+ }+ }+}+/*** Normalizes the block entries in a recordMap to ensure a consistent structure.*@@ -138,7 +213,10 @@ export async function fetchMissingPageReferences((id) => !recordMap.block[id],);- if (missingPageIds.length === 0) return;+ if (missingPageIds.length === 0) {+ linkExternalObjectMentions(recordMap);+ return;+ }try {const newBlocks = await notion.getBlocks(missingPageIds);@@ -149,6 +227,7 @@ export async function fetchMissingPageReferences(};}normalizeRecordMap(recordMap);+ linkExternalObjectMentions(recordMap);} catch (err) {console.warn("Failed to fetch missing page references:", err);}@@ -161,7 +240,12 @@ export const addSignedUrls: NotionAPI["addSignedUrls"] = async ({recordMap.signed_urls = {};if (!contentBlockIds) {- contentBlockIds = getPageContentBlockIds(recordMap);+ contentBlockIds = Array.from(+ new Set([+ ...getPageContentBlockIds(recordMap),+ ...Object.keys(recordMap.block),+ ]),+ );}const allFileInstances = contentBlockIds.flatMap((blockId) => {