markugen
Version:
Markdown to HTML/PDF static site generation tool
42 lines (41 loc) • 1.79 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = markedLinks;
const node_path_1 = __importDefault(require("node:path"));
const utils_1 = require("../utils");
/**
* Extension for adding target="_blank" to absolute URLs and switching
* *.md links to *.html
* @returns the marked extension
*/
function markedLinks() {
return {
async: false,
renderer: {
link(token) {
// md -> html
if (/\.md/i.test(token.href) && !node_path_1.default.isAbsolute(token.href)) {
const href = (0, utils_1.replaceLast)(token.href, /\.md/ig, '.html');
let text = this.parser.parseInline(token.tokens);
// check matching text in link text
const html = node_path_1.default.basename(href.split('#')[0]);
const md = node_path_1.default.basename(token.href.split('#')[0]);
text = text.replace(md, html);
return `<a class="markugen-md-link" href="${href}"` +
`${token.title ? ` title="${token.title}"` : ''}>` +
`${text}</a>`;
}
// only add for outside links
if (URL.canParse(token.href) && !token.href.startsWith('mailto:')) {
return `<a target="_blank" href="${token.href}"` +
`${token.title ? ` title="${token.title}"` : ''}>` +
`${this.parser.parseInline(token.tokens)}</a>`;
}
return false;
}
}
};
}