-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
49 lines (49 loc) · 2.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const detect_external_link_1 = __importDefault(require("detect-external-link"));
const linkExternalStack = [];
function remarkableExternalLink(md, options) {
const defaultOptions = {
'rel': 'noopener',
'target': '_blank',
'externalOnly': true,
};
const defaultOpenRender = md.renderer.rules.link_open;
const defaultCloseRender = md.renderer.rules.link_close;
const finalConfig = Object.assign({}, defaultOptions, options);
const externalLinkConfig = {
hosts: [],
};
if (finalConfig.hosts) {
externalLinkConfig.hosts = finalConfig.hosts;
}
else if (finalConfig.host) {
externalLinkConfig.hosts = [finalConfig.host];
}
md.renderer.rules.link_open = function (tokens, idx, ...args) {
let result = defaultOpenRender(tokens, idx, ...args);
let externalLink = false;
if (tokens[idx] && tokens[idx].href) {
const urlHref = tokens[idx].href;
if ((0, detect_external_link_1.default)(urlHref, externalLinkConfig)) {
result = result.replace('>', ' target="' + finalConfig.target + '" rel="' + finalConfig.rel + '">');
externalLink = true;
}
}
linkExternalStack.push(externalLink);
if (externalLink || !finalConfig.externalOnly)
result = (options.beforeLink || "") + result + (options.beforeLinkText || "");
return result;
};
md.renderer.rules.link_close = function (tokens, idx, ...args) {
let result = defaultCloseRender(tokens, idx, ...args);
const externalLink = linkExternalStack.pop();
if (externalLink || !finalConfig.externalOnly)
result = (options.afterLinkText || "") + result + (options.afterLink || "");
return result;
};
}
exports.default = remarkableExternalLink;