Skip to content

Commit 61d47ee

Browse files
committed
fix: Handle simple numbered lists
This works around a bug in notion-to-md and allows us to handle simple numbered lists. It will not handle lists that are continued after other content.
1 parent 52ece8d commit 61d47ee

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/CalloutTransformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ type Annotations = {
7979
| "pink_background"
8080
| "red_background";
8181
};
82-
type Text = {
82+
export type Text = {
8383
type: "text";
8484
text: {
8585
content: string;

src/CustomTranformers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ListBlockChildrenResponseResults,
88
} from "notion-to-md/build/types";
99
import { notionCalloutToAdmonition } from "./CalloutTransformer";
10+
import { numberedListTransformer } from "./NumberedListTransforer";
1011

1112
export function setupCustomTransformers(
1213
notionToMarkdown: NotionToMarkdown,
@@ -90,6 +91,12 @@ export function setupCustomTransformers(
9091
notionCalloutToAdmonition(notionToMarkdown, notionClient, block)
9192
);
9293

94+
notionToMarkdown.setCustomTransformer(
95+
"numbered_list_item",
96+
(block: ListBlockChildrenResponseResult) =>
97+
numberedListTransformer(notionToMarkdown, notionClient, block)
98+
);
99+
93100
// Note: Pull.ts also adds an image transformer, but has to do that for each
94101
// page so we don't do it here.
95102
}

src/NumberedListTransforer.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { NotionToMarkdown } from "notion-to-md";
2+
import { ListBlockChildrenResponseResult } from "notion-to-md/build/types";
3+
import { Client } from "@notionhq/client";
4+
import { Text } from "./CalloutTransformer";
5+
6+
// This is mostly what notion-to-markdown would normally do with a block of type
7+
// numbered_list_item. A patch is documented at the end.
8+
export function numberedListTransformer(
9+
notionToMarkdown: NotionToMarkdown,
10+
notionClient: Client,
11+
block: ListBlockChildrenResponseResult
12+
): Promise<string> {
13+
//console.log("got numbered list block " + JSON.stringify(block));
14+
// In this case typescript is not able to index the types properly, hence ignoring the error
15+
// @ts-ignore
16+
const blockContent =
17+
// @ts-ignore
18+
block.numbered_list_item?.text || block.numbered_list_item?.rich_text || [];
19+
let parsedData = "";
20+
blockContent.map((content: Text) => {
21+
const annotations = content.annotations;
22+
let plain_text = content.plain_text;
23+
24+
plain_text = notionToMarkdown.annotatePlainText(plain_text, annotations);
25+
26+
// if (content["href"])
27+
// plain_text = md.link(plain_text, content["href"]);
28+
29+
parsedData += plain_text;
30+
});
31+
32+
// There is code in notion-to-md which attempts to set an incrementing number
33+
// on each of these. Somehow it fails; in my testing, block.numbered_list_item never
34+
// has a field 'number'. But we don't actually need incrementing numbers;
35+
// markdown will do the numbering if we just make something that looks like
36+
// a member of a numbered list by starting with number followed by period and space.
37+
// I'm keeping the original code in case notion-to-md gets fixed and there is actually
38+
// some reason to use incrementing numbers (it would at least make the markdown more
39+
// human-readable); but this at least works.
40+
// A problem is that in notion, a numbered list may continue after some intermediate
41+
// content. To achieve this in markdown, we'd need to indent the intermediate content
42+
// by a tab. Not only is it difficult to do this, but there appears to be no way to
43+
// know whether we should. The data we get from notion doesn't include the item number,
44+
// and its parent is the page rather than a particular list. So there is no way I can
45+
// see to distinguish a list continuation from a new list. The code here will leave
46+
// it up to markdown to decide whether to start a new list; I believe it will do so
47+
// if it sees any intervening lines that are not list items.
48+
let num = block.numbered_list_item?.number;
49+
//console.log("got number " + num?.toString());
50+
if (!num) {
51+
num = 1;
52+
}
53+
return Promise.resolve(`${num}. ${parsedData.trim()}`);
54+
}

0 commit comments

Comments
 (0)