Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/nreact/src/context.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'

import { defaultMapImageUrl, defaultMapPageUrl } from '@texonom/nutils'
import { defaultMapImageUrl, defaultMapPageUrl, parsePageId } from '@texonom/nutils'

import { AssetWrapper } from './components/asset-wrapper'
import { Checkbox as DefaultCheckbox } from './components/checkbox'
Expand Down Expand Up @@ -75,7 +75,22 @@ export interface PartialNotionContext {

const DefaultLink: React.FC = props => <a target='_blank' rel='noopener noreferrer' {...props} />
const DefaultLinkMemo = React.memo(DefaultLink)
const DefaultPageLink: React.FC = props => <a {...props} />
const DefaultPageLink: React.FC<React.AnchorHTMLAttributes<HTMLAnchorElement>> = ({ href, ...props }) => {
const { recordMap, rootSpaceId } = useNotionContext()
let finalHref = href as string | undefined

if (typeof href === 'string' && rootSpaceId) {
const pageId = parsePageId(href)

if (pageId) {
const block = recordMap?.block?.[pageId]?.value

if (!block || block.space_id !== rootSpaceId) finalHref = `https://notion.so/${pageId.replace(/-/g, '')}`
}
}

return <a href={finalHref} {...props} />
}
const DefaultPageLinkMemo = React.memo(DefaultPageLink)

const DefaultEmbed = props => <AssetWrapper {...props} />
Expand Down
Loading