Changeset View
Changeset View
Standalone View
Standalone View
assets/src/bundles/webapp/code-highlighting.js
Show All 30 Lines | |||||
export function scrollToLine(lineDomElt) { | export function scrollToLine(lineDomElt) { | ||||
if ($(lineDomElt).closest('.swh-content').length > 0) { | if ($(lineDomElt).closest('.swh-content').length > 0) { | ||||
$('html, body').animate({ | $('html, body').animate({ | ||||
scrollTop: $(lineDomElt).offset().top - 70 | scrollTop: $(lineDomElt).offset().top - 70 | ||||
}, 500); | }, 500); | ||||
} | } | ||||
} | } | ||||
export async function highlightCode(showLineNumbers = true) { | export async function highlightCode(showLineNumbers = true, selector = 'code') { | ||||
await import(/* webpackChunkName: "highlightjs" */ 'utils/highlightjs'); | await import(/* webpackChunkName: "highlightjs" */ 'utils/highlightjs'); | ||||
// function to highlight lines based on a url fragment | // function to highlight lines based on a url fragment | ||||
// in the form '#Lx' or '#Lx-Ly' | // in the form '#Lx' or '#Lx-Ly' | ||||
function parseUrlFragmentForLinesToHighlight() { | function parseUrlFragmentForLinesToHighlight() { | ||||
const lines = []; | const lines = []; | ||||
const linesRegexp = new RegExp(/L(\d+)/g); | const linesRegexp = new RegExp(/L(\d+)/g); | ||||
Show All 15 Lines | if (lines.length === 1) { | ||||
for (let i = lines[0] + 1; i <= lines[lines.length - 1]; ++i) { | for (let i = lines[0] + 1; i <= lines[lines.length - 1]; ++i) { | ||||
highlightLine(i); | highlightLine(i); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
$(document).ready(() => { | $(document).ready(() => { | ||||
// highlight code and add line numbers | // highlight code and add line numbers | ||||
$('code').each((i, elt) => { | $(selector).each((i, elt) => { | ||||
hljs.highlightElement(elt); | hljs.highlightElement(elt); | ||||
if (showLineNumbers) { | if (showLineNumbers) { | ||||
hljs.lineNumbersElement(elt, {singleLine: true}); | hljs.lineNumbersElement(elt, {singleLine: true}); | ||||
} | } | ||||
}); | }); | ||||
if (!showLineNumbers) { | if (!showLineNumbers) { | ||||
return; | return; | ||||
Show All 38 Lines |