Skip to content

Commit fa5d54e

Browse files
authored
Merge pull request #11 from JohnThomson/newlineInTable
Handle newlines in notion table cells
2 parents 27e5da8 + b96a370 commit fa5d54e

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ sample_img/
44
i18n/
55
node_modules/
66
version.json
7+
docs/

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@
5252
"file-type": "16.5.1",
5353
"fs-extra": "^10.1.0",
5454
"limiter": "^2.1.0",
55+
"markdown-table": "^2.0.0",
5556
"node-fetch": "2.6.6",
5657
"notion-to-md": "^2.5.2",
5758
"path": "^0.12.7",
5859
"postinstall-postinstall": "^2.1.0",
5960
"sanitize-filename": "^1.6.3"
6061
},
6162
"devDependencies": {
63+
"@types/markdown-table": "^2.0.0",
6264
"@types/fs-extra": "^9.0.13",
6365
"@types/jest": "^28.1.6",
6466
"@types/node": "^12.20.11",

src/CustomTranformers.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Client } from "@notionhq/client";
22
import { ListBlockChildrenResponse } from "@notionhq/client/build/src/api-endpoints";
33
import { NotionToMarkdown } from "notion-to-md";
4+
import markdownTable from "markdown-table";
45
import {
56
ListBlockChildrenResponseResult,
67
ListBlockChildrenResponseResults,
@@ -22,6 +23,59 @@ export function setupCustomTransformers(
2223
notionColumnToMarkdown(notionToMarkdown, notionClient, block)
2324
);
2425

26+
// This is mostly a copy of the table handler from notion-to-md. The change is to handle newlines in the
27+
// notion cell content.
28+
notionToMarkdown.setCustomTransformer(
29+
"table",
30+
async (block: ListBlockChildrenResponseResult) => {
31+
const { id, has_children } = block as any;
32+
const tableArr: string[][] = [];
33+
if (has_children) {
34+
const tableRows = await getBlockChildren(notionClient, id, 100);
35+
// console.log(">>", tableRows);
36+
const rowsPromise = tableRows?.map(async row => {
37+
const { type } = row as any;
38+
const cells = (row as any)[type]["cells"];
39+
40+
/**
41+
* this is more like a hack since matching the type text was
42+
* difficult. So converting each cell to paragraph type to
43+
* reuse the blockToMarkdown function
44+
*/
45+
const cellStringPromise = cells.map(
46+
async (cell: any) =>
47+
await notionToMarkdown.blockToMarkdown({
48+
type: "paragraph",
49+
paragraph: { rich_text: cell },
50+
} as ListBlockChildrenResponseResult)
51+
);
52+
53+
const cellStringArrRaw: string[] = await Promise.all(
54+
cellStringPromise
55+
);
56+
// This is our patch to the original notion-to-md code.
57+
const cellStringArr = cellStringArrRaw.map(c =>
58+
c
59+
// Trailing newlines are almost certainly not wanted, and converting to br's gives weird results
60+
.replace(/[\r\n]+$/, "")
61+
// Preserving line breaks within cells can't be done in stock markdown. Since we're producing
62+
// mdx, which supports embedded HTML, we can handle it with <br/>.
63+
// I'm not sure exactly what line breaks might occur in the input, depending on platform,
64+
// so handle all the common cases.
65+
.replaceAll("\r\n", "<br/>")
66+
.replaceAll("\n", "<br/>")
67+
.replaceAll("\r", "<br/>")
68+
);
69+
// console.log("~~", cellStringArr);
70+
tableArr.push(cellStringArr);
71+
// console.log(tableArr);
72+
});
73+
await Promise.all(rowsPromise || []);
74+
}
75+
return markdownTable(tableArr);
76+
}
77+
);
78+
2579
// Note: Pull.ts also adds an image transformer, but has to do that for each
2680
// page so we don't do it here.
2781
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,11 @@
12721272
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz"
12731273
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==
12741274

1275+
"@types/markdown-table@^2.0.0":
1276+
version "2.0.0"
1277+
resolved "https://registry.yarnpkg.com/@types/markdown-table/-/markdown-table-2.0.0.tgz#d2a3458c61ee71c8ee2b40b76c199b85b8dbd70c"
1278+
integrity sha512-fVZN/DRjZvjuk+lo7ovlI/ZycS51gpYU5vw5EcFeqkcX6lucQ+UWgEOH2O4KJHkSck4DHAY7D7CkVLD0wzc5qw==
1279+
12751280
"@types/minimist@^1.2.0":
12761281
version "1.2.2"
12771282
resolved "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz"

0 commit comments

Comments
 (0)