Skip to content

Commit

Permalink
[render] BREAKING CHANGE: Input string into format date fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Xennis committed Jul 1, 2024
1 parent 1971a4c commit da4daef
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions examples/nextjs/src/app/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ export default async function SlugPage({ params }: { params: { slug: string } })
blocks={blocks}
options={{
// Example customization: Render dates as German dates
formatDateFn: (date: Date) => {
return date.toLocaleDateString("de", {
formatDateFn: (dateString: string) => {
return new Date(dateString).toLocaleDateString("de", {
month: "long",
day: "numeric",
year: "numeric",
})
},
// Example resolving links to other Notion pages
resolveLinkFn: (nId) => {
const page = pages.find((p) => p.blockId === nId)
resolveLinkFn: (pageId) => {
const page = pages.find((p) => p.blockId === pageId)
if (page === undefined) {
return null
}
Expand Down
8 changes: 4 additions & 4 deletions packages/render/src/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import { PageTitle } from "./components/page-title"
import { Code } from "./components/html/code"
import { Img } from "./components/html/img"

const defaultFormatDateFn = (date: Date) => date.toString()
const defaultResolveLinkFn = (nId: string) => null
const defaultFormatDateFn = (dateString: string) => new Date(dateString).toString()
const defaultResolveLinkFn = (pageId: string) => null

export const Render = ({
blocks,
options,
}: {
blocks: Array<BlockObjectResponseWithChildren>
options?: {
formatDateFn?: (date: Date) => string
resolveLinkFn?: (nId: string) => { href: string; icon: IconResponse | null } | null
formatDateFn?: (dateString: string) => string
resolveLinkFn?: (pageId: string) => { href: string; icon: IconResponse | null } | null
htmlComponents?: {
a?: (props: React.ComponentPropsWithoutRef<"a">) => JSX.Element
code?: (props: React.ComponentPropsWithoutRef<"code">) => JSX.Element
Expand Down
8 changes: 4 additions & 4 deletions packages/render/src/components/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
TextRichTextItemResponse,
} from "@notionhq/client/build/src/api-endpoints"

import { cn, notionColor, notionLinkToUuid, notionUrl } from "../util"
import { cn, notionColor, notionLinkToUuid } from "../util"
import { RenderOptions } from "../types"
import { PageTitle } from "./page-title"

Expand Down Expand Up @@ -109,15 +109,15 @@ const FormattedText = ({
)
}

const mentionText = (value: MentionRichTextItemResponse, formatDateFn: (date: Date) => string) => {
const mentionText = (value: MentionRichTextItemResponse, formatDateFn: (dateString: string) => string) => {
switch (value.mention.type) {
case "database":
return null
case "date":
try {
let dateText = formatDateFn(new Date(value.mention.date.start))
let dateText = formatDateFn(value.mention.date.start)
if (value.mention.date.end) {
dateText += ` → ${formatDateFn(new Date(value.mention.date.end))}`
dateText += ` → ${formatDateFn(value.mention.date.end)}`
}
// Ignored: value.mention.date.time_zone
return dateText
Expand Down
4 changes: 2 additions & 2 deletions packages/render/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export type BlockObjectResponseWithChildren = BlockObjectResponse & {
export type IconResponse = PageObjectResponse["icon"]

export type RenderOptions = {
formatDateFn: (date: Date) => string
resolveLinkFn: (nId: string) => { href: string; icon: IconResponse | null } | null
formatDateFn: (dateString: string) => string
resolveLinkFn: (pageId: string) => { href: string; icon: IconResponse | null } | null
htmlComponents: {
a: (props: React.ComponentPropsWithoutRef<"a">) => JSX.Element
code: (props: React.ComponentPropsWithoutRef<"code">) => JSX.Element
Expand Down

0 comments on commit da4daef

Please sign in to comment.