Obviously, this only works on code blocks (<pre><code>
) and not for inline code.
You specify the lines to be highlighted through the data-line
attribute on the <pre>
element, in the following simple format:
Examples:
In case you want the line numbering to be offset by a certain number (for example, you want the 1st line to be number 41 instead of 1, which is an offset of 40), you can additionally use the data-line-offset
attribute.
You can also link to specific lines on any code snippet, by using the following as a url hash: #{element-id}.{lines}
where
{element-id}
is the id of the <pre>
element and {lines}
is one or more lines or line ranges that follow the format
outlined above. For example, if there is an element with id="play"
on the page, you can link to lines 5-6 by linking to #play.5-6
If line numbers are also enabled for a code block and the <pre>
element has an id, you can add the linkable-line-numbers
class to the <pre>
element. This will make all line numbers clickable and when clicking any line number, it will change the hash of the current page to link to that specific line.
Even with some extra content before the code
element.
Some content