From 9561de999be3e42cd216e6fec1a678c69d997cf8 Mon Sep 17 00:00:00 2001 From: SengMitnick Date: Mon, 24 Jun 2024 16:05:24 +0800 Subject: [PATCH] feat: add support for thematic breaks/dividers in markdown parsing This commit adds support for thematic breaks/dividers in the markdown parsing functionality. It introduces a new function `divider()` in the `blocks.ts` file, which creates a divider block. Additionally, the `parseNode()` function in the `internal.ts` file has been updated to handle the `thematicBreak` node type and convert it into a divider block. --- src/notion/blocks.ts | 8 ++++++++ src/parser/internal.ts | 3 +++ test/fixtures/divider.md | 9 +++++++++ test/integration.spec.ts | 15 +++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 test/fixtures/divider.md diff --git a/src/notion/blocks.ts b/src/notion/blocks.ts index 01e9c4f..03391af 100644 --- a/src/notion/blocks.ts +++ b/src/notion/blocks.ts @@ -12,6 +12,14 @@ export type RichText = (Block & { type: 'paragraph'; })['paragraph']['rich_text'][number]; +export function divider(): Block { + return { + object: 'block', + type: 'divider', + divider: {}, + }; +} + export function paragraph(text: RichText[]): Block { return { object: 'block', diff --git a/src/parser/internal.ts b/src/parser/internal.ts index 4f879eb..4d565fc 100644 --- a/src/parser/internal.ts +++ b/src/parser/internal.ts @@ -241,6 +241,9 @@ function parseNode( case 'math': return [parseMath(node)]; + case 'thematicBreak': + return [notion.divider()]; + default: return []; } diff --git a/test/fixtures/divider.md b/test/fixtures/divider.md new file mode 100644 index 0000000..14dddf2 --- /dev/null +++ b/test/fixtures/divider.md @@ -0,0 +1,9 @@ +Thematic Break + +*** + +Divider + +--- + +END \ No newline at end of file diff --git a/test/integration.spec.ts b/test/integration.spec.ts index a971d4d..ca0c2ec 100644 --- a/test/integration.spec.ts +++ b/test/integration.spec.ts @@ -92,6 +92,21 @@ const hello = "hello"; expect(actual).toStrictEqual(expected); }); + + it('should deal with divider', () => { + const text = fs.readFileSync('test/fixtures/divider.md').toString(); + const actual = markdownToBlocks(text); + + const expected = [ + notion.paragraph([notion.richText('Thematic Break')]), + notion.divider(), + notion.paragraph([notion.richText('Divider')]), + notion.divider(), + notion.paragraph([notion.richText('END')]), + ]; + + expect(actual).toStrictEqual(expected); + }); it('should break up large elements', () => { const text = fs.readFileSync('test/fixtures/large-item.md').toString();