|
| 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