Skip to content

Commit 2988b70

Browse files
committed
fix #36 Links don't work in numbered lists or callouts
1 parent b1fc879 commit 2988b70

File tree

4 files changed

+149
-5
lines changed

4 files changed

+149
-5
lines changed

src/plugins/CalloutTransformer.spec.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,70 @@ test("smoketest callout", async () => {
4343
results = await blocksToMarkdown(config, [block as unknown as NotionBlock]);
4444
expect(results).toContain(":::info");
4545
});
46+
47+
test("external link inside callout, bold preserved", async () => {
48+
const config = { plugins: [standardCalloutTransformer] };
49+
const results = await blocksToMarkdown(config, [
50+
{
51+
type: "callout",
52+
callout: {
53+
rich_text: [
54+
{
55+
type: "text",
56+
text: { content: "Callouts inline ", link: null },
57+
annotations: {
58+
bold: false,
59+
italic: false,
60+
strikethrough: false,
61+
underline: false,
62+
code: false,
63+
color: "default",
64+
},
65+
plain_text: "Callouts inline ",
66+
href: null,
67+
},
68+
{
69+
type: "text",
70+
text: {
71+
content: "great page",
72+
link: { url: `https://github.com` },
73+
},
74+
annotations: {
75+
bold: true,
76+
italic: false,
77+
strikethrough: false,
78+
underline: false,
79+
code: false,
80+
color: "default",
81+
},
82+
plain_text: "great page",
83+
href: `https://github.com`,
84+
},
85+
{
86+
type: "text",
87+
text: { content: ".", link: null },
88+
annotations: {
89+
bold: false,
90+
italic: false,
91+
strikethrough: false,
92+
underline: false,
93+
code: false,
94+
color: "default",
95+
},
96+
plain_text: ".",
97+
href: null,
98+
},
99+
],
100+
icon: { type: "emoji", emoji: "⚠️" },
101+
color: "gray_background",
102+
},
103+
} as unknown as NotionBlock,
104+
]);
105+
expect(results.trim()).toBe(
106+
`:::caution
107+
108+
Callouts inline [**great page**](https://github.com).
109+
110+
:::`
111+
);
112+
});

src/plugins/CalloutTransformer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ export async function notionCalloutToAdmonition(
3131

3232
plain_text = notionToMarkdown.annotatePlainText(plain_text, annotations);
3333

34-
// if (content["href"])
35-
// plain_text = md.link(plain_text, content["href"]);
34+
if (content["href"]) plain_text = `[${plain_text}](${content["href"]})`;
3635

3736
parsedData += plain_text;
3837
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { NotionBlock } from "../config/configuration";
2+
import { blocksToMarkdown } from "../TestRun";
3+
import { standardNumberedListTransformer } from "./NumberedListTransformer";
4+
5+
let block: any;
6+
beforeEach(() => {
7+
block = {
8+
has_children: false,
9+
archived: false,
10+
type: "callout",
11+
callout: {
12+
rich_text: [
13+
{
14+
type: "text",
15+
text: { content: "This is information callout", link: null },
16+
annotations: {
17+
bold: false,
18+
italic: false,
19+
strikethrough: false,
20+
underline: false,
21+
code: false,
22+
color: "default",
23+
},
24+
plain_text: "This is the callout",
25+
href: null,
26+
},
27+
],
28+
icon: { type: "emoji", emoji: "ℹ️" },
29+
color: "gray_background",
30+
},
31+
};
32+
});
33+
34+
test("external link inside numbered list, italic preserved", async () => {
35+
const config = { plugins: [standardNumberedListTransformer] };
36+
const results = await blocksToMarkdown(config, [
37+
{
38+
type: "numbered_list_item",
39+
numbered_list_item: {
40+
rich_text: [
41+
{
42+
type: "text",
43+
text: { content: "link ", link: null },
44+
annotations: {
45+
bold: false,
46+
italic: false,
47+
strikethrough: false,
48+
underline: false,
49+
code: false,
50+
color: "default",
51+
},
52+
plain_text: "link ",
53+
href: null,
54+
},
55+
{
56+
type: "text",
57+
text: {
58+
content: "github",
59+
link: { url: "https://github.com" },
60+
},
61+
annotations: {
62+
bold: false,
63+
italic: true,
64+
strikethrough: false,
65+
underline: false,
66+
code: false,
67+
color: "default",
68+
},
69+
plain_text: "github",
70+
href: "https://github.com",
71+
},
72+
],
73+
color: "default",
74+
},
75+
} as unknown as NotionBlock,
76+
]);
77+
expect(results.trim()).toBe(`1. link [_github_](https://github.com)`);
78+
});

src/plugins/NumberedListTransformer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export function numberedListTransformer(
2323

2424
plain_text = notionToMarkdown.annotatePlainText(plain_text, annotations);
2525

26-
// if (content["href"])
27-
// plain_text = md.link(plain_text, content["href"]);
28-
26+
if (content["href"]) {
27+
plain_text = `[${plain_text}](${content["href"]})`;
28+
}
2929
parsedData += plain_text;
3030
});
3131

0 commit comments

Comments
 (0)