Skip to content

Fix multiline comments highlighting in c++ #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/highlightjs-line-numbers.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 37 additions & 5 deletions src/highlightjs-line-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
'use strict';

var TABLE_NAME = 'hljs-ln',
LINE_NAME = 'hljs-ln-line',
CODE_BLOCK_NAME = 'hljs-ln-code',
NUMBERS_BLOCK_NAME = 'hljs-ln-numbers',
NUMBER_LINE_NAME = 'hljs-ln-n',
LINE_NAME = 'hljs-ln-line',
CODE_BLOCK_NAME = 'hljs-ln-code',
NUMBERS_BLOCK_NAME = 'hljs-ln-numbers',
NUMBER_LINE_NAME = 'hljs-ln-n',
DATA_ATTR_NAME = 'data-line-number';

// string format
Expand Down Expand Up @@ -75,7 +75,8 @@
// convert options
var firstLineIndex = !!options.singleLine ? 0 : 1;

var lines = getLines(element.innerHTML);
var text = changeMultilineComments(element);
var lines = getLines(text);

if (lines.length > firstLineIndex) {
var html = '';
Expand Down Expand Up @@ -105,6 +106,37 @@
}
}

function duplicateMultilineTag(element){
var result = '';
var lines = element.innerText.split(/\r\n|\r|\n/g);
for(var i = 0; i<lines.length; i++){
element.innerText = lines[i];
result += element.outerHTML;
if(i < lines.length -1)
result += '\n';
}
return result;
}

function changeMultilineComments(element) {
var result = '';
var children = element.childNodes;
for(var child in children){
if(children.hasOwnProperty(child)) {
if(children[child].className === "hljs-comment"){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing against "hljs-string" also fixes the highlighting of multi-line strings

result += duplicateMultilineTag(children[child]);
}
else if(children[child].childNodes.length) {
result += children[child].outerHTML;
}
else {
result += children[child].textContent;
}
}
}
return result;
}

function getLines(text) {
if (text.length === 0) return [];
return text.split(/\r\n|\r|\n/g);
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy