Skip to content

Instantly share code, notes, and snippets.

@zstix
Last active January 15, 2021 21:53
Show Gist options
  • Save zstix/569d01a4ab0cebad758137cb1890d5d4 to your computer and use it in GitHub Desktop.
Save zstix/569d01a4ab0cebad758137cb1890d5d4 to your computer and use it in GitHub Desktop.
const visit = require('unist-util-visit');
const indentedCodeBlock = () => (tree) => {
visit(
tree,
(node) => node.type === 'code',
(codeBlock, index, parent) => {
const values = codeBlock.value.split('```').filter(Boolean);
for (const value of values) {
const index = values.indexOf(value);
if (index === 0) {
codeBlock.value = value;
continue;
}
parent.children.splice(index + 1, 0, {
type: i % 2 !== 0 ? 'text', 'code',
value,
});
};
}
);
};
module.exports = indentedCodeBlock;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment