Skip to content

Commit 819902a

Browse files
committed
feat: add setLinkTransformer for adding custom links
1 parent 69642ce commit 819902a

File tree

2 files changed

+81
-12
lines changed

2 files changed

+81
-12
lines changed

src/notion-to-md.spec.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe("setCustomTransformer", () => {
1515
expect.objectContaining({
1616
type: "test",
1717
test: { foo: "bar" },
18-
})
18+
}),
1919
);
2020
});
2121
test("supports only one customTransformer per type ", () => {
@@ -65,4 +65,60 @@ describe("setCustomTransformer", () => {
6565
});
6666
expect(md).toBe("---");
6767
});
68+
69+
test("setLinkTransformer works", async () => {
70+
const n2m = new NotionToMarkdown({ notionClient: {} as any });
71+
n2m.setLinkTransformer((text, href) => {
72+
return `<a href="${href}" data-testid="my-link">${text}</a>`;
73+
});
74+
const md = await n2m.blockToMarkdown({
75+
id: "test",
76+
type: "paragraph",
77+
paragraph: {
78+
color: "default",
79+
rich_text: [
80+
{
81+
type: "text",
82+
text: {
83+
content: "Link using at-sign ",
84+
link: null,
85+
},
86+
annotations: {
87+
bold: false,
88+
italic: false,
89+
strikethrough: false,
90+
underline: false,
91+
code: false,
92+
color: "default",
93+
},
94+
plain_text: "Link using at-sign ",
95+
href: null,
96+
},
97+
{
98+
type: "mention",
99+
mention: {
100+
type: "page",
101+
page: {
102+
id: "f1b1910b-caec-8014-aecb-d34ee7f50191",
103+
},
104+
},
105+
annotations: {
106+
bold: false,
107+
italic: false,
108+
strikethrough: false,
109+
underline: false,
110+
code: false,
111+
color: "default",
112+
},
113+
plain_text: "My page",
114+
href: "https://www.notion.so/f1b1910bcaec8014aecbd34ee7f50191",
115+
},
116+
],
117+
},
118+
object: "block",
119+
});
120+
expect(md).toBe(
121+
'Link using at-sign <a href="https://www.notion.so/f1b1910bcaec8014aecbd34ee7f50191" data-testid="my-link">My page</a>',
122+
);
123+
});
68124
});

src/notion-to-md.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
import * as md from "./utils/md";
1717
import { getBlockChildren } from "./utils/notion";
1818

19+
type LinkTransformer = (text: string, href: string) => string;
20+
1921
/**
2022
* Converts a Notion page to Markdown.
2123
*/
@@ -44,6 +46,14 @@ export class NotionToMarkdown {
4446
return this;
4547
}
4648

49+
setLinkTransformer(fn: LinkTransformer) {
50+
this.linkTransformer = fn;
51+
}
52+
53+
linkTransformer(text: string, href: string) {
54+
return md.link(text, href);
55+
}
56+
4757
/**
4858
* Converts Markdown Blocks to string
4959
* @param {MdBlock[]} mdBlocks - Array of markdown blocks
@@ -120,8 +130,9 @@ export class NotionToMarkdown {
120130
mdOutput[pageIdentifier] = mdOutput[pageIdentifier] || "";
121131
if (mdstr[childPageTitle]) {
122132
// child page heading followed by child page content
123-
mdOutput[pageIdentifier] +=
124-
`\n${childPageTitle}\n${mdstr[childPageTitle]}`;
133+
mdOutput[
134+
pageIdentifier
135+
] += `\n${childPageTitle}\n${mdstr[childPageTitle]}`;
125136
}
126137
}
127138
} else if (mdBlocks.type === "toggle") {
@@ -159,8 +170,7 @@ export class NotionToMarkdown {
159170
mdOutput[pageIdentifier] += "\n";
160171
} else if (mdBlocks.type === "callout") {
161172
// do nothing the callout block is already processed
162-
}
163-
else {
173+
} else {
164174
let mdstr = this.toMarkdownString(
165175
mdBlocks.children,
166176
pageIdentifier,
@@ -375,7 +385,7 @@ export class NotionToMarkdown {
375385
title = matches ? matches[0] : type;
376386
}
377387

378-
return md.link(title, link);
388+
return this.linkTransformer(title, link);
379389
}
380390
}
381391
break;
@@ -399,7 +409,8 @@ export class NotionToMarkdown {
399409
};
400410
}
401411

402-
if (blockContent) return md.link(title, blockContent.url);
412+
if (blockContent)
413+
return this.linkTransformer(title, blockContent.url);
403414
}
404415
break;
405416

@@ -430,7 +441,7 @@ export class NotionToMarkdown {
430441
const tableRows = await getBlockChildren(this.notionClient, id, 100);
431442
let rowsPromise = tableRows?.map(async (row) => {
432443
const { type } = row as any;
433-
if (type !== 'table_row') return
444+
if (type !== "table_row") return;
434445
const cells = (row as any).table_row["cells"];
435446

436447
/**
@@ -490,7 +501,7 @@ export class NotionToMarkdown {
490501
plain_text = this.annotatePlainText(plain_text, annotations);
491502

492503
if (content["href"])
493-
plain_text = md.link(plain_text, content["href"]);
504+
plain_text = this.linkTransformer(plain_text, content["href"]);
494505

495506
parsedData += plain_text;
496507
});
@@ -500,9 +511,11 @@ export class NotionToMarkdown {
500511
switch (type) {
501512
case "code":
502513
{
503-
const codeContent = block.code.rich_text.map((t: any) => t.plain_text).join("\n");
504-
const language = block.code.language || "plaintext";
505-
parsedData = md.codeBlock(codeContent, language);
514+
const codeContent = block.code.rich_text
515+
.map((t: any) => t.plain_text)
516+
.join("\n");
517+
const language = block.code.language || "plaintext";
518+
parsedData = md.codeBlock(codeContent, language);
506519
}
507520
break;
508521

0 commit comments

Comments
 (0)