-
Notifications
You must be signed in to change notification settings - Fork 123
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
Support line numbers for code blocks #991
Conversation
f2dcdeb
to
ebb868b
Compare
Nice. Can we make it the default? i.e., all fenced code blocks will have line numbers by default. |
Sure, then users can disable it similarly (or add any other classes they want) by doing |
Yes please do provide the option to disable it. |
Ok changes made, Now all code fences that specify a language will have numbered lines. Add the class |
asset/css/markbind.css
Outdated
@@ -297,3 +297,28 @@ panel[minimized]:not([expanded])::before { | |||
li.footnote-item:target { | |||
background-color: #eee; | |||
} | |||
|
|||
pre { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this rule be changed to apply on .hljs
instead? I doubt it will have an observable effect but it would be better practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean pre.hljs
instead?
What do you think of supporting arbitrary starting line numbers? I could see some use cases where the author wants to show a short code snippet without showing the whole file. |
When a language is not specified, the .hljs class is not applied by markdown it, which is probably why the old solution was used in the first place to add the .hljs class to code fences with no language. Bc But reverting the change would break markdown-it-attrs for code fences. |
Is it possible for MarkBind to add the class back in after all markdown syntax has been rendered (i.e. some post-rendering processing)? |
I made code highlighting a new renderer rule instead of using the This allows us to manually render the attributes parsed from
This would require a separate markdown plugin, as it is not possible (i.e. not nice) with pure css. On a separate note, why do inline styles not work with markdown it attrs?
I tried doing this, The inline style does get parsed and rendered in renderAttrs, but gets stripped out in the final html: I tried it with |
This page seems to be broken https://deploy-preview-991--markbind-master.netlify.com/userguide/reusingcontents |
Forgot to manually escape code blocks without language, Thanks! |
Ready for review! |
asset/css/markbind.css
Outdated
@@ -298,7 +298,7 @@ li.footnote-item:target { | |||
background-color: #eee; | |||
} | |||
|
|||
pre { | |||
pre.hljs { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class .hljs
is applied to the <code>
inside of <pre>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed! thanks
Thanks! I've edited them, looked for instances of |
Fixes #1017, too with the added
suggested in that issue Before hovering: |
# Conflicts: # test/functional/test_site/expected/siteData.json
* 'master' of https://github.com/MarkBind/markbind: Update tests Allow using 'none' footer attribute in frontmatter (MarkBind#1002) Support line numbers for code blocks (MarkBind#991) 2.11.0 Update test files due to changes in PR MarkBind#982 Update vue-strap version to v2.0.1-markbind.36 Make highlighting bold (MarkBind#1045) Support markdown for header attr in dropdown (MarkBind#1029) Add '_site' to the ignored folders in site.json (MarkBind#1046) Use path.join instead of string interpolation (MarkBind#1052) Implement box markdown header attributes parsing (MarkBind#1025) Make the position of top navbar fixed (MarkBind#982) Exclude *.md files from being copied over on build (MarkBind#1010) # Conflicts: # docs/css/main.css
Works in production. Nice! 👍 Thanks @openorclose and reviewers. Let's push ahead and make our code blocks the best of class! |
What is the purpose of this pull request? (put "X" next to an item, remove the rest)
• [ ] Documentation update
• [ ] Bug fix
• [ ] New feature
• [x] Enhancement to an existing feature
• [ ] Other, please explain:
Partially fixes #473 (only adding support for line nos),
Fixes #1023
What is the rationale for this request?
What changes did you make? (Give an overview)
Small bugfix:
Our code fences (
```
) do not support markdown-it-attrs even though one of the examples there shows it should.Turns out this is due to our overriding of the syntax highlighting.
We copied the second example from markdown-it, so that we could add our own
hljs
class to the<pre>
tag, but this breaks markdown-it-attrs since the attrs there do not get added to our hardcodedpre
tag.So instead we provide our own renderer to render the attributes parsed by markdown-it-attrs.
Actual implementation
There is no way to directly use CSS and determine when line breaks happen, so we change
highlightLines
in markdown-it to split every\n
and wrap aspan
around them.Then, we use some css to insert the line numbers to it.
Line numbers will be shown by default, and authors can disable it by adding the class
no-line-numbers
.Provide some example code that this change will affect:
Before and After:
If line numbers are not wanted:
Is there anything you'd like reviewers to focus on?
Check out the 2103 textbook here, where all code snippets have line numbers:
(the search on top doesn't work, it will bring you to the actual 2103 book site)
https://openorclose.github.io/se-book/
These pages have many code snippets:
Testing instructions:
Verify that
<span>
s get added to html of code fences, and manually check generated code fences for line numbers.Proposed commit message: (wrap lines at 72 characters)
// TODO when design finalised