notion-page.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. "use client";
  2. import dynamic from "next/dynamic";
  3. import Link from "next/link";
  4. import { useTheme } from "next-themes";
  5. import type { ExtendedRecordMap } from "notion-types";
  6. import { useEffect, useMemo, useRef, useSyncExternalStore } from "react";
  7. import { NotionRenderer } from "react-notion-x";
  8. import { useLocale, useT } from "@/components/locale-provider";
  9. import { formatDate, localePath } from "@/lib/i18n";
  10. const prismComponents = [
  11. "prism-markup-templating",
  12. "prism-markup",
  13. "prism-bash",
  14. "prism-c",
  15. "prism-cpp",
  16. "prism-csharp",
  17. "prism-docker",
  18. "prism-java",
  19. "prism-js-templates",
  20. "prism-coffeescript",
  21. "prism-diff",
  22. "prism-git",
  23. "prism-go",
  24. "prism-graphql",
  25. "prism-handlebars",
  26. "prism-less",
  27. "prism-makefile",
  28. "prism-markdown",
  29. "prism-objectivec",
  30. "prism-ocaml",
  31. "prism-python",
  32. "prism-reason",
  33. "prism-rust",
  34. "prism-sass",
  35. "prism-scss",
  36. "prism-solidity",
  37. "prism-sql",
  38. "prism-stylus",
  39. "prism-swift",
  40. "prism-wasm",
  41. "prism-yaml",
  42. ];
  43. const Code = dynamic(
  44. () =>
  45. import("react-notion-x/third-party/code").then(async (m) => {
  46. await Promise.allSettled(
  47. prismComponents.map(
  48. (component) => import(`prismjs/components/${component}.js`),
  49. ),
  50. );
  51. return m.Code;
  52. }),
  53. { ssr: false },
  54. );
  55. const Collection = dynamic(
  56. () =>
  57. import("react-notion-x/third-party/collection").then((m) => m.Collection),
  58. { ssr: false },
  59. );
  60. const Equation = dynamic(() =>
  61. import("react-notion-x/third-party/equation").then((m) => m.Equation),
  62. );
  63. const Pdf = dynamic(
  64. () => import("react-notion-x/third-party/pdf").then((m) => m.Pdf),
  65. {
  66. ssr: false,
  67. },
  68. );
  69. const Modal = dynamic(
  70. () => import("react-notion-x/third-party/modal").then((m) => m.Modal),
  71. { ssr: false },
  72. );
  73. const noSubscribe = () => () => {};
  74. export const NotionPage = ({
  75. recordMap,
  76. rootPageId,
  77. title,
  78. created,
  79. notice,
  80. }: {
  81. recordMap: ExtendedRecordMap;
  82. rootPageId: string;
  83. title?: string;
  84. /** When the post was first published (a translation keeps its original's date). */
  85. created?: number;
  86. /** A note above the body, e.g. that it hasn't been translated yet. */
  87. notice?: string;
  88. }) => {
  89. const lang = useLocale();
  90. const { blog } = useT();
  91. const { resolvedTheme } = useTheme();
  92. const mounted = useSyncExternalStore(
  93. noSubscribe,
  94. () => true,
  95. () => false,
  96. );
  97. const components = useMemo(
  98. () => ({ Code, Collection, Equation, Pdf, Modal }),
  99. [],
  100. );
  101. const articleRef = useRef<HTMLElement>(null);
  102. // Link previews show images from other sites, which come and go: drop the dead ones.
  103. useEffect(() => {
  104. const root = articleRef.current;
  105. if (!root) return;
  106. const onError = (e: Event) => {
  107. if (!(e.target instanceof HTMLImageElement)) return;
  108. const box = e.target.closest<HTMLElement>(
  109. ".notion-bookmark-link-icon, .notion-bookmark-image",
  110. );
  111. if (box) box.style.display = "none";
  112. };
  113. root.addEventListener("error", onError, true);
  114. // An image that failed before hydration is loaded again so its error is heard.
  115. root
  116. .querySelectorAll<HTMLImageElement>(".notion-bookmark img")
  117. .forEach((img) => {
  118. if (img.complete && img.naturalWidth === 0) {
  119. img.setAttribute("src", img.getAttribute("src") ?? "");
  120. }
  121. });
  122. return () => root.removeEventListener("error", onError, true);
  123. }, [recordMap]);
  124. if (!recordMap) {
  125. return null;
  126. }
  127. return (
  128. <article ref={articleRef} className="mx-auto max-w-[760px]" data-col="post">
  129. {/* Header and back link line up with the Notion body (its page padding plus the text's 2px). */}
  130. <Link
  131. className="lg ml-[calc(min(16px,8vw)_+_2px)] inline-flex h-9 items-center gap-1.5 rounded-full px-3.5 text-[13px] font-medium"
  132. data-cursor={blog.backCursor}
  133. href={localePath(lang, "/blog")}
  134. >
  135. <span aria-hidden className="lg-caustic" />
  136. <span>{blog.back}</span>
  137. </Link>
  138. <header className="mt-8 mb-6 px-[calc(min(16px,8vw)_+_2px)]">
  139. {created && (
  140. <time className="eyebrow" dateTime={new Date(created).toISOString()}>
  141. {formatDate(lang, created, {
  142. month: "long",
  143. day: "numeric",
  144. year: "numeric",
  145. })}
  146. </time>
  147. )}
  148. <h1 className="mt-3 font-display text-[clamp(2.1rem,4.8vw,3.2rem)] leading-[1.06] font-semibold tracking-[-0.018em] text-balance zh:leading-[1.3]">
  149. {title}
  150. </h1>
  151. {notice && <p className="mt-4 text-[14px] text-muted">{notice}</p>}
  152. </header>
  153. <div lang={notice ? "en" : undefined}>
  154. <NotionRenderer
  155. components={components}
  156. darkMode={mounted && resolvedTheme === "dark"}
  157. fullPage={false}
  158. recordMap={recordMap}
  159. rootPageId={rootPageId}
  160. />
  161. </div>
  162. </article>
  163. );
  164. };