-
-
Notifications
You must be signed in to change notification settings - Fork 91
API
config
contains options for extended functionalities.
config:{
separateChildPage: boolean, // default: false
parseChildPages: boolean // default: true
}
-
separateChildPage
: enable to separate child page content from the parent page content -
parseChildPages
: disable to ignore child pages during fetching and parsing
- takes the output of
pageToMarkdown
orblocksToMarkdown
as an argument - converts to markdown string of object
-
parent
property contains the target page markdown string. - if
separateChildPage
is enabled inconfig
then all the child pages content are added corresponding to their title in the same object.
for (separateChildPage=false
):
output:
for (separateChildPage=true
):
output:
- Uses
blocksToMarkdown
internally. -
id
(pageid) as input and converts all the blocks in the page to corresponding markdown object -
totalPage
is the retrieve block children request number i.epage_size Maximum = totalPage * 100
.
Notion API allows to fetch up to 100 children on a page. Total page stands for the number of pages required to get all the child blocks on a page where each page contains <= 100 blocks.
The default value is null
which means all the blocks of the page will be converted to markdown, so one can choose to not pass any value
as totalPage.
How to use
totalPage
arg ?
if the number of blocks in a notion page is indefinite or the requirement is to convert the whole notion page to markdown, then no need to pass
totalPage
as argument.if the requirement is to convert the first
100
blocks of the page to markdown then usetotalPage
as1
.if the notion page contains
150
blocks thentotalPage
argument should be greater than or equal to2
leading topageSize = 2 * 100
and rendering all150
blocks.
Example:
// converting whole page to markdown
const x = await n2m.pageToMarkdown("target_page_id");
//or
const y = await n2m.pageToMarkdown("target_page_id",null);
// converting first 100 blocks to markdown.
const z = await n2m.pageToMarkdown("target_page_id",1);
-
blocks
: array of notion blocks -
totalPage
: the retrieve block children request number i.epage_size Maximum = totalPage * 100
. - deals with nested blocks
- uses
blockToMarkdown
internally.
- Takes single notion block and converts to markdown string
- does not deal with nested notion blocks
- Refer docs to know more about notion blocks
You can define your own custom transformer for a notion type, to parse and return your own string.
setCustomTransformer(type, func)
will overload the parsing for the giving type.
const { NotionToMarkdown } = require("notion-to-md");
const n2m = new NotionToMarkdown({ notionClient: notion });
n2m.setCustomTransformer('embed', async (block) => {
const {embed} = block as any;
if (!embed?.url) return '';
return `<figure>
<iframe src="${embed?.url}"></iframe>
<figcaption>${await n2m.blockToMarkdown(embed?.caption)}</figcaption>
</figure>`;
});
const result = n2m.blockToMarkdown(block);
// Result will now parse the `embed` type with your custom function.
Note: Be aware that setCustomTransformer
will take only the last function for the given type. You can't set two different transforms for the same type.