honkit
Version:
HonKit is building beautiful books using Markdown.
82 lines (81 loc) • 3.73 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 html_1 = require("@honkit/html");
const resolveLinks_1 = __importDefault(require("../resolveLinks"));
describe("resolveLinks", () => {
function resolveFileBasic(href) {
return `fakeDir/${href}`;
}
function resolveFileCustom(href) {
if (path_1.default.extname(href) == ".md") {
return `${href.slice(0, -3)}.html`;
}
return href;
}
describe("Absolute path", () => {
const TEST = "<p>This is a <a href=\"/test/cool.md\"></a></p>";
test("should resolve path starting by \"/\" in root directory", () => {
const $ = (0, html_1.loadHtml)(TEST);
return (0, resolveLinks_1.default)("hello.md", resolveFileBasic, $).then(() => {
const link = $("a");
expect(link.attr("href")).toBe("fakeDir/test/cool.md");
});
});
test("should resolve path starting by \"/\" in child directory", () => {
const $ = (0, html_1.loadHtml)(TEST);
return (0, resolveLinks_1.default)("afolder/hello.md", resolveFileBasic, $).then(() => {
const link = $("a");
expect(link.attr("href")).toBe("../fakeDir/test/cool.md");
});
});
});
describe("Anchor", () => {
test("should prevent anchors in resolution", () => {
const TEST = "<p>This is a <a href=\"test/cool.md#an-anchor\"></a></p>";
const $ = (0, html_1.loadHtml)(TEST);
return (0, resolveLinks_1.default)("hello.md", resolveFileCustom, $).then(() => {
const link = $("a");
expect(link.attr("href")).toBe("test/cool.html#an-anchor");
});
});
test("should ignore pure anchor links", () => {
const TEST = "<p>This is a <a href=\"#an-anchor\"></a></p>";
const $ = (0, html_1.loadHtml)(TEST);
return (0, resolveLinks_1.default)("hello.md", resolveFileCustom, $).then(() => {
const link = $("a");
expect(link.attr("href")).toBe("#an-anchor");
});
});
});
describe("Custom Resolver", () => {
const TEST = "<p>This is a <a href=\"/test/cool.md\"></a> <a href=\"afile.png\"></a></p>";
test("should resolve path correctly for absolute path", () => {
const $ = (0, html_1.loadHtml)(TEST);
return (0, resolveLinks_1.default)("hello.md", resolveFileCustom, $).then(() => {
const link = $("a").first();
expect(link.attr("href")).toBe("test/cool.html");
});
});
test("should resolve path correctly for absolute path (2)", () => {
const $ = (0, html_1.loadHtml)(TEST);
return (0, resolveLinks_1.default)("afodler/hello.md", resolveFileCustom, $).then(() => {
const link = $("a").first();
expect(link.attr("href")).toBe("../test/cool.html");
});
});
});
describe("External link", () => {
const TEST = "<p>This is a <a href=\"http://www.github.com\">external link</a></p>";
test("should have target=\"_blank\" attribute", () => {
const $ = (0, html_1.loadHtml)(TEST);
return (0, resolveLinks_1.default)("hello.md", resolveFileBasic, $).then(() => {
const link = $("a");
expect(link.attr("target")).toBe("_blank");
});
});
});
});