-
Notifications
You must be signed in to change notification settings - Fork 253
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
feature: implement graphql, json, xml and yaml syntax highlighting #5176
feature: implement graphql, json, xml and yaml syntax highlighting #5176
Conversation
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 straightforward - thank you! As was mentioned in standup, it would be good to add a note to the documentation for render_json
and render_yaml
in nautobot/docs/user-guide/platform-functionality/template-filters.md
.
I wonder actually, should we enhance those two template filters to automatically wrap the result in an appropriate <code class="...
block so that it's auto-magical, or would that be a potentially breaking change in some way?
@glennmatthews, yeah I definitely see your point and I agree that adding a set of wrapping elements with a certain required class is not ideal. I tried to find a common pattern which could automate the process a bit and here's why I didn't go down that path:
|
Great thought/catch. I hadn't considered that case. In that case, let's add this as a highlighted feature under the |
Should we introduce a new template tag that does this though? I want to make sure it is as easy as possible to consume. |
For Norbert's reference, do you mean something like this?
|
I talked it over with John and we think we're OK with adding the |
Let's go with |
Something like (untested): @library.filter()
@register.filter()
def render_json(value, syntax_highlight=True):
"""
Render a dictionary as formatted JSON.
Unless `syntax_highlight=False` is specified, the returned string will be wrapped in a
`<code class="language-json>` HTML tag to flag it for syntax highlighting by highlight.js.
"""
rendered_json = json.dumps(value, indent=4, sort_keys=True, ensure_ascii=False)
if syntax_highlight:
return format_html('<code class="language-json">{}</code>', rendered_json)
return rendered_json |
How difficult/costly is it to enable additional languages? Might be useful at least to include XML as I think there's some work in several Apps that might benefit from that. |
@glennmatthews, adding more languages boils down to increasing library size which ultimately affects page load times. Here's a list of all languages available in highlight.js: Download a Custom Build - highlight.js. Supporting one, two or three more (I believe in Nautobot apart from JSON and YAML I also saw code snippets in GraphQL, Python and XML) shouldn't make much of a difference - it's just a couple of kBs (with no gizp) as long as we just don't grab them all blind. |
…r_yaml template filters
@glennmatthews @lampwins, now when the |
IMO it's worth adding a note in the Release Overview still. |
Let's confirm with @lampwins, but I think it'd be good to add GraphQL and XML now. We could already make use of GraphQL highlighting e.g. in |
Yes, let's add GraphQL and XML but draw the line there. Are we creating new template tags for these? |
nautobot/docs/user-guide/platform-functionality/template-filters.md
Outdated
Show resolved
Hide resolved
nautobot/docs/user-guide/platform-functionality/template-filters.md
Outdated
Show resolved
Hide resolved
I don't think we need to at this time - just maybe document that you need to use |
…rs.md Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
…rs.md Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
…-syntax-highlighting
Co-authored-by: Glenn Matthews <glenn.matthews@networktocode.com>
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.
That's going to be in |
Oh wow, thanks @gsnider2195, due to lack of understanding of that piece of code I must've left it as-is |
…-syntax-highlighting
Closes #5098
What's Changed
github.min.css
andgithub-dark.min.css
.language-graphql
,language-json
,language-xml
andlanguage-yaml
class. These elements preferably have the following HTML structure:<pre><code class="language-json">...</pre></code>
.<code class="language-json">...</code>
and<code class="language-yaml">...</code>
wrappers torender_json
andrender_yaml
Django template filters. This new default behavior can be disabled withsyntax_highlight=False
arg./theme-preview/
page with syntax highlighting examples.Screenshots