You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
745 B
36 lines
745 B
3 years ago
|
const { getRoot } = require('../utils');
|
||
|
|
||
|
/**
|
||
|
* @return {Function} PostHTML plugin
|
||
|
*/
|
||
|
function extractNamespacesToRoot() {
|
||
|
return (tree) => {
|
||
|
const namespaces = {};
|
||
|
|
||
|
tree.match({ tag: /.*/ }, (node) => {
|
||
|
const attrs = node.attrs || {};
|
||
|
|
||
|
Object.keys(attrs).forEach((attr) => {
|
||
|
if (attr.startsWith('xmlns')) {
|
||
|
if (attr in namespaces === false) {
|
||
|
namespaces[attr] = attrs[attr];
|
||
|
}
|
||
|
|
||
|
delete node.attrs[attr];
|
||
|
}
|
||
|
});
|
||
|
|
||
|
return node;
|
||
|
});
|
||
|
|
||
|
const root = getRoot(tree);
|
||
|
root.attrs = root.attrs || {};
|
||
|
|
||
|
Object.keys(namespaces).forEach(name => root.attrs[name] = namespaces[name]);
|
||
|
|
||
|
return tree;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
module.exports = extractNamespacesToRoot;
|