Skip to content

Commit c6f5789

Browse files
authored
Merge pull request #3229 from metacpan/haarg/fix-synhi-whitespace
Fix whitespace handling in syntaxhighlighter
2 parents 73dc5d1 + 185dd14 commit c6f5789

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

root/static/js/syntaxhighlighter.mjs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,24 +132,29 @@ const processPackages = function(code) {
132132
return code.replace(/(<code class="p(?:er)?l keyword">(use|package|require)<\/code> <code class="p(?:er)?l plain">)([A-Z_a-z][0-9A-Z_a-z]*(?:::[0-9A-Z_a-z]+)*)(.*?<\/code>)/g, replace_pattern);
133133
};
134134

135-
const getCodeLinesHtml = Renderer.prototype.getCodeLinesHtml;
136-
Renderer.prototype.getCodeLinesHtml = function() {
137-
// the syntax highlighter has a bug that strips spaces from the first line.
138-
// replace any leading whitespace with an entity, preventing that.
139-
// html = html.replace(/^ /, "&#32;");
140-
// html = html.replace(/^\t/, "&#9;");
141-
let html = getCodeLinesHtml.apply(this, arguments);
142-
return processPackages.call(this, html);
135+
const processUrls = Renderer.prototype.processUrls;
136+
Renderer.prototype.processUrls = function(html, ...args) {
137+
html = processPackages.apply(this, [html]);
138+
html = processUrls.apply(this, [html, ...args]);
139+
return html;
143140
};
144141

145142
const getHtml = Renderer.prototype.getHtml;
146-
Renderer.prototype.getHtml = function() {
147-
let html = getHtml.call(this);
143+
Renderer.prototype.getHtml = function(...args) {
144+
let html = getHtml.call(this, ...args);
148145
html = html.replace(/\s+(<(tbody|table|div)\b)/g, '$1');
149146
html = html.replace(/(<\/(tbody|table|div)>)\s+/g, '$1');
150147
return html;
151148
};
152149

150+
const wrapLine = Renderer.prototype.wrapLine;
151+
Renderer.prototype.wrapLine = function(lineIndex, lineNumber, lineHtml) {
152+
if (lineHtml == ' ') {
153+
lineHtml = '';
154+
}
155+
return wrapLine.call(this, lineIndex, lineNumber, lineHtml);
156+
};
157+
153158
// on pod pages, set the language to perl if no other language is set
154159
CODE: for (const code of document.querySelectorAll(".pod pre > code")) {
155160
for (const className of code.classList) {
@@ -231,14 +236,44 @@ for (const code of document.querySelectorAll(".content pre > code")) {
231236

232237
config.package_target_type = source ? 'source' : 'pod';
233238

234-
// highlighter strips leading blank lines, throwing off line numbers.
235-
// add a blank line for the highlighter to strip
236-
// const html = code.innerHTML;
237-
// if (html.match(/^ *\n/)) {
238-
// code.innerHTML = "\n " + html;
239-
// }
239+
let highlightObject = code;
240+
241+
const html = code.innerHTML;
242+
if (html.match(/^ *\n+/)) {
243+
// highlighter strips leading blank lines, throwing off line numbers.
244+
// use this awful hack to bypass it. depends on specific details inside
245+
// the syntaxhighlighter module
246+
247+
const fakeCode = {
248+
className: code.className,
249+
id: code.id,
250+
title: code.title,
251+
innerHTML: {
252+
toString: function() {
253+
return html
254+
},
255+
replace: function(search, replace) {
256+
if (search.toString() == /^[ ]*[\n]+|[\n]*[ ]*$/g.toString()) {
257+
return html.replace(/\n$/g, '');
258+
}
259+
return html.replace(search, replace);
260+
},
261+
},
262+
};
263+
const parentNode = code.parentNode;
264+
fakeCode.parentNode = {
265+
replaceChild: function(newEl, oldEl) {
266+
if (oldEl === fakeCode) {
267+
oldEl = code
268+
}
269+
parentNode.replaceChild(newEl, oldEl);
270+
},
271+
};
272+
273+
highlightObject = fakeCode;
274+
}
240275

241-
SyntaxHighlighter.highlight(config, code);
276+
SyntaxHighlighter.highlight(config, highlightObject);
242277

243278
const pod_lines = pre.dataset.podLines;
244279
if (pod_lines) {

root/static/less/syntaxhighlighter-theme.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
.line {
2424
white-space: pre;
25+
min-height: 1.2em;
2526
}
2627

2728
// main table and columns

0 commit comments

Comments
 (0)