How can I ignore leading and trailing linebreaks within an html “code” block using css, javascript or jquery?

前端 未结 6 959
伪装坚强ぢ
伪装坚强ぢ 2021-02-19 18:59

In my HTML source code, I have a code block like the following (I use showdown and highlight.js on this page):


double myNum         


        
6条回答
  •  死守一世寂寞
    2021-02-19 19:34

    I just remove them manually before the highlighter starts.

    const trimLine = node => 
      node.firstChild.nodeValue = node.firstChild.nodeValue.replace(/^\n+|\n+$/g, "");
    
    
    
    window.onload = function () {
    
      Array.prototype.slice
          .call(document.getElementsByTagName("code"), 0)
          .map(trimLine);
    
      hljs.initHighlightingOnLoad();
    
    }

提交回复
热议问题