posthtml-rewrite-paths
v1.0.1
Published
Rewrites paths in HTML attributes.
Downloads
97
Maintainers
Readme
posthtml-rewrite-paths
A PostHTML plugin for rewriting paths in html attributes.
Before:
<script src="dist/index.js"></script>
After:
<script src="dist/index.hash.js"></script>
Install
npm i -D posthtml-rewrite-paths
Usage
const options = {
// Map of tags and the attributes to check for paths. This is the
// default if omitted.
search: { "script": ["src"]},
// Map of path names to replace or a path to a json file containing
// the same. If a replacement value is nullish, the attribute is
// removed.
pathMap: { "dist/index.js": "index.hash.js" }
};
const html = `<body><script src="dist/index.js"></script></body>`;
posthtml([ plugin(options) ])
.process(html)
.then(result => {
// write result.html;
});
Options
{
pathMap: "path-to-json-file.json" | { "path/to/replace.js": "replacement/path.js" };
search: { "tag-name": ["attribute-to-check", "attribute-to-check", ...]}
}
Paths are matched textually with some minor normalisation of path separators. Hence "src/a.js" will match "/src\a.js", but not "C:\project\src\s.js"; and leading and trailing whitespace in the path pattern is ignored.
There is a special case when pathMap
is loaded from file so that
{ "path/to/replace.js": { "path": "replacement/path.js" } }
is also accepted.
This allows for simpler integration with tools that produce files in this
format.