Skip to content

Commit f1176c5

Browse files
committed
fix: Multiple links in a paragraph (#108)
1 parent 2d37898 commit f1176c5

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

src/plugins/internalLinks.spec.ts

+80-2
Original file line numberDiff line numberDiff line change
@@ -606,9 +606,87 @@ test("internal link inside codeblock ignored", async () => {
606606
);
607607
});
608608

609+
test("multiple internal links in a paragraph", async () => {
610+
const targetPageAId = "123";
611+
const targetPageA: NotionPage = makeSamplePageObject({
612+
slug: undefined,
613+
name: "Hello World A",
614+
id: targetPageAId,
615+
});
616+
const targetPageBId = "456";
617+
const targetPageB: NotionPage = makeSamplePageObject({
618+
slug: undefined,
619+
name: "Hello World B",
620+
id: targetPageBId,
621+
});
622+
623+
const results = await getMarkdown(
624+
{
625+
type: "paragraph",
626+
paragraph: {
627+
rich_text: [
628+
{
629+
type: "text",
630+
text: {
631+
content: "A",
632+
link: { url: `/${targetPageAId}` },
633+
},
634+
annotations: {
635+
bold: false,
636+
italic: false,
637+
strikethrough: false,
638+
underline: false,
639+
code: false,
640+
color: "default",
641+
},
642+
plain_text: "A",
643+
href: `/${targetPageAId}`,
644+
},
645+
{
646+
type: "text",
647+
text: { content: " ", link: null },
648+
annotations: {
649+
bold: false,
650+
italic: false,
651+
strikethrough: false,
652+
underline: false,
653+
code: false,
654+
color: "default",
655+
},
656+
plain_text: " ",
657+
href: null,
658+
},
659+
{
660+
type: "text",
661+
text: {
662+
content: "B",
663+
link: { url: `/${targetPageBId}` },
664+
},
665+
annotations: {
666+
bold: false,
667+
italic: false,
668+
strikethrough: false,
669+
underline: false,
670+
code: false,
671+
color: "default",
672+
},
673+
plain_text: "B",
674+
href: `/${targetPageBId}`,
675+
},
676+
],
677+
color: "default",
678+
},
679+
},
680+
targetPageA,
681+
targetPageB
682+
);
683+
expect(results.trim()).toBe(`[A](/${targetPageAId}) [B](/${targetPageBId})`);
684+
});
685+
609686
async function getMarkdown(
610687
block: Record<string, unknown>,
611-
targetPage?: NotionPage
688+
targetPage?: NotionPage,
689+
targetPage2?: NotionPage
612690
) {
613691
const config = {
614692
plugins: [
@@ -617,5 +695,5 @@ async function getMarkdown(
617695
standardExternalLinkConversion,
618696
],
619697
};
620-
return await oneBlockToMarkdown(config, block, targetPage);
698+
return await oneBlockToMarkdown(config, block, targetPage, targetPage2);
621699
}

src/plugins/pluginTestRun.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ export function makeSamplePageObject(options: {
241241
export async function oneBlockToMarkdown(
242242
config: IDocuNotionConfig,
243243
block: Record<string, unknown>,
244-
targetPage?: NotionPage
244+
targetPage?: NotionPage,
245+
targetPage2?: NotionPage
245246
): Promise<string> {
246247
// just in case someone expects these other properties that aren't normally relevant,
247248
// we merge the given block properties into an actual, full block
@@ -280,6 +281,6 @@ export async function oneBlockToMarkdown(
280281
return await blocksToMarkdown(
281282
config,
282283
[fullBlock as NotionBlock],
283-
targetPage ? [dummyPage1, targetPage, dummyPage2] : undefined
284+
targetPage ? [dummyPage1, targetPage, targetPage2 ?? dummyPage2] : undefined
284285
);
285286
}

src/transform.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function doLinkFixes(
189189
markdown: string,
190190
config: IDocuNotionConfig
191191
): string {
192-
const linkRegExp = /\[.*\]\([^\)]*\)/g;
192+
const linkRegExp = /\[.*?\]\([^\)]*?\)/g;
193193

194194
logDebug("markdown before link fixes", markdown);
195195
let match: RegExpExecArray | null;

0 commit comments

Comments
 (0)