-
Notifications
You must be signed in to change notification settings - Fork 124
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
Add heading and line highlighting to code blocks #1034
Conversation
8e578eb
to
90df946
Compare
Heading is a misnomer; these are meant to be additional (and possibly unimportant) info. That's why they are on the right rather than left, away from the readers main path of reading.
What about when the page is converted to pdf? |
Then, should it be given another name, like 'alt-text'? |
I think we should. |
We could use the |
The user may not want to hide it from the print. |
aa87b47
to
0c8df0a
Compare
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.
Thanks for looking at this! Perhaps you can consider changing the base branch to be #991 instead of master to make it easier to review subsequently. I forgot that we work on different forks here 😅
asset/css/markbind.css
Outdated
position: relative; | ||
} | ||
|
||
.code-block > .code-block-heading { |
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.
.code-block-heading
sounds specific enough without having to further qualify it with .code-block >
. Let's remove the unnecessary qualifiers.
docs/userGuide/syntax/code.mbdf
Outdated
</span> | ||
</include> | ||
|
||
##### Using them altogether |
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.
I feel that this section isn't very necessary, since combining selectors should be fairly self explanatory. I'd lean towards removing this section to avoid bloating the user guide.
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.
Turns out we do need it 😅
let highlightLines = token.attrGet('highlight-lines') | ||
let lineNumbers = [] | ||
if (highlightLines) { | ||
lineNumbers = highlightLines.split(',').map(v => v.split('-').map(v => parseInt(v, 10))) |
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.
Some comments here to describe what the input format should look like would be helpful! It would make maintaining this code easier in the future.
Splitting up this line into several steps with well-named intermediate variables could help as well.
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.
Edited.
return currentLineNumber === start | ||
}) | ||
if (inRange) { | ||
return `<span class="highlighted">${line || '​'}</span>`; |
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.
I think we can abstract the section ${line || '​'}
into a separate variable, to make it clearer that this is identical across both cases.
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.
Addressed.
@@ -45,17 +45,44 @@ markdownIt.renderer.rules.fence = (tokens, idx, options, env, slf) => { | |||
if (!highlighted) { | |||
str = markdownIt.utils.escapeHtml(str); | |||
} | |||
let highlightLines = token.attrGet('highlight-lines') |
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 have missing ;
s in this file.
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.
Done. Turns out eslint ignores src/lib/markbind/src/lib/markdown-it/*
😱
if (heading) { | ||
return `<div class='code-block'>` | ||
+ `<div class='code-block-heading'><span>` + heading + `<span></div>` | ||
+ `<div class='code-block-content'><pre><code ${slf.renderAttrs(token)}>${str}</code></pre></div>` |
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.
Same as above, lets abstract the common part into it's own variable.
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.
Done
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.
@@ -45,17 +45,55 @@ markdownIt.renderer.rules.fence = (tokens, idx, options, env, slf) => { | |||
if (!highlighted) { | |||
str = markdownIt.utils.escapeHtml(str); | |||
} | |||
|
|||
const linesToHighlight = token.attrGet('highlight-lines'); |
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.
What do you think of this?
linesToHighlight
-> highlightLinesInput
arrLinesToHighlight
-> highlightLines
This is to make it clearer which is the raw input that needs to be processed, and which are the actual lines to be highlighted.
I generally also don't like using arr
to denote arrays as it leads to unnecessary verbosity IMO. A well named plural variable should be sufficient to convey that.
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.
Looks good to me.
|
ae4b108
to
2b9d5c4
Compare
#991 has been merged. Ready for review. |
3c03216
to
066ddcf
Compare
const heading = token.attrGet('heading'); | ||
const codeBlockContent = `<pre><code ${slf.renderAttrs(token)}>${str}</code></pre>`; | ||
if (heading) { | ||
return `${'<div class=\'code-block\'>' |
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 nested string interpolation feels a little strange to me. I normally do prefer string interpolation to concatenation, but for multi-line strings it gets difficult to read. What do you think of this?
return '<div class="code-block">'
+ `<div class="code-block-heading"><span>${heading}<span></div>`
+ `<div class="code-block-content">${codeBlockContent}</div>`
+ '</div>';
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.
Sorry if this diff was already included when I last reviewed it and I didn't catch it then 🙏
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.
Done 👍
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.
LGTM 🚀
@nbriannl will need you to update the css files, thanks. |
246ee90
to
4feb2e9
Compare
Added heading to code blocks Add line highlighting to code blocks Edit css remove unit for length 0 css (cherry picked from commit da7e23a) Update User Guide (cherry picked from commit aceb551) Remove console log (cherry picked from commit cd8cc26) Update User Guide Update User Guide Update tests Edit CSS Edit CSS Remove User Guide Section Remove uneeded specifity Rewrite code for clarity Edit User Guide Handle wrap for very long single word Improve variable naming Update test sites Add note for optimization Settle rebase Reduce complexity of code sample in User Guide rewrite return string
4feb2e9
to
abdff1e
Compare
CSS files updated. @yamgent |
@nbriannl the netlify preview of this PR has some hover over glitches. Are they being addressed elsewhere? |
@damithc what is the hover over glitch specifically? |
The page width seem to change as I scroll the page. In the top half of the page, the middle column seem to be wider than intended, which pushes the pageNav gets out of view (as seen from the screenshot) but things go back to normal in the 2nd half of the page. Scroll that specific page in the user guide from top to bottom while watching the width of the middle column. Let me know if you can't reproduce it. |
I can reproduce it in this PR. #1077 has been raised regarding this. |
Got it. Seems severe enough problem that needs to be fixed before the next release though. Are you working on it? |
I was not working on it and this is the first time I'm aware regarding this. I'm investigating it now. |
…nvert-to-code-block * 'master' of https://github.com/MarkBind/markbind: Allow changing parameter properties (MarkBind#1075) Custom timezone for built-in timestamp (MarkBind#1073) Fix reload inconsistency when updating frontmatter (MarkBind#1068) Implement an api to ignore content in certain tags (MarkBind#1047) Enable AppVeyor CI (MarkBind#1040) Add heading and line highlighting to code blocks (MarkBind#1034) Add dividers and fix bug in siteNav (MarkBind#1063) Fixed navbar no longer covers modals (MarkBind#1070) Add copy code-block plugin (MarkBind#1043) Render plugins on dynamic resources (MarkBind#1051) Documentation for Implement no-* attributes for <box> (MarkBind#1042) Migrate to bootstrap-vue popovers (MarkBind#1033) Refactor preprocess and url processing functions (MarkBind#1026) Add pageNav to Using Plugins Page (MarkBind#1062) # Conflicts: # docs/userGuide/syntax/siteNavigationMenus.mbdf
* 'master' of https://github.com/MarkBind/markbind: 2.12.0 Update outdated test files Update vue-strap version to v2.0.1-markbind.37 Fix refactor to processDynamicResources (MarkBind#1092) Implement lazy page building for markbind serve (MarkBind#1038) Add warnings for conflicting/deprecated component attribs (MarkBind#1057) Allow changing parameter properties (MarkBind#1075) Custom timezone for built-in timestamp (MarkBind#1073) Fix reload inconsistency when updating frontmatter (MarkBind#1068) Implement an api to ignore content in certain tags (MarkBind#1047) Enable AppVeyor CI (MarkBind#1040) Add heading and line highlighting to code blocks (MarkBind#1034) Add dividers and fix bug in siteNav (MarkBind#1063) Fixed navbar no longer covers modals (MarkBind#1070) Add copy code-block plugin (MarkBind#1043) Render plugins on dynamic resources (MarkBind#1051) Documentation for Implement no-* attributes for <box> (MarkBind#1042) Migrate to bootstrap-vue popovers (MarkBind#1033) Refactor preprocess and url processing functions (MarkBind#1026) Add pageNav to Using Plugins Page (MarkBind#1062)
Note: Will take out of WIP when #991 is approvedNote: This branch is a rebase over #991 in order to work on top of his changes. Will rebase over master once #991 is mergedWhat is the purpose of this pull request? (put "X" next to an item, remove the rest)
• [X] Enhancement to an existing feature
Code headings fixes #646 #645
Partially fixes the requirements of line highlighting in #473
What is the rationale for this request?
For MarkBind to be a developer documentation tool, can be helpful to indicate the language of a code block in the block itself. To make the usage more general, any heading test can be optionally added to a code block, be it a file name & extension, the language used, or a generic heading.
Also, it would be good to be able to highlight specific lines of the code block, so to allow the author to emphasis certain lines.
What changes did you make? (Give an overview)
Because the following are markdown-it-attrs, you can use the original fenced code syntax if you do not want to use these feature.
We overwrite the markdown-it rule for fenced code blocks, to add a heading (which is a div) if a heading is specified as an attribute.
As for the highlight-lines attribute, firstly, the values are parsed into ranges. Secondly, we add upon the implementation of the
map
function in #991 that maps each lines into spans. What this addition is is that if the current line is within any of the ranges that is intended to be highlighted, it is then given the CSS class "highlighted". This also means that it is ok to give an invalid ranges, i.e. a line number that is out of bounds of the number of lines in the code, because the lines are checked against the ranges and not the other way around.CSS changes were also made to enable styling and position of the heading div with respect to the div containing the code.
The headings are meant to be additional (and possibly unimportant) info. That's why they are on the right rather than left, away from the readers main path of reading.
Hence, the following design, here is how it looks like
Provide some example code that this change will affect:
Add the following syntax to any test site and serve
Is there anything you'd like reviewers to focus on?
Please comment if the styling of the component can be improved.
Proposed commit message: (wrap lines at 72 characters)
Add heading and line highlighting to code blocks