UNPKG

antora-confluence

Version:

A tool to convert and publish Antora documentation to Confluence

120 lines (119 loc) 4.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.rewriteInternalLinks = exports.rewriteDescriptionLists = void 0; const node_html_parser_1 = __importDefault(require("node-html-parser")); const Enum_1 = require("../constants/Enum"); const node_path_1 = __importDefault(require("node:path")); const Logger_1 = require("../Logger"); const LOGGER = (0, Logger_1.getLogger)(); var LinkTags; (function (LinkTags) { LinkTags["dt"] = "th"; LinkTags["dd"] = "td"; })(LinkTags || (LinkTags = {})); const rewriteDescriptionLists = (content) => { content.querySelectorAll("dl").forEach((dl) => { dl.querySelectorAll("div").forEach((div) => { const child = div.firstChild; div.replaceWith(child); }); const rows = []; let current = { dt: [], dd: [] }; rows.push(current); dl.querySelectorAll("dt, dd").forEach((child) => { const tagName = child.tagName.toLowerCase(); if (tagName == "dt" && current.dd.length > 0) { current = { dt: [], dd: [] }; rows.push(current); } //@ts-ignore current[tagName].push(child); child.remove(); }); rows.forEach((row) => { const sizes = { dt: row.dt.length, dd: row.dd.length }; const rowspanIdx = { dt: -1, dd: sizes.dd - 1 }; const rowspan = Math.abs(sizes.dt - sizes.dd) + 1; let max = sizes.dt; if (sizes.dt < sizes.dd) { max = sizes.dd; rowspanIdx.dt = sizes.dt - 1; rowspanIdx.dd = -1; } for (let idx = 0; idx < max; idx++) { const tr = dl.insertAdjacentHTML("afterend", "<tr></tr>"); const types = ["dt", "dd"]; types.forEach((type) => { //@ts-ignore if (sizes[type] > idx) { //@ts-ignore tr.appendChild(row[type][idx]); //@ts-ignore if (idx == rowspanIdx[type] && rowspan > 1) { //@ts-ignore row[type][idx].setAttribute("rowspan", rowspan); } } else if (idx == 0) { tr.insertAdjacentHTML("afterend", //@ts-ignore `<${LinkTags[type]} rowspan="${rowspan}"></${LinkTags[type]}>`); } }); } }); const node = dl.firstChild; dl.replaceWith("<table></table>").appendChild(node); }); return { content, }; }; exports.rewriteDescriptionLists = rewriteDescriptionLists; const findLinkedPageInTree = (pageTree, fqfn) => { return pageTree.find((page) => page.fqfn === fqfn); }; const rewriteInternalLinks = (content, baseUrl, flatPages) => { content.querySelectorAll("a[href]").forEach((a) => { const href = a.getAttribute("href"); let pageTitle; let anchor; let localAnchor = false; if (href) { if (href.startsWith("#")) { // it is a local anchor anchor = href.substring(1); localAnchor = true; LOGGER.debug(`Rewrite link to local anchor ${anchor}`); } else if (!href.startsWith("http") && !href.startsWith("//") && !href.startsWith("mailto")) { let linkedPageFqfn = href; if (href.includes("#")) { const hrefWithAnchor = href.split("#"); linkedPageFqfn = hrefWithAnchor[0]; anchor = hrefWithAnchor[1]; } pageTitle = findLinkedPageInTree(flatPages, node_path_1.default.join(node_path_1.default.dirname(baseUrl), linkedPageFqfn)).pageTitle; LOGGER.debug(`Rewrite link to other page with title ${pageTitle} original link was ${href}`); } if (pageTitle && a.text) { const linkMacro = (0, node_html_parser_1.default)(`<ac:link ${anchor ? `ac:anchor="${anchor}"` : ""}> ${localAnchor ? "" : `<ri:page ri:content-title="${pageTitle.trim()}"/>`} <ac:plain-text-link-body>${Enum_1.Placeholder.CDATA_PLACEHOLDER_START} ${a.text.trim()} ${Enum_1.Placeholder.CDATA_PLACEHOLDER_END}</ac:plain-text-link-body> </ac:link>`); a.replaceWith(linkMacro); } } }); return { content, }; }; exports.rewriteInternalLinks = rewriteInternalLinks;