notion.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { cache } from "react";
  2. import { NotionAPI } from "notion-client";
  3. import { Block, ExtendedRecordMap } from "notion-types";
  4. import { getPageTitle } from "notion-utils";
  5. import { Blog } from "@/types/blog";
  6. import { notionBlogConfig } from "@/config/site";
  7. const notion = new NotionAPI();
  8. export const getPageContent = cache(async (pageId: string) => {
  9. const recordMap = await notion.getPage(pageId);
  10. const title = getPageTitle(recordMap);
  11. const blocks = recordMap.block;
  12. return { title, blocks, recordMap };
  13. });
  14. export async function getAllBlogPosts(pageId: string) {
  15. const recordMap = await notion.getPage(pageId);
  16. const blocks = recordMap.block;
  17. let blogPosts: Blog[] = [];
  18. Object.entries(blocks).map(([key, value]) => {
  19. if (key !== notionBlogConfig.blogParentId) {
  20. blogPosts.push({
  21. id: key,
  22. block: value.value,
  23. pageCover: value.value.format?.page_cover,
  24. title: value.value.properties?.title[0][0],
  25. createdAt: new Date(value.value.created_time),
  26. lastEditedAt: new Date(value.value.last_edited_time),
  27. description: value.value.properties?.["\\u2O5F"]?.[0]?.[0] ?? "",
  28. });
  29. }
  30. });
  31. // Sort by newest first
  32. blogPosts.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
  33. return blogPosts;
  34. }
  35. /** Extract a plain-text excerpt from a Notion recordMap (first ~160 chars). */
  36. export function extractDescription(recordMap: ExtendedRecordMap): string {
  37. const blocks = Object.values(recordMap.block);
  38. for (const block of blocks) {
  39. const value = block?.value;
  40. if (!value) continue;
  41. if (value.type !== "text" && value.type !== "quote") continue;
  42. const text = value.properties?.title
  43. ?.map((chunk: unknown[]) => chunk[0])
  44. .join("")
  45. .trim();
  46. if (text && text.length > 0) {
  47. return text.length > 160 ? text.slice(0, 157) + "..." : text;
  48. }
  49. }
  50. return "";
  51. }
  52. export const customMapImageUrl = (url: string, block: Block): string => {
  53. if (!url) {
  54. throw new Error("URL can't be empty");
  55. }
  56. if (url.startsWith("data:")) {
  57. return url;
  58. } // more recent versions of notion don't proxy unsplash images
  59. if (url.startsWith("https://images.unsplash.com")) {
  60. return url;
  61. }
  62. try {
  63. const u = new URL(url);
  64. if (
  65. u.pathname.startsWith("/secure.notion-static.com") &&
  66. u.hostname.endsWith(".amazonaws.com")
  67. ) {
  68. if (
  69. u.searchParams.has("X-Amz-Credential") &&
  70. u.searchParams.has("X-Amz-Signature") &&
  71. u.searchParams.has("X-Amz-Algorithm")
  72. ) {
  73. // if the URL is already signed, then use it as-is
  74. url = u.origin + u.pathname;
  75. }
  76. }
  77. } catch {
  78. // ignore invalid urls
  79. }
  80. if (url.startsWith("/images")) {
  81. url = `https://www.notion.so${url}`;
  82. }
  83. url = `https://www.notion.so${
  84. url.startsWith("/image") ? url : `/image/${encodeURIComponent(url)}`
  85. }`;
  86. const notionImageUrlV2 = new URL(url);
  87. let table = block.parent_table === "space" ? "block" : block.parent_table;
  88. if (table === "collection" || table === "team") {
  89. table = "block";
  90. }
  91. notionImageUrlV2.searchParams.set("table", table);
  92. notionImageUrlV2.searchParams.set("id", block.id);
  93. notionImageUrlV2.searchParams.set("cache", "v2");
  94. url = notionImageUrlV2.toString();
  95. return url;
  96. };