Compare commits

...

1 Commits

Author SHA1 Message Date
mohamedsalem401
806bc89e8c Open external links in a new tab 2024-02-29 13:52:36 +02:00
3 changed files with 19 additions and 1 deletions

View File

@ -110,6 +110,7 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) {
? "/" + possibleWikiLinkPermalinks[0] ? "/" + possibleWikiLinkPermalinks[0]
: possibleWikiLinkPermalinks[0]) || : possibleWikiLinkPermalinks[0]) ||
""; "";
const isExternal = /^https?:\/\//.test(link);
wikiLink.data.exists = !!matchingPermalink; wikiLink.data.exists = !!matchingPermalink;
wikiLink.data.permalink = link; wikiLink.data.permalink = link;
@ -130,7 +131,7 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) {
wikiLink.data.hName = "a"; wikiLink.data.hName = "a";
wikiLink.data.hProperties = { wikiLink.data.hProperties = {
className: classNames + " " + "transclusion", className: classNames + " " + "transclusion",
href: hrefTemplate(link) + headingId, href: hrefTemplate(link) + headingId
}; };
wikiLink.data.hChildren = [{ type: "text", value: displayName }]; wikiLink.data.hChildren = [{ type: "text", value: displayName }];
@ -163,7 +164,11 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) {
wikiLink.data.hProperties = { wikiLink.data.hProperties = {
className: classNames, className: classNames,
href: hrefTemplate(link) + headingId, href: hrefTemplate(link) + headingId,
target: "_blank",
}; };
if(isExternal){
wikiLink.data.hProperties.target = "_blank"; // Open in a new tab
}
wikiLink.data.hChildren = [{ type: "text", value: displayName }]; wikiLink.data.hChildren = [{ type: "text", value: displayName }];
} }
} }

View File

@ -96,6 +96,9 @@ function html(opts: HtmlOptions = {}) {
: possibleWikiLinkPermalinks[0]) || : possibleWikiLinkPermalinks[0]) ||
""; "";
const isExternal = /^https?:\/\//.test(link);
const openInNewTab = isExternal ? 'target="_blank"' : '';
// remove leading # if the target is a heading on the same page // remove leading # if the target is a heading on the same page
const displayName = alias || target.replace(/^#/, ""); const displayName = alias || target.replace(/^#/, "");
// replace spaces with dashes and lowercase headings // replace spaces with dashes and lowercase headings

View File

@ -331,4 +331,14 @@ describe("micromark-extension-wiki-link", () => {
expect(serialized).toBe(`<p><a href="li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#li-nk-w(i)th-àcèô-íã_a(n)d_underline!:ª%@'*º$-°~./\\" class="internal new">li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#LI NK-W(i)th-àcèô íã_a(n)d_uNdErlinE!:ª%@'*º$ °~./\\</a></p>`); expect(serialized).toBe(`<p><a href="li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#li-nk-w(i)th-àcèô-íã_a(n)d_underline!:ª%@'*º$-°~./\\" class="internal new">li nk-w(i)th-àcèô íã_a(n)d_underline!:ª%@'*º$ °~./\\#LI NK-W(i)th-àcèô íã_a(n)d_uNdErlinE!:ª%@'*º$ °~./\\</a></p>`);
}); });
}) })
describe("External links", () => {
test("parses an external link that opens in a new tab", () => {
const serialized = micromark("[google](https://www.google.com/)", "ascii", {
extensions: [syntax()],
htmlExtensions: [html() as any],
});
expect(serialized).toBe(`<p><a href="https://www.google.com/" target="_blank">google</a></p>`);
});
})
}); });