【问题标题】:Using Highlight.js inside a node script?在节点脚本中使用 Highlight.js?
【发布时间】:2018-03-10 07:18:38
【问题描述】:

尝试在使用JSDom 选择需要突出显示的块的节点脚本中运行highlight.js

          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

想法?

【问题讨论】:

  • 上述错误发生在哪一行?什么是堆栈跟踪?

标签: javascript html node.js jsdom highlight.js


【解决方案1】:

这将产生高光:

 const highlight = require('highlight.js');
 console.log(highlight.highlight('html', markup).value);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-29
    • 2019-08-28
    相关资源
    最近更新 更多