nodebook
Version:
Node.js — Apprendre par l'exemple • Devenez autonome avec JavaScript, l'écosystème npm, le développement frontend, les applications web et en ligne de commande.
50 lines (39 loc) • 1.27 kB
JavaScript
;
const isBash = (b) => b.getAttribute('language', 'bash');
const index = (blocks, block) => blocks.findIndex(el => el === block);
module.exports = function bash$Extension () {
this.treeProcessor(function(){
this.process(doc => {
doc.findBy({ context: 'listing' }, isBash).forEach(block => {
const {parent} = block;
let isModified = false;
const lines = block.lines.map(line => {
if (line[0] === '$' && line[1] === ' ') {
isModified = true;
return '<span data-bash-subs="$"></span>' + line.slice(2);
}
if (line[0] === '>' && line[1] === ' ') {
isModified = true;
return '<span data-bash-subs=">"></span>' + line.slice(2);
}
return line;
});
if (isModified) {
const blockIndex = index(parent.getBlocks(), block);
block.lines = lines;
block.$remove_sub('specialcharacters');
parent.blocks[blockIndex] = block;
}
});
});
});
this.docinfoProcessor(function(){
this.process(() => {
return `<style type="text/css">
.listingblock [data-bash-subs]::before{
content: attr(data-bash-subs) " ";
opacity: .5; }
</style>`;
});
});
};