在节点脚本中使用 Highlight.js?
Using Highlight.js inside a node script?
正在尝试运行highlight.js inside a node script that uses JSDom到select需要高亮的块
const dom = new JSDOM(html);
const document = dom.window.document;
document.querySelectorAll('.Test_markup > code').forEach((node)=> {
var markup = node.innerHTML;
console.log(markup);
highlight.highlightBlock(markup);
});
记录的标记如下所示:
<div class="Grid">
<!-- Use Nunjucks to keep markup DRY -->
<div class="Grid_cell">1/2</div>
<div class="Grid_cell">2/2</div>
</div>
当我 运行 标记上的代码时,结果是:
TypeError: Cannot read property 'replace' of undefined
想法?
这将产生亮点:
const highlight = require('highlight.js');
console.log(highlight.highlight('html', markup).value);
正在尝试运行highlight.js inside a node script that uses JSDom到select需要高亮的块
const dom = new JSDOM(html);
const document = dom.window.document;
document.querySelectorAll('.Test_markup > code').forEach((node)=> {
var markup = node.innerHTML;
console.log(markup);
highlight.highlightBlock(markup);
});
记录的标记如下所示:
<div class="Grid">
<!-- Use Nunjucks to keep markup DRY -->
<div class="Grid_cell">1/2</div>
<div class="Grid_cell">2/2</div>
</div>
当我 运行 标记上的代码时,结果是:
TypeError: Cannot read property 'replace' of undefined
想法?
这将产生亮点:
const highlight = require('highlight.js');
console.log(highlight.highlight('html', markup).value);