Skip to content

Commit

Permalink
fix: Only convert html links if they use http or https protocol (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvanoni committed Jan 31, 2024
1 parent 10f27ba commit 12c48d6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/content/sync/html-to-notion/__tests__/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BlockObjectRequest } from '@notionhq/client/build/src/api-endpoints';
import * as annotations from './annotations';
import * as blockquote from './blockquote';
import * as formatting from './formatting';
import * as links from './links';
import * as nestedStyles from './nestedStyles';
import * as simple from './simple';
import * as textOnly from './textOnly';
Expand All @@ -21,6 +22,7 @@ const cases: Record<string, Pick<NoteTestCase, 'expected'>> = {
annotations,
blockquote,
formatting,
links,
nestedStyles,
simple,
textOnly,
Expand Down
3 changes: 3 additions & 0 deletions src/content/sync/html-to-notion/__tests__/fixtures/links.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<a href="https://notion.so">https link</a>
<a href="http://notion.so">http link</a>
<a href="zotero://select/item">zotero link</a>
19 changes: 19 additions & 0 deletions src/content/sync/html-to-notion/__tests__/fixtures/links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BlockObjectRequest } from '@notionhq/client/build/src/api-endpoints';

export const expected: BlockObjectRequest[] = [
{
paragraph: {
rich_text: [
{
text: { content: 'https link', link: { url: 'https://notion.so/' } },
},
{ text: { content: ' ' } },
{
text: { content: 'http link', link: { url: 'http://notion.so/' } },
},
{ text: { content: ' ' } },
{ text: { content: 'zotero link' } },
],
},
},
];
4 changes: 3 additions & 1 deletion src/content/sync/html-to-notion/parse-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ function getMathExpression(element: HTMLElement): string | null {
}

function parseAnchorElement(element: HTMLAnchorElement): RichTextElement {
const isHttpURL = /^https?:\/\/.+/.test(element.href);

return {
...parseRichTextElement(element),
link: { url: element.href },
...(isHttpURL && { link: { url: element.href } }),
};
}

Expand Down

0 comments on commit 12c48d6

Please sign in to comment.