Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
305 changes: 279 additions & 26 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"remark-gfm": "^1.0.0",
"remark-math": "^4.0.0",
"remark-parse": "^9.0.0",
"remark-footnotes": "^3.0.0",
"unified": "^9.2.1"
},
"devDependencies": {
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import type * as md from './markdown';
import gfm from 'remark-gfm';
import remarkMath from 'remark-math';
import footnotes from 'remark-footnotes';

/**
* Parses Markdown content into Notion Blocks.
Expand All @@ -22,7 +23,12 @@ export function markdownToBlocks(

options?: BlocksOptions,
): notion.Block[] {
const root = unified().use(markdown).use(gfm).use(remarkMath).parse(body);
const root = unified()
.use(markdown)
.use(gfm)
.use(remarkMath)
.use(footnotes)
.parse(body);
return parseBlocks(root as unknown as md.Root, options);
}

Expand All @@ -37,6 +43,6 @@ export function markdownToRichText(
text: string,
options?: RichTextOptions,
): notion.RichText[] {
const root = unified().use(markdown).use(gfm).parse(text);
const root = unified().use(markdown).use(gfm).use(footnotes).parse(text);
return parseRichText(root as unknown as md.Root, options);
}
20 changes: 20 additions & 0 deletions src/markdown/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import type {
TableContent,
Text,
ThematicBreak,
FootnoteReference,
FootnoteDefinition,
} from './types';

export function text(value: string): Text {
Expand Down Expand Up @@ -191,3 +193,21 @@ export function tableCell(...children: PhrasingContent[]): RowContent {
children: children,
};
}

export function footnoteReference(identifier: string): FootnoteReference {
return {
type: 'footnoteReference',
identifier,
};
}

export function footnoteDefinition(
identifier: string,
...children: FlowContent[]
): FootnoteDefinition {
return {
type: 'footnoteDefinition',
identifier,
children,
};
}
18 changes: 17 additions & 1 deletion src/markdown/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export type FlowContent =
| Image
| ImageReference
| ThematicBreak
| FootnoteDefinition
| Content
| Table
| Math;
Expand All @@ -162,7 +163,11 @@ export type Content = Definition | Paragraph;

export type ListContent = ListItem;

export type PhrasingContent = Link | LinkReference | StaticPhrasingContent;
export type PhrasingContent =
| Link
| LinkReference
| StaticPhrasingContent
| FootnoteReference;

export type StaticPhrasingContent =
| Image
Expand All @@ -179,3 +184,14 @@ export type StaticPhrasingContent =
export type TableContent = TableRow;

export type RowContent = TableCell;

export interface FootnoteReference extends Node {
type: 'footnoteReference';
identifier: string;
}

export interface FootnoteDefinition extends Parent {
type: 'footnoteDefinition';
identifier: string;
children: FlowContent[];
}
16 changes: 16 additions & 0 deletions src/parser/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ function parseInline(
case 'inlineMath':
return [notion.richText(element.value, {...copy, type: 'equation'})];

case 'footnoteReference': {
const ref = element as md.FootnoteReference;
return [notion.richText(`[${ref.identifier}]`, copy)];
}

default:
return [];
}
Expand Down Expand Up @@ -329,6 +334,17 @@ function parseNode(
case 'math':
return [parseMath(node)];

case 'footnoteDefinition': {
const id = node.identifier;
const content: notion.RichText[] = [];
node.children.forEach(c => {
if (c.type === 'paragraph')
c.children.forEach(ch => content.push(...parseInline(ch)));
});

return [notion.paragraph([notion.richText(`${id}. `), ...content])];
}

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

Expand Down
19 changes: 19 additions & 0 deletions test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,25 @@ const hello = "hello";

expect(actual).toStrictEqual(expected);
});

it('should parse footnotes', () => {
const text = 'Here is some text.[^1]\n\n[^1]: Footnote definition.';

const actual = markdownToBlocks(text);

const expected = [
notion.paragraph([
notion.richText('Here is some text.'),
notion.richText('[1]'),
]),
notion.paragraph([
notion.richText('1. '),
notion.richText('Footnote definition.'),
]),
];

expect(actual).toStrictEqual(expected);
});
});

describe('markdownToRichText', () => {
Expand Down
19 changes: 19 additions & 0 deletions test/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,23 @@ describe('gfm parser', () => {

expect(actual).toStrictEqual(expected);
});

it('should parse footnotes', () => {
const ast = md.root(
md.paragraph(md.text('Hello'), md.footnoteReference('1')),
md.footnoteDefinition('1', md.paragraph(md.text('Footnote content'))),
);

const actual = parseBlocks(ast, options);

const expected = [
notion.paragraph([notion.richText('Hello'), notion.richText('[1]')]),
notion.paragraph([
notion.richText('1. '),
notion.richText('Footnote content'),
]),
];

expect(actual).toStrictEqual(expected);
});
});