Skip to content

Commit dfae74c

Browse files
authored
feat: add support for thematic breaks/dividers in markdown parsing (#61)
* 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. * test: add support for thematic breaks/dividers in markdown parsing
1 parent 8cfccaa commit dfae74c

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

src/notion/blocks.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ export type RichText = (Block & {
1212
type: 'paragraph';
1313
})['paragraph']['rich_text'][number];
1414

15+
export function divider(): Block {
16+
return {
17+
object: 'block',
18+
type: 'divider',
19+
divider: {},
20+
};
21+
}
22+
1523
export function paragraph(text: RichText[]): Block {
1624
return {
1725
object: 'block',

src/parser/internal.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ function parseNode(
241241
case 'math':
242242
return [parseMath(node)];
243243

244+
case 'thematicBreak':
245+
return [notion.divider()];
246+
244247
default:
245248
return [];
246249
}

test/fixtures/divider.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Thematic Break
2+
3+
***
4+
5+
Divider
6+
7+
---
8+
9+
END

test/integration.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ hello _world_
1919
notion.richText('hello '),
2020
notion.richText('world', {annotations: {italic: true}}),
2121
]),
22+
notion.divider(),
2223
notion.headingTwo([notion.richText('heading2')]),
2324
notion.toDo(true, [notion.richText('todo')]),
2425
];
@@ -93,6 +94,21 @@ const hello = "hello";
9394
expect(actual).toStrictEqual(expected);
9495
});
9596

97+
it('should deal with divider', () => {
98+
const text = fs.readFileSync('test/fixtures/divider.md').toString();
99+
const actual = markdownToBlocks(text);
100+
101+
const expected = [
102+
notion.paragraph([notion.richText('Thematic Break')]),
103+
notion.divider(),
104+
notion.paragraph([notion.richText('Divider')]),
105+
notion.divider(),
106+
notion.paragraph([notion.richText('END')]),
107+
];
108+
109+
expect(actual).toStrictEqual(expected);
110+
});
111+
96112
it('should break up large elements', () => {
97113
const text = fs.readFileSync('test/fixtures/large-item.md').toString();
98114
const actual = markdownToBlocks(text);

test/parser.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe('gfm parser', () => {
7979

8080
const expected = [
8181
notion.paragraph([notion.richText('hello')]),
82+
notion.divider(),
8283
notion.paragraph([notion.richText('world')]),
8384
];
8485

0 commit comments

Comments
 (0)