weixin_39584549
2021-01-12 17:55 阅读 1

Empty first line is dropped

if I have a pre block of code that starts with a blank line, the line is not rendered by SyntaxHighlighter. This appears to happen if the first line contains only whitespace or contains only a newline.

For example, the following pre block:


public class Foo {
}

will be rendered without the initial blank line.

public class Foo {
}

Line numbering and highlighting will all be off by one. The problem also appears to occur for trailing blank lines. Blank lines surrounded by non-blank lines render properly.

该提问来源于开源项目:syntaxhighlighter/syntaxhighlighter

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • weixin_39671467 weixin_39671467 2021-01-12 17:55

    I noticed that too. I wanted to look into it at some point to see if SyntaxHighlighter is doing it or if it's a DOM issue out of SyntaxHighlighter's control. I would also highly prefer to at least have the option to render leading & trailing newlines in the highlighted code.

    点赞 评论 复制链接分享
  • weixin_39584549 weixin_39584549 2021-01-12 17:55

    I found that if I changed the trimFirstAndLastLines function to:

    function trimFirstAndLastLines(str)
    {
        return str.replace(/^[ ]*[\n]/g, sh.config.space + '\n').replace(/[\n][ ]*$/g, '');
    };
    

    this fixes the problem.

    点赞 评论 复制链接分享
  • weixin_39875167 weixin_39875167 2021-01-12 17:55

    This is a built in feature. Most times when people start the code blocks they would have on one line and then begin code on the next line. This would result in a new line right after the opening tag. Therefore trimming of empty lines is necessary for most common case. I'm open to having this an on/off feature if you wish to contribute it. Otherwise it seems like a very low priority feature.

    点赞 评论 复制链接分享
  • weixin_39571403 weixin_39571403 2021-01-12 17:55

    This feature is a bug for my purposes :( I will need an ugly hack, or is it possible with the current version to deactivate this automatic trimming? Thanks for any help!

    点赞 评论 复制链接分享
  • weixin_39622901 weixin_39622901 2021-01-12 17:55

    This should be an opt-in function, not a default behaviour. In my case, I needed to highlight an excerpt from a larger file and line-numbers to refer to the original lines. I had to add an empty comment to every empty line to avoid empty lines getting stripped (and therefore de-referencing the original file lines).

    点赞 评论 复制链接分享

相关推荐