Skip to content
Open
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion packages/notion-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"test:unit": "vitest run"
},
"dependencies": {
"is-url-superb": "catalog:",
"memoize": "catalog:",
"normalize-url": "catalog:",
"notion-types": "workspace:*",
Expand Down
3 changes: 1 addition & 2 deletions packages/notion-utils/src/get-page-image-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type * as types from 'notion-types'

import { getBlockIcon } from './get-block-icon'
import { getBlockValue } from './get-block-value'
import { isUrl } from './is-url'

/**
* Gets URLs of all images contained on the given page.
Expand Down Expand Up @@ -65,7 +64,7 @@ export const getPageImageUrls = (
}

const pageIcon = getBlockIcon(block, recordMap)
if (pageIcon && isUrl(pageIcon)) {
if (pageIcon && URL.canParse(pageIcon)) {
images.push({
block,
url: pageIcon
Expand Down
1 change: 0 additions & 1 deletion packages/notion-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export * from './get-page-tweet-urls'
export * from './get-text-content'
export * from './group-block-content'
export * from './id-to-uuid'
export * from './is-url'
export * from './map-image-url'
export * from './map-page-url'
export * from './merge-record-maps'
Expand Down
1 change: 0 additions & 1 deletion packages/notion-utils/src/is-url.ts

This file was deleted.

4 changes: 3 additions & 1 deletion packages/notion-x-to-md/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"test:unit": "vitest run"
},
"dependencies": {
"commander": "^14.0.3",
"date-fns": "catalog:",
"format-number": "catalog:",
"notion-client": "workspace:*",
Expand All @@ -37,6 +36,9 @@
"p-map": "catalog:",
"tweet-to-md": "^1.0.5"
},
"devDependencies": {
"@types/node": "catalog:"
},
"keywords": [
"notion",
"md",
Expand Down
45 changes: 32 additions & 13 deletions packages/notion-x-to-md/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
#!/usr/bin/env node
import { program } from 'commander'
import { parseArgs } from 'node:util'

import { NotionAPI } from 'notion-client'

import { notionPageToMarkdown } from './notion-page-to-markdown'

program
.name('notion-x-to-md')
.description('Converts a Notion page to Markdown')
.argument('<page>', 'Notion page ID or URL (must be publicly accessible)')
.action(async (page: string) => {
const api = new NotionAPI()
const recordMap = await api.getPage(page)
const markdown = await notionPageToMarkdown(recordMap)
console.log(markdown)
})

await program.parseAsync()
const { values, positionals } = parseArgs({
options: {
help: {
type: 'boolean',
short: 'h'
}
},
allowPositionals: true
})

if (values.help) {
console.log(`notion-x-to-md Converts a Notion page to Markdown

notion-x-to-md <page>
<page> Notion page ID or URL (must be publicly accessible)
`)
process.exit(0)
}

const page = positionals.at(0)

if (!page) {
console.error('No page specified')
process.exit(1)
}

const api = new NotionAPI()
const recordMap = await api.getPage(page)
const markdown = await notionPageToMarkdown(recordMap)
console.log(markdown)
4 changes: 2 additions & 2 deletions packages/notion-x-to-md/src/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type ExtendedRecordMap,
type PageBlock
} from 'notion-types'
import { getBlockIcon, isUrl } from 'notion-utils'
import { getBlockIcon } from 'notion-utils'

const isIconBlock = (value: Block): value is PageBlock | CalloutBlock => {
return (
Expand All @@ -26,7 +26,7 @@ export function getIcon(
return '📁'
}

if (isUrl(icon) || icon.startsWith('/icons/')) {
if (URL.canParse(icon) || icon.startsWith('/icons/')) {
return '❔'
}

Expand Down
4 changes: 3 additions & 1 deletion packages/notion-x-to-md/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"extends": "../../tsconfig",
"compilerOptions": {
"outDir": "build"
"outDir": "build",
"types": ["node"],
"module": "esnext"
},
"include": ["src", "*.config.ts"]
}
1 change: 0 additions & 1 deletion packages/react-notion-x/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
"@types/react": "catalog:",
"@types/react-dom": "catalog:",
"@types/react-modal": "catalog:",
"clipboard-copy": "catalog:",
"date-fns": "catalog:",
"format-number": "catalog:",
"lodash.throttle": "catalog:",
Expand Down
4 changes: 2 additions & 2 deletions packages/react-notion-x/src/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { TabBlock } from './components/tab-block'
import { Text } from './components/text'
import { useNotionContext } from './context'
import { LinkIcon } from './icons/link-icon'
import { cs, isUrl } from './utils'
import { cs } from './utils'

export interface BlockProps {
block: types.Block
Expand Down Expand Up @@ -142,7 +142,7 @@ export function Block(props: BlockProps) {
}

const pageIcon = getBlockIcon(block, recordMap) ?? defaultPageIcon
const isPageIconUrl = pageIcon && isUrl(pageIcon)
const isPageIconUrl = pageIcon && URL.canParse(pageIcon)

const toc = getPageTableOfContents(
block as types.PageBlock,
Expand Down
16 changes: 1 addition & 15 deletions packages/react-notion-x/src/components/asset-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function AssetWrapper({
const id = parsePageId(caption, { uuid: true })

const isPage = caption.charAt(0) === '/' && id
if (isPage || isValidURL(caption)) {
if (isPage || URL.canParse(caption)) {
isURL = true
}
}
Expand Down Expand Up @@ -78,20 +78,6 @@ export function AssetWrapper({
return figure
}

function isValidURL(str: string) {
// TODO: replace this with a more well-tested package
const pattern = new RegExp(
'^(https?:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
'(\\#[-a-z\\d_]*)?$',
'i'
)
return !!pattern.test(str)
}

function extractHostname(url?: string) {
try {
const hostname = new URL(url!).hostname
Expand Down
4 changes: 2 additions & 2 deletions packages/react-notion-x/src/components/page-icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React from 'react'

import { useNotionContext } from '../context'
import { DefaultPageIcon } from '../icons/default-page-icon'
import { cs, isUrl } from '../utils'
import { cs } from '../utils'
import { LazyImage } from './lazy-image'

const isIconBlock = (value: Block): value is PageBlock | CalloutBlock => {
Expand Down Expand Up @@ -37,7 +37,7 @@ export function PageIconImpl({
const icon = getBlockIcon(block, recordMap)?.trim() || defaultIcon
const title = getBlockTitle(block, recordMap)

if (icon && isUrl(icon)) {
if (icon && URL.canParse(icon)) {
const url = mapImageUrl(icon, block)
isImage = true

Expand Down
5 changes: 2 additions & 3 deletions packages/react-notion-x/src/third-party/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'prismjs/components/prism-jsx.min.js'
import 'prismjs/components/prism-tsx.min.js'
import 'prismjs/components/prism-typescript.min.js'

import copyToClipboard from 'clipboard-copy'
import { type CodeBlock } from 'notion-types'
import { getBlockTitle } from 'notion-utils'
// eslint-disable-next-line import/no-duplicates, no-duplicate-imports
Expand Down Expand Up @@ -62,8 +61,8 @@ export function Code({
}
}, [codeRef])

const onClickCopyToClipboard = React.useCallback(() => {
void copyToClipboard(content)
const onClickCopyToClipboard = React.useCallback(async () => {
await navigator.clipboard.writeText(content)
setIsCopied(true)

if (copyTimeout.current) {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-notion-x/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { formatDate, formatNotionDateTime, isUrl } from 'notion-utils'
export { formatDate, formatNotionDateTime } from 'notion-utils'

export const cs = (...classes: Array<string | undefined | false>) =>
classes.filter((a) => !!a).join(' ')
Expand Down
Loading