Skip to content

Commit

Permalink
fix: Multiple links in a paragraph (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-polk committed Jun 24, 2024
1 parent 2d37898 commit f1176c5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
82 changes: 80 additions & 2 deletions src/plugins/internalLinks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,87 @@ test("internal link inside codeblock ignored", async () => {
);
});

test("multiple internal links in a paragraph", async () => {
const targetPageAId = "123";
const targetPageA: NotionPage = makeSamplePageObject({
slug: undefined,
name: "Hello World A",
id: targetPageAId,
});
const targetPageBId = "456";
const targetPageB: NotionPage = makeSamplePageObject({
slug: undefined,
name: "Hello World B",
id: targetPageBId,
});

const results = await getMarkdown(
{
type: "paragraph",
paragraph: {
rich_text: [
{
type: "text",
text: {
content: "A",
link: { url: `/${targetPageAId}` },
},
annotations: {
bold: false,
italic: false,
strikethrough: false,
underline: false,
code: false,
color: "default",
},
plain_text: "A",
href: `/${targetPageAId}`,
},
{
type: "text",
text: { content: " ", link: null },
annotations: {
bold: false,
italic: false,
strikethrough: false,
underline: false,
code: false,
color: "default",
},
plain_text: " ",
href: null,
},
{
type: "text",
text: {
content: "B",
link: { url: `/${targetPageBId}` },
},
annotations: {
bold: false,
italic: false,
strikethrough: false,
underline: false,
code: false,
color: "default",
},
plain_text: "B",
href: `/${targetPageBId}`,
},
],
color: "default",
},
},
targetPageA,
targetPageB
);
expect(results.trim()).toBe(`[A](/${targetPageAId}) [B](/${targetPageBId})`);
});

async function getMarkdown(
block: Record<string, unknown>,
targetPage?: NotionPage
targetPage?: NotionPage,
targetPage2?: NotionPage
) {
const config = {
plugins: [
Expand All @@ -617,5 +695,5 @@ async function getMarkdown(
standardExternalLinkConversion,
],
};
return await oneBlockToMarkdown(config, block, targetPage);
return await oneBlockToMarkdown(config, block, targetPage, targetPage2);
}
5 changes: 3 additions & 2 deletions src/plugins/pluginTestRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ export function makeSamplePageObject(options: {
export async function oneBlockToMarkdown(
config: IDocuNotionConfig,
block: Record<string, unknown>,
targetPage?: NotionPage
targetPage?: NotionPage,
targetPage2?: NotionPage
): Promise<string> {
// just in case someone expects these other properties that aren't normally relevant,
// we merge the given block properties into an actual, full block
Expand Down Expand Up @@ -280,6 +281,6 @@ export async function oneBlockToMarkdown(
return await blocksToMarkdown(
config,
[fullBlock as NotionBlock],
targetPage ? [dummyPage1, targetPage, dummyPage2] : undefined
targetPage ? [dummyPage1, targetPage, targetPage2 ?? dummyPage2] : undefined
);
}
2 changes: 1 addition & 1 deletion src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function doLinkFixes(
markdown: string,
config: IDocuNotionConfig
): string {
const linkRegExp = /\[.*\]\([^\)]*\)/g;
const linkRegExp = /\[.*?\]\([^\)]*?\)/g;

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

0 comments on commit f1176c5

Please sign in to comment.