Skip to content

Instantly share code, notes, and snippets.

@josemarluedke
Created March 23, 2020 19:07
Show Gist options
  • Save josemarluedke/44e649a96da30e9030b5171e81330b38 to your computer and use it in GitHub Desktop.
Save josemarluedke/44e649a96da30e9030b5171e81330b38 to your computer and use it in GitHub Desktop.
const visit = require('unist-util-visit');
const escapeHBS = node => {
if(!node.value) {
return;
}
if (((/^({{|<[A-Z]|<\/[A-Z])/g)).test(node.value)) {
node.type = 'html';
}
}
const removeParagraphsWithHTML = (ast) => {
return (node) => {
if (!node.children) {
return
}
if (node.children[0].type === 'html') {
let index = ast.children.findIndex((item) => item === node)
ast.children.splice(index, 1, ...node.children);
}
}
}
module.exports = function() {
return ast => {
visit(ast, 'text', escapeHBS)
visit(ast, 'paragraph', removeParagraphsWithHTML(ast))
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment