Skip to content

Commit

Permalink
feat: Replace parseText with getTextFromBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Jul 23, 2023
1 parent 923dc6e commit 80cdba3
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 117 deletions.
4 changes: 2 additions & 2 deletions src/get-notion-object-title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
RichTextItemResponse,
} from '@notionhq/client/build/src/api-endpoints';

import { parseText } from './parse-text';
import { getTextFromBlock } from './getTextFromBlock';

export type Icon =
| {
Expand Down Expand Up @@ -75,5 +75,5 @@ export const getNotionObjectTitle = (
return `${icon}${title}`;
}

return parseText(notionObject);
return getTextFromBlock(notionObject);
};
1 change: 1 addition & 0 deletions src/getTextFromBlock.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function getTextFromBlock(input: unknown): string;
127 changes: 127 additions & 0 deletions src/getTextFromBlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* This file originates from https://github.com/makenotion/notion-sdk-js/tree/main/examples/parse-text-from-any-block-type
*
* Credit to Jess Mitchell @jessmitch42
*/

/*
---------------------------------------------------------------------------
*/
// Take rich text array from a block child that supports rich text and return the plain text.
// Note: All rich text objects include a plain_text field.
const getPlainTextFromRichText = richText => {
return richText.map(t => t.plain_text).join("")
// Note: A page mention will return "Undefined" as the page name if the page has not been shared with the integration. See: https://developers.notion.com/reference/block#mention
}

// Use the source URL and optional caption from media blocks (file, video, etc.)
const getMediaSourceText = block => {
let source, caption

if (block[block.type].external) {
source = block[block.type].external.url
} else if (block[block.type].file) {
source = block[block.type].file.url
} else if (block[block.type].url) {
source = block[block.type].url
} else {
source = "[Missing case for media block types]: " + block.type
}
// If there's a caption, return it with the source
if (block[block.type].caption.length) {
caption = getPlainTextFromRichText(block[block.type].caption)
return caption + ": " + source
}
// If no caption, just return the source URL
return source
}

// Get the plain text from any block type supported by the public API.
export const getTextFromBlock = block => {
let text

// Get rich text from blocks that support it
if (block[block.type].rich_text) {
// This will be an empty string if it's an empty line.
text = getPlainTextFromRichText(block[block.type].rich_text)
}
// Get text for block types that don't have rich text
else {
switch (block.type) {
case "unsupported":
// The public API does not support all block types yet
text = "[Unsupported block type]"
break
case "bookmark":
text = block.bookmark.url
break
case "child_database":
text = block.child_database.title
// Use "Query a database" endpoint to get db rows: https://developers.notion.com/reference/post-database-query
// Use "Retrieve a database" endpoint to get additional properties: https://developers.notion.com/reference/retrieve-a-database
break
case "child_page":
text = block.child_page.title
break
case "embed":
case "video":
case "file":
case "image":
case "pdf":
text = getMediaSourceText(block)
break
case "equation":
text = block.equation.expression
break
case "link_preview":
text = block.link_preview.url
break
case "synced_block":
// Provides ID for block it's synced with.
text = block.synced_block.synced_from
? "This block is synced with a block with the following ID: " +
block.synced_block.synced_from[block.synced_block.synced_from.type]
: "Source sync block that another blocked is synced with."
break
case "table":
// Only contains table properties.
// Fetch children blocks for more details.
text = "Table width: " + block.table.table_width
break
case "table_of_contents":
// Does not include text from ToC; just the color
text = "ToC color: " + block.table_of_contents.color
break
case "breadcrumb":
case "column_list":
case "divider":
text = "No text available"
break
default:
text = "[Needs case added]"
break
}
}
// Blocks with the has_children property will require fetching the child blocks. (Not included in this example.)
// e.g. nested bulleted lists
if (block.has_children) {
// For now, we'll just flag there are children blocks.
text = text + " (Has children)"
}
// Includes block type for readability. Update formatting as needed.
return block.type + ": " + text
}

async function retrieveBlockChildren(id) {
console.log("Retrieving blocks (async)...")
const blocks = []

// Use iteratePaginatedAPI helper function to get all blocks first-level blocks on the page
for await (const block of iteratePaginatedAPI(notion.blocks.children.list, {
block_id: id, // A page ID can be passed as a block ID: https://developers.notion.com/docs/working-with-page-content#modeling-content-as-blocks
})) {
blocks.push(block)
}

return blocks
}
1 change: 0 additions & 1 deletion src/parse-text.d.ts

This file was deleted.

114 changes: 0 additions & 114 deletions src/parse-text.js

This file was deleted.

0 comments on commit 80cdba3

Please sign in to comment.