honkit
Version:
HonKit is building beautiful books using Markdown.
47 lines (45 loc) • 1.64 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const url_1 = __importDefault(require("url"));
const location_1 = __importDefault(require("../../utils/location"));
const editHTMLElement_1 = __importDefault(require("./editHTMLElement"));
/**
Resolve all HTML links:
- /test.md in hello -> ../test.html
@param {string} currentFile
@param {Function(String) -> String} resolveFile
@param {HTMLDom} $
*/
function resolveLinks(currentFile, resolveFile, $) {
const currentDirectory = path_1.default.dirname(currentFile);
return (0, editHTMLElement_1.default)($, "a", ($a) => {
let href = $a.attr("href");
// Don't change a tag without href
if (!href) {
return;
}
if (location_1.default.isExternal(href)) {
$a.attr("target", "_blank");
return;
}
// Split anchor
const parsed = url_1.default.parse(href);
href = parsed.pathname || "";
if (href) {
// Calcul absolute path for this
href = location_1.default.toAbsolute(href, currentDirectory, ".");
// Resolve file
href = resolveFile(href);
// Convert back to relative
href = location_1.default.relative(currentDirectory, href);
}
// Add back anchor
href = href + (parsed.hash || "");
$a.attr("href", href);
});
}
exports.default = resolveLinks;