antora-confluence
Version:
A tool to convert and publish Antora documentation to Confluence
49 lines (48 loc) • 1.97 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 Logger_1 = require("../Logger");
const LOGGER = (0, Logger_1.getLogger)();
const rewriteImages = (content, baseUrl) => {
const uploads = [];
content.querySelectorAll("img").forEach((img) => {
const src = img.getAttribute("src");
const imgWidth = img.getAttribute("width") || 500;
const imgAlign = img.getAttribute("align") || "center";
if (!src?.startsWith("http")) {
const sanitizedBaseUrl = baseUrl
.toString()
.replaceAll("\\\\", "/")
.replaceAll("/[^/]*$", "/");
let newUrl;
let fileName;
if (src?.startsWith("data:image")) {
LOGGER.warn("Embedded images, not yet implemented. Skipping...");
}
else {
newUrl = path_1.default.join(sanitizedBaseUrl, src);
fileName = decodeURI((src?.split("/").pop() || "").split("?")[0]);
newUrl = decodeURI(newUrl);
uploads.push({
fileName,
filePath: newUrl,
comment: "automatically uploaded",
});
img.insertAdjacentHTML("afterend", `<ac:image ac:align="${imgAlign}" ac:width="${imgWidth}"><ri:attachment ri:filename="${fileName}"/></ac:image>`);
}
}
else {
// it is an online image, so we have to use the ri:url tag
img.insertAdjacentHTML("afterend", `<ac:image ac:align="${imgAlign}" ac:width="${imgWidth}"><ri:url ri:value="${src}"/></ac:image>`);
}
img.remove();
});
return {
uploads,
content,
};
};
exports.default = rewriteImages;