Skip to content

Commit 16ec71e

Browse files
committed
Fix internal link code & unit test
1 parent a321b2e commit 16ec71e

File tree

10 files changed

+405
-72
lines changed

10 files changed

+405
-72
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"cmdhelp": "ts-node --compiler-options \"{\\\"module\\\": \\\"commonjs\\\"}\" src/index.ts",
1818
"// test out with a private sample notion db": "",
1919
"pull-test-tagged": "rm -rf ./docs/ && cross-var ts-node src/index.ts -n $DOCU_NOTION_INTEGRATION_TOKEN -r $DOCU_NOTION_TEST_ROOT_PAGE_ID --log-level debug --status-tag test",
20-
"pull-test-outline": "rm -rf ./docs/ && cross-var ts-node --compiler-options \"{\\\"module\\\": \\\"commonjs\\\"}\" src/index.ts -n %DOCU_NOTION_INTEGRATION_TOKEN% -r %DOCU_NOTION_TEST_ROOT_PAGE_ID% --log-level debug",
20+
"pull-sample-site": "rm -rf ./docs/ && cross-var ts-node src/index.ts -n %DOCU_NOTION_INTEGRATION_TOKEN% -r $DOCU_NOTION_SAMPLE_ROOT_PAGE --log-level debug",
21+
"pull-test-tagged": "rm -rf ./docs/ && cross-var ts-node src/index.ts -n $DOCU_NOTION_INTEGRATION_TOKEN -r $DOCU_NOTION_TEST_ROOT_PAGE_ID --log-level debug --status-tag test",
2122
"// test with a semi-stable/public site:": "",
2223
"pull-sample": "cross-var ts-node --compiler-options \"{\\\"module\\\": \\\"commonjs\\\"}\" src/index.ts -n %DOCU_NOTION_INTEGRATION_TOKEN% -r %DOCU_NOTION_SAMPLE_ROOT_PAGE% -m ./sample --locales en,es,fr,de --log-level verbose",
2324
"pull-sample-with-paths": "cross-var ts-node --compiler-options \"{\\\"module\\\": \\\"commonjs\\\"}\" src/index.ts -n %DOCU_NOTION_INTEGRATION_TOKEN% -r %DOCU_NOTION_SAMPLE_ROOT_PAGE% -m ./sample --img-output-path ./sample_img",

src/NotionPage.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ export class NotionPage {
7070
baseLinkId === this.pageId || // from a link_to_page.pageId, which still has the dashes
7171
baseLinkId === this.pageId.replaceAll("-", ""); // from inline links, which are lacking the dashes
7272

73-
logDebug(
74-
`matchedLinkId`,
75-
`comparing pageId:${this.pageId} to id ${id} --> ${match.toString()}`
76-
);
73+
// logDebug(
74+
// `matchedLinkId`,
75+
// `comparing pageId:${this.pageId} to id ${id} --> ${match.toString()}`
76+
// );
7777
return match;
7878
}
7979

src/TestRun.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ export async function blocksToMarkdown(
2222
notionClient,
2323
});
2424

25-
if (pages && pages.length) {
26-
console.log(pages[0]);
27-
console.log(pages[0].matchesLinkId);
28-
}
25+
// if (pages && pages.length) {
26+
// console.log(pages[0]);
27+
// console.log(pages[0].matchesLinkId);
28+
// }
2929
const docunotionContext: IDocuNotionContext = {
3030
notionToMarkdown: notionToMD,
3131
// TODO when does this actually need to do get some children?
@@ -209,7 +209,7 @@ export function makeSamplePageObject(options: {
209209
},
210210
},
211211
url: `https://www.notion.so/Hello-World-${id}`,
212-
}; // ?
212+
};
213213

214214
const p = new NotionPage({
215215
layoutContext: "/Second-Level/Third-Level",
@@ -255,9 +255,17 @@ export async function oneBlockToMarkdown(
255255
...block,
256256
};
257257

258+
const dummyPage1 = makeSamplePageObject({
259+
slug: "dummy1",
260+
name: "Dummy1",
261+
});
262+
const dummyPage2 = makeSamplePageObject({
263+
slug: "dummy2",
264+
name: "Dummy2",
265+
});
258266
return await blocksToMarkdown(
259267
config,
260268
[fullBlock as NotionBlock],
261-
targetPage ? [targetPage] : undefined
269+
targetPage ? [dummyPage1, targetPage, dummyPage2] : undefined
262270
);
263271
}

src/config/default.docunotion.config.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {
33
imgurGifEmbed,
44
vimeoEmbed,
55
youtubeEmbed,
6-
} from "../DocusaurusEmbeds";
6+
} from "../plugins/StandardEmbeddings";
77
import { standardImageTransformer } from "../images";
8-
import { standardLinkConversion } from "../plugins/internalLinks";
8+
import { standardInternalLinkConversion } from "../plugins/internalLinks";
99
import { standardCalloutTransformer } from "../plugins/CalloutTransformer";
1010
import { standardColumnListTransformer } from "../plugins/ColumnListTransformer";
1111
import { standardColumnTransformer } from "../plugins/ColumnTransformer";
@@ -14,6 +14,7 @@ import { standardHeadingTransformer } from "../plugins/HeadingTransformer";
1414
import { standardNumberedListTransformer } from "../plugins/NumberedListTransformer";
1515
import { standardTableTransformer } from "../plugins/TableTransformer";
1616
import { IDocuNotionConfig } from "./configuration";
17+
import { standardExternalLinkConversion } from "../plugins/externalLinks";
1718

1819
const defaultConfig: IDocuNotionConfig = {
1920
plugins: [
@@ -31,7 +32,8 @@ const defaultConfig: IDocuNotionConfig = {
3132
standardNumberedListTransformer,
3233

3334
// Link modifiers, which are special because they can read metadata from all the pages in order to figure out the correct url
34-
standardLinkConversion,
35+
standardInternalLinkConversion,
36+
standardExternalLinkConversion,
3537

3638
// Regexps plus javascript `import`s that operate on the Markdown output
3739
imgurGifEmbed,

src/plugins/CalloutTransformer.spec.ts

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { NotionBlock } from "../config/configuration";
2-
import { blocksToMarkdown } from "../TestRun";
2+
import { NotionPage } from "../NotionPage";
3+
import { blocksToMarkdown, makeSamplePageObject } from "../TestRun";
34
import { standardCalloutTransformer } from "./CalloutTransformer";
4-
import { standardColumnTransformer } from "./ColumnTransformer";
5+
import { standardExternalLinkConversion } from "./externalLinks";
6+
import { standardInternalLinkConversion } from "./internalLinks";
57

68
let block: any;
79
beforeEach(() => {
@@ -45,7 +47,13 @@ test("smoketest callout", async () => {
4547
});
4648

4749
test("external link inside callout, bold preserved", async () => {
48-
const config = { plugins: [standardCalloutTransformer] };
50+
const config = {
51+
plugins: [
52+
standardCalloutTransformer,
53+
standardInternalLinkConversion,
54+
standardExternalLinkConversion,
55+
],
56+
};
4957
const results = await blocksToMarkdown(config, [
5058
{
5159
type: "callout",
@@ -110,3 +118,85 @@ Callouts inline [**great page**](https://github.com).
110118
:::`
111119
);
112120
});
121+
122+
test("internal link inside callout, bold preserved", async () => {
123+
const config = {
124+
plugins: [
125+
standardCalloutTransformer,
126+
standardInternalLinkConversion,
127+
standardExternalLinkConversion,
128+
],
129+
};
130+
const slugTargetPage: NotionPage = makeSamplePageObject({
131+
slug: "hello-world",
132+
name: "Hello World",
133+
id: "123",
134+
});
135+
const results = await blocksToMarkdown(
136+
config,
137+
[
138+
{
139+
type: "callout",
140+
callout: {
141+
rich_text: [
142+
{
143+
type: "text",
144+
text: { content: "Callouts inline ", link: null },
145+
annotations: {
146+
bold: false,
147+
italic: false,
148+
strikethrough: false,
149+
underline: false,
150+
code: false,
151+
color: "default",
152+
},
153+
plain_text: "Callouts inline ",
154+
href: null,
155+
},
156+
{
157+
type: "text",
158+
text: {
159+
content: "great page",
160+
link: { url: `/123#456` },
161+
},
162+
annotations: {
163+
bold: true,
164+
italic: false,
165+
strikethrough: false,
166+
underline: false,
167+
code: false,
168+
color: "default",
169+
},
170+
plain_text: "great page",
171+
href: `/123#456`,
172+
},
173+
{
174+
type: "text",
175+
text: { content: ".", link: null },
176+
annotations: {
177+
bold: false,
178+
italic: false,
179+
strikethrough: false,
180+
underline: false,
181+
code: false,
182+
color: "default",
183+
},
184+
plain_text: " the end.",
185+
href: null,
186+
},
187+
],
188+
icon: { type: "emoji", emoji: "⚠️" },
189+
color: "gray_background",
190+
},
191+
} as unknown as NotionBlock,
192+
],
193+
[slugTargetPage]
194+
);
195+
expect(results.trim()).toBe(
196+
`:::caution
197+
198+
Callouts inline [**great page**](/hello-world#456) the end.
199+
200+
:::`
201+
);
202+
});

src/plugins/ColumnTransformer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Client } from "@notionhq/client";
21
import { NotionAPI } from "notion-client";
32
import { NotionToMarkdown } from "notion-to-md";
43
import { ListBlockChildrenResponseResult } from "notion-to-md/build/types";
@@ -66,6 +65,10 @@ async function getColumnWidth(
6665
const recordMap = await unofficialNotionClient.getPage(blockId);
6766
const blockResult = recordMap.block[blockId];
6867

68+
// ENHANCE: could we use https://github.com/NotionX/react-notion-x/tree/master/packages/notion-types
69+
// to get away from "any", which might be particularly helpful in the future
70+
// since this is using the unofficial (reverse engineered?) API.
71+
6972
const columnFormat = blockResult?.value?.format as any;
7073
const columnRatio = (columnFormat?.column_ratio as number) || 0.5;
7174

src/plugins/externalLinks.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,49 @@ test("external link inside callout", async () => {
7575
);
7676
});
7777

78+
test("inline links to external site", async () => {
79+
const results = await getMarkdown({
80+
type: "paragraph",
81+
paragraph: {
82+
rich_text: [
83+
{
84+
type: "text",
85+
text: { content: "Inline ", link: null },
86+
annotations: {
87+
bold: false,
88+
italic: false,
89+
strikethrough: false,
90+
underline: false,
91+
code: false,
92+
color: "default",
93+
},
94+
plain_text: "Inline ",
95+
href: null,
96+
},
97+
{
98+
type: "text",
99+
text: {
100+
content: "github",
101+
link: { url: "https://github.com" },
102+
},
103+
annotations: {
104+
bold: false,
105+
italic: false,
106+
strikethrough: false,
107+
underline: false,
108+
code: false,
109+
color: "default",
110+
},
111+
plain_text: "github",
112+
href: "https://github.com",
113+
},
114+
],
115+
color: "default",
116+
},
117+
});
118+
expect(results.trim()).toBe("Inline [github](https://github.com)");
119+
});
120+
78121
async function getMarkdown(block: object) {
79122
const config = {
80123
plugins: [standardExternalLinkConversion],

0 commit comments

Comments
 (0)