-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Highlight specific lines of code using comments #7296
Comments
Thanks for suggesting! Definitely a sound use case, and yes, the current situation is not ideal. Line highlighting is implemented in I'm taking this back with me to the drawing board where I'm currently at, thinking deeply about the UX/DX/AX of Material for MkDocs. For now, we'd consider this upstream, but we'll look into improving the highlighting capabilities. |
Just another idea: as mentioned, you could write a simple custom fence that pre-processes each code block looking for |
Got any quick link on how to write and package one as part of the build?
I might be able to do that over next weekend :)
…On Wed, 26 Jun 2024 at 11:11, Martin Donath ***@***.***> wrote:
Just another idea: as mentioned, you could write a simple custom fence
that pre-processes each code block looking for # hl or even # hl:start
and # hl:end comments to support multi-line highlighting. This fence
would then extract and remove those from the code block and configure
pygments with the appropriate hl_lines. If that proves to be viable, we
can think about abstracting this further and bring it to the Material for
MkDocs code base.
—
Reply to this email directly, view it on GitHub
<#7296 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAZPQBAKQS2RFGWBZOXOSM3ZJKAS5AVCNFSM6AAAAABJ5JB77KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJRGIYDENJVHA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
The documentation for custom fences should contain all you need. One thing I am not sure about is how to do some processing and then allow the standard processing to take place afterwards. Perhaps @facelessuser can comment on that. Would it be possible to modify the code block in a validator and return |
Hmm, I searched but no, not really, except for the official documentation on custom fences. Maybe @facelessuser has a quick snippet where a custom fence extends / pre-configures the functionality of another fence? As TL;DR, turning this: ``` py
def bubble_sort(items):
for i in range(len(items)): # hl
for j in range(len(items) - 1 - i): # hl
if items[j] > items[j + 1]:
items[j], items[j + 1] = items[j + 1], items[j] #hl
``` Into this: ``` py hl_lines="2-3,5"
def bubble_sort(items):
for i in range(len(items)):
for j in range(len(items) - 1 - i):
if items[j] > items[j + 1]:
items[j], items[j + 1] = items[j + 1], items[j]
``` ... and then feeding it into Pygments. |
@alexvoss is faster than Lucky Luke 🤠 Sorry for double-tagging, Issac. |
Hello everyone! I came with the same question that @heitorlessa has about how we can generate comment highlights in the code instead of having to do it in the I tried some solutions that were given here to create a Custom fence configuration:Custom fence code:def pseudo_highlight(src="", language="", class_name=None, options=None, md="", **kwargs):
"""Formatter wrapper."""
return """
```python hl_lines="1"
import json
```
""" ResultIs there any chance to reopen this issue and you include this support in the project? Thanks |
I agree with this being a useful feature/addition. However, this is a feature request that must be raised either to Python Markdown Extensions or Pygments (?). We can't do anything about it in Material for MkDocs. |
Thanks for the reply @squidfunk! I don't have much knowledge in either project, but I'll read the code for both and try to understand where it fits best. If you have any suggestions/guidance, I'd love to hear them. By the way, after I open the issue with my use case, does it make sense for you to try to influence the decision by saying that this adds value to Material for the Mkdocs project and can benefit customers? Since this project is widely used, your opinion carries a lot of weight. But it's just an idea. Thanks again. |
So from my experience, there are some people that value my opinion and there are some that don't. That's just about how it is with every opinion, you'll find some that share it and some that don't |
I'm the author of Pymdown Extensions, which provides SuperFences. I'm going to be honest, I'm not a huge fan of using comments in code blocks to highlight lines.
While not impossible, I'm not sure it is something I'm willing to invest time in for the above reasons. Those willing to do so can utilize custom fences to do this work if they desire. If there are questions on how to do so, I'm willing to answer such questions as I have time. Feel free to open a discussion in https://github.com/facelessuser/pymdown-extensions/discussions. |
Thank you both for maintaining amazing open source projects that help customers immensely, that is priceless. And thanks for providing a space for discussion @facelessuser, but I don't see much point in opening a discussion for a decision that seems to have already been made. I'll wait for the community to get involved (or not) in some similar demand. |
It wasn't about opening a discussion on a decision already made, but about custom fences:
|
Aaa ok @facelessuser! Sorry for my misunderstanding. I'll open a discussion to see how I can make it work with custom fences. I tried, but I seem to be failing on some things. |
Context
As of today, you can highlight lines using
hl_lines=
syntax when adding a code block. This is great when you start, or when you rarely update the code block.As we grow way beyond 500 code snippets, we run into issues where sometimes a new import, or code snippet improvements break highlight. Two years ago we also added formatting and linting of all code snippets for quality and consistency, but it aggravated the issue at scale.
For example, if we make a minor update to the code snippet above it will break the highlight:
Description
It'd be great to be able to highlight a specific code line using comments. For example:
This removes the need to manually fix each code snippet
hl_lines
whenever there's a change in the code that shifts the line number where the highlight is. This is similar to the great code annotation feature today, whereas instead of# (1)
we'd use another term to highlight it.Related links
Use Cases
In our case, we want to add
from __future__ import annotations
to all code snippets for older Python versions (e.g., Python 3.8), along with other enhancements. This however would turn into days of work to change, format, lint, verify, and fix each code highlighted line we had before / after the change.Off the top of my head, this would benefit people:
One of the examples that would break should we make that change: https://docs.powertools.aws.dev/lambda/python/latest/utilities/batch/#recommended
Visuals
Before submitting
The text was updated successfully, but these errors were encountered: