Compare commits
1 Commits
main
...
external-l
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
806bc89e8c |
@ -110,6 +110,7 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) {
|
||||
? "/" + possibleWikiLinkPermalinks[0]
|
||||
: possibleWikiLinkPermalinks[0]) ||
|
||||
"";
|
||||
const isExternal = /^https?:\/\//.test(link);
|
||||
|
||||
wikiLink.data.exists = !!matchingPermalink;
|
||||
wikiLink.data.permalink = link;
|
||||
@ -130,7 +131,7 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) {
|
||||
wikiLink.data.hName = "a";
|
||||
wikiLink.data.hProperties = {
|
||||
className: classNames + " " + "transclusion",
|
||||
href: hrefTemplate(link) + headingId,
|
||||
href: hrefTemplate(link) + headingId
|
||||
};
|
||||
wikiLink.data.hChildren = [{ type: "text", value: displayName }];
|
||||
|
||||
@ -163,7 +164,11 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) {
|
||||
wikiLink.data.hProperties = {
|
||||
className: classNames,
|
||||
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 }];
|
||||
}
|
||||
}
|
||||
|
||||
@ -96,6 +96,9 @@ function html(opts: HtmlOptions = {}) {
|
||||
: possibleWikiLinkPermalinks[0]) ||
|
||||
"";
|
||||
|
||||
const isExternal = /^https?:\/\//.test(link);
|
||||
const openInNewTab = isExternal ? 'target="_blank"' : '';
|
||||
|
||||
// remove leading # if the target is a heading on the same page
|
||||
const displayName = alias || target.replace(/^#/, "");
|
||||
// replace spaces with dashes and lowercase headings
|
||||
|
||||
@ -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>`);
|
||||
});
|
||||
})
|
||||
|
||||
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>`);
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user