Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

fix(notion): keep subpage links on the view URL

Marc Seitz1a9a09988317
diff --git a/components/view/viewer/notion-page.tsx b/components/view/viewer/notion-page.tsx
index fee91454..93776bbc 100644
--- a/components/view/viewer/notion-page.tsx
+++ b/components/view/viewer/notion-page.tsx
@@ -6,13 +6,16 @@ import React from "react";
import { ConfidentialViewOverlay } from "@/ee/features/permissions/components/confidential-view/confidential-view-overlay";
import { Slash } from "lucide-react";
import { ExtendedRecordMap } from "notion-types";
-import { parsePageId } from "notion-utils";
import { useQueryState } from "nuqs";
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 {
+ classifyViewerHref,
+ toViewerPageHref,
+} from "@/lib/notion/viewer-page-url";
import { useSafePageViewTracker } from "@/lib/tracking/safe-page-view-tracker";
import { getTrackingOptions } from "@/lib/tracking/tracking-config";
import { NotionTheme } from "@/lib/types";
@@ -111,7 +114,7 @@ const obfuscateNotionIds = (container: HTMLElement) => {
const anchors = container.querySelectorAll("a[href]:not([target])");
anchors.forEach((anchor) => {
const href = anchor.getAttribute("href");
- if (href && uuidPattern.test(href)) {
+ if (href && !href.includes("pageid=") && uuidPattern.test(href)) {
const newHref = href.replace(uuidPattern, (match) =>
getObfuscatedId(match),
);
@@ -467,44 +470,71 @@ export const NotionPage = ({
};
}, [scrollToHashElement]);
- const PageLinkComponent = useMemo(
+ const ViewerNotionLink = useMemo(
() =>
- function PageLink({
+ function ViewerNotionLink({
href,
className,
children,
style,
+ target,
+ rel,
+ ...rest
}: {
href?: string;
className?: string;
children?: React.ReactNode;
style?: React.CSSProperties;
+ target?: string;
+ rel?: string;
[key: string]: any;
}) {
- const handleClick = (e: React.MouseEvent) => {
- e.preventDefault();
- e.stopPropagation();
- if (!href) return;
+ const classified = classifyViewerHref(href);
- const pageId =
- parsePageId(href, { uuid: false }) ??
- href.split("/").pop()?.split("?")[0]?.split("#")[0];
+ if (!classified) {
+ return (
+ <a className={className} style={style} href={href} {...rest}>
+ {children}
+ </a>
+ );
+ }
- if (pageId) {
- setSubPageId(pageId);
+ switch (classified.kind) {
+ case "page":
+ return (
+ <a
+ className={className}
+ style={style}
+ href={toViewerPageHref(classified.pageId)}
+ onClick={(event) => {
+ event.preventDefault();
+ event.stopPropagation();
+ setSubPageId(classified.pageId);
+ }}
+ >
+ {children}
+ </a>
+ );
+ case "other":
+ return (
+ <a
+ className={className}
+ style={style}
+ href={classified.href}
+ target={classified.openInNewTab ? (target ?? "_blank") : target}
+ rel={
+ classified.openInNewTab ? (rel ?? "noopener noreferrer") : rel
+ }
+ {...rest}
+ >
+ {children}
+ </a>
+ );
+ default: {
+ const _exhaustive: never = classified;
+ return _exhaustive;
}
- };
-
- return (
- <a
- className={className}
- style={style}
- href={href}
- onClick={handleClick}
- >
- {children}
- </a>
- );
+ }
},
[setSubPageId],
);
@@ -513,7 +543,8 @@ export const NotionPage = ({
() => ({
Collection,
Code,
- PageLink: PageLinkComponent,
+ PageLink: ViewerNotionLink,
+ Link: ViewerNotionLink,
propertyRelationValue: (
props: { data?: any[] },
defaultValueFn: () => React.ReactNode,
@@ -540,7 +571,7 @@ export const NotionPage = ({
));
},
}),
- [PageLinkComponent],
+ [ViewerNotionLink],
);
// Obfuscate Notion IDs in the DOM after rendering
@@ -636,6 +667,7 @@ export const NotionPage = ({
fullPage={true}
darkMode={theme ? theme === "dark" : false}
disableHeader={true}
+ mapPageUrl={toViewerPageHref}
components={notionComponents}
/>
</div>
diff --git a/lib/notion/viewer-page-url.ts b/lib/notion/viewer-page-url.ts
new file mode 100644
index 00000000..821e42c2
--- /dev/null
+++ b/lib/notion/viewer-page-url.ts
@@ -0,0 +1,55 @@
+import { parsePageId } from "notion-utils";
+
+export type ViewerHref =
+ | { kind: "page"; pageId: string }
+ | { kind: "other"; href: string; openInNewTab: boolean };
+
+function normalizePageId(value: string): string | null {
+ return parsePageId(value, { uuid: false }) ?? null;
+}
+
+export function getViewerPageIdFromHref(href: string): string | null {
+ try {
+ const url = new URL(href, "http://local.invalid");
+ const fromQuery = url.searchParams.get("pageid");
+ if (fromQuery) {
+ return normalizePageId(fromQuery);
+ }
+
+ const pathId = normalizePageId(url.pathname);
+ if (!pathId) return null;
+
+ const stripped = url.pathname.replace(/^\/|\/$/g, "").replace(/-/g, "");
+ if (stripped.toLowerCase() === pathId) {
+ return pathId;
+ }
+
+ if (url.origin !== "http://local.invalid") {
+ return null;
+ }
+
+ return pathId;
+ } catch {
+ return normalizePageId(href);
+ }
+}
+
+export function classifyViewerHref(href: string | undefined): ViewerHref | null {
+ if (!href) return null;
+
+ const pageId = getViewerPageIdFromHref(href);
+ if (pageId) {
+ return { kind: "page", pageId };
+ }
+
+ return {
+ kind: "other",
+ href,
+ openInNewTab: /^https?:\/\//i.test(href) || href.startsWith("//"),
+ };
+}
+
+export function toViewerPageHref(pageId: string): string {
+ const id = normalizePageId(pageId) ?? pageId.replace(/-/g, "");
+ return `?pageid=${id}`;
+}
diff --git a/package.json b/package.json
index 3e8e3936..81614123 100644
--- a/package.json
+++ b/package.json
@@ -10,8 +10,6 @@
"build": "next build",
"start": "next start",
"lint": "next lint",
- "check:brand-logo": "tsx scripts/check-brand-logo.ts",
- "check:mcp-schemas": "tsx scripts/check-mcp-schema-dialect.ts",
"postinstall": "prisma generate",
"vercel-build": "prisma migrate deploy && next build",
"email": "email dev --dir ./components/emails --port 3001",