-
Hi there! I was wondering if there was a way to access source locations from a plugin. I need to add a I've seen in the posthtml-parser that an options Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi yes this is available when using posthtml since version 0.16.0 Here is an example that implements what you need through a plugin, referring to the properties of the tree and the node. const fs = require('fs');
const path = require('path');
const posthtml = require('posthtml');
const files = [
'files/index.html',
'files/test.html'
]
const plugin = function (tree) {
tree.walk(node => {
Object.assign(node, {
attrs: {
'data-file-name': tree.options.from,
'data-location': JSON.stringify(node.location)
}
});
return node;
})
return tree;
}
files.forEach(file => {
const html = fs.readFileSync(path.join(__dirname, file), 'utf-8');
const result = posthtml(plugin)
.process(html, {
sync: true,
from: file,
sourceLocations: true
})
.html
console.log(result)
});
|
Beta Was this translation helpful? Give feedback.
Hi yes this is available when using posthtml since version 0.16.0
Here is an example that implements what you need through a plugin, referring to the properties of the tree and the node.