Skip to content

Handle newlines in notion table cells #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2022
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ sample_img/
i18n/
node_modules/
version.json
docs/
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@
"file-type": "16.5.1",
"fs-extra": "^10.1.0",
"limiter": "^2.1.0",
"markdown-table": "^2.0.0",
"node-fetch": "2.6.6",
"notion-to-md": "^2.5.2",
"path": "^0.12.7",
"postinstall-postinstall": "^2.1.0",
"sanitize-filename": "^1.6.3"
},
"devDependencies": {
"@types/markdown-table": "^2.0.0",
"@types/fs-extra": "^9.0.13",
"@types/jest": "^28.1.6",
"@types/node": "^12.20.11",
Expand Down
54 changes: 54 additions & 0 deletions src/CustomTranformers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Client } from "@notionhq/client";
import { ListBlockChildrenResponse } from "@notionhq/client/build/src/api-endpoints";
import { NotionToMarkdown } from "notion-to-md";
import markdownTable from "markdown-table";
import {
ListBlockChildrenResponseResult,
ListBlockChildrenResponseResults,
Expand All @@ -22,6 +23,59 @@ export function setupCustomTransformers(
notionColumnToMarkdown(notionToMarkdown, notionClient, block)
);

// This is mostly a copy of the table handler from notion-to-md. The change is to handle newlines in the
// notion cell content.
notionToMarkdown.setCustomTransformer(
"table",
async (block: ListBlockChildrenResponseResult) => {
const { id, has_children } = block as any;
const tableArr: string[][] = [];
if (has_children) {
const tableRows = await getBlockChildren(notionClient, id, 100);
// console.log(">>", tableRows);
const rowsPromise = tableRows?.map(async row => {
const { type } = row as any;
const cells = (row as any)[type]["cells"];

/**
* this is more like a hack since matching the type text was
* difficult. So converting each cell to paragraph type to
* reuse the blockToMarkdown function
*/
const cellStringPromise = cells.map(
async (cell: any) =>
await notionToMarkdown.blockToMarkdown({
type: "paragraph",
paragraph: { rich_text: cell },
} as ListBlockChildrenResponseResult)
);

const cellStringArrRaw: string[] = await Promise.all(
cellStringPromise
);
// This is our patch to the original notion-to-md code.
const cellStringArr = cellStringArrRaw.map(c =>
c
// Trailing newlines are almost certainly not wanted, and converting to br's gives weird results
.replace(/[\r\n]+$/, "")
// Preserving line breaks within cells can't be done in stock markdown. Since we're producing
// mdx, which supports embedded HTML, we can handle it with <br/>.
// I'm not sure exactly what line breaks might occur in the input, depending on platform,
// so handle all the common cases.
.replaceAll("\r\n", "<br/>")
.replaceAll("\n", "<br/>")
.replaceAll("\r", "<br/>")
);
// console.log("~~", cellStringArr);
tableArr.push(cellStringArr);
// console.log(tableArr);
});
await Promise.all(rowsPromise || []);
}
return markdownTable(tableArr);
}
);

// Note: Pull.ts also adds an image transformer, but has to do that for each
// page so we don't do it here.
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,11 @@
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz"
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==

"@types/markdown-table@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/markdown-table/-/markdown-table-2.0.0.tgz#d2a3458c61ee71c8ee2b40b76c199b85b8dbd70c"
integrity sha512-fVZN/DRjZvjuk+lo7ovlI/ZycS51gpYU5vw5EcFeqkcX6lucQ+UWgEOH2O4KJHkSck4DHAY7D7CkVLD0wzc5qw==

"@types/minimist@^1.2.0":
version "1.2.2"
resolved "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz"
Expand Down