0.3.0 • Published 5 years ago
posthtml-img-src-rewrite v0.3.0
Image Source Rewrite
Rewrite an img src according to prefix/suffix rules
Before:
<html>
<body>
<img src="images/test.png">
</body>
</html>After:
<html>
<body>
<img src="images/prefix_test_suffix.png">
</body>
</html>Install
npm i posthtml-img-src-rewriteUsage
Pass in options for the prefix and suffix values to be used for the <img src> rewrites.
const fs = require('fs');
const posthtml = require('posthtml');
const imgSrcRewrite = require('posthtml-img-src-rewrite');
posthtml()
.use(imgSrcRewrite({ prefix: 'prefix_', suffix: '_suffix' }))
.process(html/*, options */)
.then(result => fs.writeFileSync('./after.html', result.html));Options
Pass in the values to be used as the prefix and suffix of all <img src> rewrites.
Function options
Options allow the use of functions to calculate the values as well.
Before:
<html>
<body>
<img src="images/test.png">
</body>
</html>Add option:
const fs = require('fs');
const posthtml = require('posthtml');
const imgSrcRewrite = require('posthtml-img-src-rewrite');
posthtml()
.use(imgSrcRewrite({ prefix: () => 'prefix_', suffix: () => '_suffix' }))
.process(html/*, options */)
.then(result => fs.writeFileSync('./after.html', result.html));After:
<html>
<body>
<img src="images/prefix_test_suffix.png">
</body>
</html>Exclusion
Exclude images from rewrite based on class. By default, this class is img-src-rewrite-exclude.
Before:
<html>
<body>
<img src="images/test.png">
<img src="images/exclude.png" class="img-src-rewrite-exclude">
</body>
</html>Add option:
const fs = require('fs');
const posthtml = require('posthtml');
const imgSrcRewrite = require('posthtml-img-src-rewrite');
posthtml()
.use(imgSrcRewrite({ prefix: () => 'prefix_', suffix: () => '_suffix', excludeClass: 'img-src-rewrite-exclude' }))
.process(html/*, options */)
.then(result => fs.writeFileSync('./after.html', result.html));After:
<html>
<body>
<img src="images/prefix_test_suffix.png">
</body>
</html>Contributing
See PostHTML Guidelines and contribution guide.