Support line numbers for any block of code
Hi,
I am not able to make the code line numbers show. Here is the book.toml file. And I am using the Windows binary.
I am trying to follow the instructions here: https://rust-lang.github.io/mdBook/format/config.html
Here is an example of the rendered page: https://easiest.studio/php/zh-cn/chapter_1.html
Thanks!
Dallas
[book] authors = ["曹首光"] language = "zh-cn" multilingual = false src = "src"
[output.html] default-theme = "light" preferred-dark-theme = "navy"
[output.html.fold] enable = false level = 0
[output.html.playground] editable = true copyable = true copy-js = true line-numbers = true
The playground only supports Rust. I do not see any Rust code on your sample page.
Thanks for the explanation Ehuss and thank you for the great work!
Is there any plan to support adding support line numbers for other languages apart from Rust
Some rationale behind this: if the intent of mdbook is writing books dealing primarily with source code, then the need for having numbered code examples is great. Take the following for example:
On line 42, we....
With the current level of support, we are expecting the reader to count 42 lines by hand or input the text into an editor and determine which is line 42. Neither of these is ideal. I'm all ears for alternative solutions that resolve this issue.
I was looking into the source code to figure out how that works (not sure if this was mentioned in the docs but if it was then I missed it), I found this test which asserts that any arg passed in the code block generates a CSS class.
Since that is the case, isn't adding line numbers rather straight forward? A developer's famous last words I know.
I'm not really a CSS person but we could perhaps do something similar to this if an argument show_line_numbers is present?
It would be much nicer if the line numbers are shown without the need for the code to be editable.
Heya, I've roughly implemented this:
- Demo: https://azriel.im/mdbook_line_numbers/
- Setup: https://azriel.im/mdbook_line_numbers/setup.html#create-the-file
Currently I use it with additional-js.
Tested:
-
:white_check_mark: code copy button
-
:white_check_mark: copy using Ctrl C
-
:white_check_mark: hidden lines
-
:white_check_mark: theming (through
doc/theme/highlight.css):
The implementation could probably be cleaned up some more -- I cloned https://github.com/wcoder/highlightjs-line-numbers.js and worked backwards (swapping JS + table elements for CSS + div).
But also, maybe swapping highlightjs for syntect for static generation is a more favourable option.