Skip to content

Commit

Permalink
feat: add support for thematic breaks/dividers in markdown parsing (#61)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sengmitnick authored Aug 1, 2024
1 parent 8cfccaa commit dfae74c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/notion/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions src/parser/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ function parseNode(
case 'math':
return [parseMath(node)];

case 'thematicBreak':
return [notion.divider()];

default:
return [];
}
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/divider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Thematic Break

***

Divider

---

END
16 changes: 16 additions & 0 deletions test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ hello _world_
notion.richText('hello '),
notion.richText('world', {annotations: {italic: true}}),
]),
notion.divider(),
notion.headingTwo([notion.richText('heading2')]),
notion.toDo(true, [notion.richText('todo')]),
];
Expand Down Expand Up @@ -93,6 +94,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();
const actual = markdownToBlocks(text);
Expand Down
1 change: 1 addition & 0 deletions test/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('gfm parser', () => {

const expected = [
notion.paragraph([notion.richText('hello')]),
notion.divider(),
notion.paragraph([notion.richText('world')]),
];

Expand Down

0 comments on commit dfae74c

Please sign in to comment.