markdownToBlocks
markdownToBlocks is a utility function that converts a markdown string into an array of Notion block objects.
This allows you to easily transform markdown content into a format suitable for appendBlockChildren or page creation in Notion.
Parameters
| Name | Type | Description |
|---|---|---|
markdown |
string |
The markdown text to convert. |
Returns
An Array of Notion block objects representing the parsed markdown structure.
Each block can be used directly with appendBlockChildren or similar NotionBridge functions.
Supported Markdown Features
- Headings (
#,##,###) - Paragraphs
- Bulleted lists (
-,*,+) - Numbered lists (
1.,1)) - To-do lists (
- [ ],- [x]) - Blockquotes (
>) - Horizontal rules (
---) - Code blocks (
```with language detection) - Inline code (
`code`) - Bold (
**bold**,__bold__) - Italic (
*italic*,_italic_) - Links (
[text](url)) - Images (
) - Tables (GitHub-flavored markdown)
Example
const markdown = `
# Project Overview
This is a **bold** paragraph with a [link](https://example.com).
- [x] Write docs
- [ ] Add more features
## Code Example
\`\`\`js
console.log("Hello, world!");
\`\`\`
| Name | Status |
|--------|-----------|
| Task 1 | Complete |
| Task 2 | Incomplete|
`;
const blocks = markdownToBlocks(markdown);
await notion.appendBlockChildren('parentBlockId', blocks);
Notes
- Only basic markdown features are supported (no nested lists, no advanced table features).
- Inline formatting (bold, italic, code, links) is supported inside paragraphs, headings, and list items.
- Images are added as Notion image blocks with external URLs.
- Tables are converted to Notion table blocks (if at least header and one row are present).
- Code blocks use language detection for Notion's code block type (see
languageMapin the source).
Example Output
[
{
"object": "block",
"type": "heading_1",
"heading_1": {
"rich_text": [
{ "type": "text", "text": { "content": "Project Overview" } }
]
}
},
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [
{ "type": "text", "text": { "content": "This is a " } },
{ "type": "text", "text": { "content": "bold" }, "annotations": { "bold": true } },
{ "type": "text", "text": { "content": " paragraph with a " } },
{ "type": "text", "text": { "content": "link" }, "href": "https://example.com" },
{ "type": "text", "text": { "content": "." } }
]
}
},
{
"object": "block",
"type": "to_do",
"to_do": {
"rich_text": [
{ "type": "text", "text": { "content": "Write docs" } }
],
"checked": true
}
},
{
"object": "block",
"type": "to_do",
"to_do": {
"rich_text": [
{ "type": "text", "text": { "content": "Add more features" } }
],
"checked": false
}
},
{
"object": "block",
"type": "heading_2",
"heading_2": {
"rich_text": [
{ "type": "text", "text": { "content": "Code Example" } }
]
}
},
{
"object": "block",
"type": "code",
"code": {
"rich_text": [
{ "type": "text", "text": { "content": "console.log(\"Hello, world!\");" } }
],
"language": "javascript"
}
},
{
"object": "block",
"type": "table",
"table": {
"table_width": 2,
"has_column_header": true,
"has_row_header": false,
"children": [
{
"object": "block",
"type": "table_row",
"table_row": {
"cells": [
[{ "type": "text", "text": { "content": "Name" } }],
[{ "type": "text", "text": { "content": "Status" } }]
]
}
},
{
"object": "block",
"type": "table_row",
"table_row": {
"cells": [
[{ "type": "text", "text": { "content": "Task 1" } }],
[{ "type": "text", "text": { "content": "Complete" } }]
]
}
},
{
"object": "block",
"type": "table_row",
"table_row": {
"cells": [
[{ "type": "text", "text": { "content": "Task 2" } }],
[{ "type": "text", "text": { "content": "Incomplete" } }]
]
}
}
]
}
}
]