pc035860 / angular-highlightjs

AngularJS directive for syntax highlighting with highlight.js
http://pc035860.github.io/angular-highlightjs/example/
MIT License
294 stars 53 forks source link

Some blank lines are deleted #78

Closed DavidZemon closed 7 years ago

DavidZemon commented 8 years ago

Raw hljs works as expected, but you'll notice one of the blank lines in the code block is removed when using angular-highlightjs

Raw: https://jsfiddle.net/a1vh98ws/1/ Angular: https://jsfiddle.net/Lasdvo33/1/

pc035860 commented 8 years ago

It seems that Raw version uses highlight.js 9.6.0, and Angular version uses highlight.js 8.9.1 . Sync there version will produce the same output.

Here's Angular version with highlight.js 9.6.0 https://jsfiddle.net/arp4a21t/

DavidZemon commented 8 years ago

Maybe it's a browser difference? Still broken for me: broken

I'm running Ubuntu 16.10 x64, Chrome 53.0.2785.101 (64-bit)

pc035860 commented 8 years ago

I find this is actually a bug of angular-highlightjs. While trying to strip new lines from the head of the code, the regex doesn't work as expected.

https://github.com/pc035860/angular-highlightjs/blob/master/src/angular-highlightjs.js#L283

// get static code
// strip the starting "new line" character
var staticHTML = tElm[0].innerHTML.replace(/^(\r\n|\r|\n)/m, ''),
    staticText = tElm[0].textContent.replace(/^(\r\n|\r|\n)/m, '');

I'll get this fixed soon. Thanks for the report!

n4bb12 commented 7 years ago

To find a new line at index 0, skip the m modifier: .replace(/^(\r\n|\r|\n)/, '')

Or alternatively you could just trim whitespace at start/end.

Trim start: .replace(/^\s+/g, '')

Trim start and end: .replace(/^\s+|\s+$/g, '')

Would love if this gets fixed =)

pc035860 commented 7 years ago

@n4bb1 Thanks for the advice. Just got it fixed and published a new version.

n4bb12 commented 7 years ago

Thanks man.

I am also having the same issue with the include-directive: https://github.com/pc035860/angular-highlightjs/blob/master/src/angular-highlightjs.js#L467