在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):tryfabric/martian开源软件地址(OpenSource Url):https://github.com/tryfabric/martian开源编程语言(OpenSource Language):TypeScript 99.9%开源软件介绍(OpenSource Introduction):Martian: Markdown to Notion ParserConvert Markdown and GitHub Flavoured Markdown to Notion API Blocks and RichText. Martian is a Markdown parser to convert any Markdown content to Notion API block or RichText objects. It uses unified to create a Markdown AST, then converts the AST into Notion objects. Designed to make using the Notion SDK and API easier. Notion API version 1.0. Supported Markdown Elements
UsageBasic usage:The package exports two functions, which you can import like this: // JS
const {markdownToBlocks, markdownToRichText} = require('@tryfabric/martian');
// TS
import {markdownToBlocks, markdownToRichText} from '@tryfabric/martian'; Here are couple of examples with both of them: markdownToRichText(`**Hello _world_**`); Result[ { "type": "text", "annotations": { "bold": true, "strikethrough": false, "underline": false, "italic": false, "code": false, "color": "default" }, "text": { "content": "Hello " } }, { "type": "text", "annotations": { "bold": true, "strikethrough": false, "underline": false, "italic": true, "code": false, "color": "default" }, "text": { "content": "world" } } ] markdownToBlocks(`
hello _world_
***
## heading2
* [x] todo
`); Result[ { "object": "block", "type": "paragraph", "paragraph": { "rich_text": [ { "type": "text", "annotations": { "bold": false, "strikethrough": false, "underline": false, "italic": false, "code": false, "color": "default" }, "text": { "content": "hello " } }, { "type": "text", "annotations": { "bold": false, "strikethrough": false, "underline": false, "italic": true, "code": false, "color": "default" }, "text": { "content": "world" } } ] } }, { "object": "block", "type": "heading_2", "heading_2": { "rich_text": [ { "type": "text", "annotations": { "bold": false, "strikethrough": false, "underline": false, "italic": false, "code": false, "color": "default" }, "text": { "content": "heading2" } } ] } }, { "object": "block", "type": "to_do", "to_do": { "rich_text": [ { "type": "text", "annotations": { "bold": false, "strikethrough": false, "underline": false, "italic": false, "code": false, "color": "default" }, "text": { "content": "todo" } } ], "checked": true } } ] Working with Notion's limitsSometimes a Markdown input would result in an output that would be rejected by the Notion API: here are some options to deal with that. An item exceeds the children or character limitBy default, the package will try to resolve these kind of issues by re-distributing the content to multiple blocks: when that's not possible, const options = {
notionLimits: {
truncate: false,
},
};
markdownToBlocks('input', options);
markdownToRichText('input', options); Manually handling errors related to Notions's limitsYou can set a callback for when one of the resulting items would exceed Notion's limits. Please note that this function will be called regardless of whether the final output will be truncated. const options = {
notionLimits: {
// truncate: true, // by default
onError: (err: Error) => {
// Something has appened!
console.error(err);
},
},
};
markdownToBlocks('input', options);
markdownToRichText('input', options); Working with imagesIf an image as an invalid URL, the Notion API will reject the whole request: const options = {
strictImageUrls: false,
}; Default behavior: markdownToBlocks('![](InvalidURL)'); Result[ { "object": "block", "type": "paragraph", "paragraph": { "rich_text": [ { "type": "text", "annotations": { "bold": false, "strikethrough": false, "underline": false, "italic": false, "code": false, "color": "default" }, "text": { "content": "InvalidURL" } } ] } } ]
markdownToBlocks('![](InvalidURL)', {
strictImageUrls: false,
}); Result[ { "object": "block", "type": "image", "image": { "type": "external", "external": { "url": "InvalidURL" } } } ] Non-inline elements when parsing rich textBy default, if the text provided to Default behavior: markdownToRichText('# Header\nAbc', {
// nonInline: 'ignore', // Default
}); Result[ { type: 'text', annotations: { bold: false, strikethrough: false, underline: false, italic: false, code: false, color: 'default' }, text: { content: 'Abc', link: undefined } } ] Throw an error: markdownToRichText('# Header\nAbc', {
nonInline: 'throw',
}); ResultError: Unsupported markdown element: {"type":"heading","depth":1,"children":[{"type":"text","value":"Header","position":{"start":{"line":1,"column":3, "offset":2},"end":{"line":1,"column":9,"offset":8}}}],"position":{"start":{"line":1,"column":1,"offset":0},"end":{"line":1,"column":9,"offset":8}}} Built with |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论