posthtml-relative-paths
Version:
Convert the absolute paths in HTML files to relative paths
83 lines • 3.63 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 = require("path");
const posix_1 = require("path/posix");
const fs_1 = require("fs");
const utils_1 = require("./utils");
const fast_glob_1 = __importDefault(require("fast-glob"));
const micro_memoize_1 = __importDefault(require("micro-memoize"));
function calculateRelativePath(url, filePath, root, src) {
let resolvedFile = (0, posix_1.join)(root, url);
if (!(0, fs_1.existsSync)(resolvedFile)) {
let found = false;
const srcFile = (0, posix_1.join)(src, url);
if ((0, fs_1.existsSync)(srcFile)) {
console.log(`Replaced ${resolvedFile} with ${srcFile}`);
resolvedFile = srcFile;
found = true;
}
const filesSearched = memoizedSearchInSourceFolder(src, url);
if (filesSearched.length === 1) {
const fileSearched = filesSearched[0];
console.warn(`Replaced ${resolvedFile} with ${fileSearched}`);
resolvedFile = fileSearched;
found = true;
}
const jpegFile = `${resolvedFile}.jpeg`;
if ((0, fs_1.existsSync)(jpegFile)) {
console.warn(`Replaced ${resolvedFile} with ${jpegFile} due to astro-imagetools' bug`);
resolvedFile = jpegFile;
found = true;
}
if (!found) {
console.error(`Could not resolve ${url} in ${filePath}`);
return url;
}
}
const folder = (0, utils_1.toPosixPath)((0, path_1.dirname)(filePath));
let relativeFile = (0, posix_1.relative)(folder, resolvedFile);
relativeFile = (0, utils_1.prependDot)(relativeFile);
return relativeFile;
}
const memoizedCalculateRelativePath = (0, micro_memoize_1.default)(calculateRelativePath);
function searchInSourceFolder(src, url) {
const files = fast_glob_1.default.sync([`${src}/**/${(0, utils_1.removeDot)(url)}`], {
dot: true,
onlyFiles: true,
});
if (files.length === 0) {
return fast_glob_1.default.sync([`${src}/**/${(0, posix_1.basename)(url)}`], {
dot: true,
onlyFiles: true,
});
}
else {
return files;
}
}
const memoizedSearchInSourceFolder = (0, micro_memoize_1.default)(searchInSourceFolder);
function PostHTMLRelativePaths(filePathGiven, rootGiven = (0, path_1.resolve)("./dist"), srcGiven = (0, path_1.resolve)("./src")) {
const root = (0, utils_1.toPosixPath)(!(0, path_1.isAbsolute)(rootGiven) ? (0, path_1.resolve)(rootGiven) : rootGiven);
const src = (0, utils_1.toPosixPath)(!(0, path_1.isAbsolute)(srcGiven) ? (0, path_1.resolve)(srcGiven) : srcGiven);
const filePath = (0, utils_1.toPosixPath)(!(0, path_1.isAbsolute)(filePathGiven) ? (0, path_1.resolve)(filePathGiven) : filePathGiven);
return (tree) => {
tree.walk((node) => {
if (typeof node.attrs !== "object") {
return node;
}
for (const [tag, url] of Object.entries(node.attrs)) {
if (typeof url === "string" &&
["src", "href", "srcset", "content"].includes(tag) &&
(url.startsWith("/") || url.startsWith("./") || url.startsWith("../"))) {
node.attrs[tag] = memoizedCalculateRelativePath(url, filePath, root, src);
}
}
return node;
});
};
}
exports.default = PostHTMLRelativePaths;
//# sourceMappingURL=index.js.map