Skip to content
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

Markdown render hooks for code blocks #6702

Closed
acalvo opened this issue Jan 2, 2020 · 8 comments
Closed

Markdown render hooks for code blocks #6702

acalvo opened this issue Jan 2, 2020 · 8 comments
Assignees
Milestone

Comments

@acalvo
Copy link

acalvo commented Jan 2, 2020

Would it be possible to have markdown render hooks for code blocks, just as we do for links and images since v0.62?

Say I want to add a "copy" button in all code snippets in my site. Right now I'm forced to use a shortcode around my code snippets that adds it. If we had something similar to link/image render hooks, we could keep using standard markdown syntax and implement these kind of features in an elegant way.

@acalvo acalvo added the Proposal label Jan 2, 2020
@frerich
Copy link

frerich commented Feb 10, 2020

I would love to have this. Right now, I use a custom short code (e.g. {{< code language="perl" >}}) but that doesn't work too well with common Markdown editors (e.g. Typora).

In case this is implemented, it would be great if the entire info string of fenced code blocks is passed to the render hook. Right now, my short code supports specifying an optional caption for the code snippet, e.g. {{< code language="perl" caption"After refactoring the login function" >}}. It would be great if I coculd use e.g.

```perl "After refactoring the login function"
...
```

or something along the lines of that to specify an optional caption.

@maelle
Copy link

maelle commented May 27, 2020

A render hook for code blocks would also make it possible (I think?) to add links to documentation / hover information on some elements inside the code blocks.

Something like GitHub code navigation.

@stale
Copy link

stale bot commented Oct 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

@stale stale bot added the Stale label Oct 4, 2020
@frerich
Copy link

frerich commented Oct 4, 2020

I'd still greatly appreciate this feature. To avoid that this ticket gets closed due to a lack of activity, I'd like to summarise.

There are many plausible features associated with code blocks:

  • Adding a 'Copy' button next to each code snippet which lets users copy the code block to the clipboard
  • Custom (more semantic?) HTML markup
  • Adding a caption to each code snippet
  • Adding hyperlinks to code blocks (e.g. having function calls automatically reference the relevant API documentation)

Right now, implementing any of these features requires a custom shortcode. However, a shortcode has a number of downsides:

  • Fellow authors need to be taught about it - it's a custom extension to Markdown
  • Tool need to be (but usually cannot be) taught about it. As a consequence, popular Markdown editors like Typora won't highlight code snippets marked up with a custom shortcode as code.

@stale stale bot removed the Stale label Oct 4, 2020
@kdabir
Copy link

kdabir commented Mar 9, 2021

I can add one more use case

To be able to process something like mermaid using standard code fence

```mermaid
     graph TD
         Start --> Stop
```

Shortcodes block are not rendered as code in any text editors or even on Github, making it very hard to read block as a Markdown content. for example, the following content loses indentation, very bad for copy paste:

{{< mermaid >}}
graph TD
Start --> Stop
{{< /mermaid >}}

I would love to see hooks for code blocks and even inline code. IMHO, the more we can get out from standard markdown syntax the better it is for the entire ecosystem 🙏 .

@bep bep added Enhancement and removed Proposal labels Mar 9, 2021
@bep bep added this to the v0.82 milestone Mar 9, 2021
@bep
Copy link
Member

bep commented Mar 9, 2021

In case this is implemented, it would be great if the entire info string of fenced code blocks is passed to the render hook.

You would get a map of .Attributes in your hook, so you could make up your own, e.g.:

```bash
echo "foo";
```
{comment="Hugo is cool"}

Which would allow this hook:

{{ comment := .Attributes.comment }}

(note to self: #8313)

@bep bep modified the milestones: v0.82, v0.83 Mar 21, 2021
jeremy-jameson added a commit to technology-toolbox/techtoolbox-hugo that referenced this issue Apr 9, 2021
@bep bep modified the milestones: v0.83, v0.84 May 1, 2021
@bep bep modified the milestones: v0.84, v0.85 Jun 18, 2021
@bep bep modified the milestones: v0.85, v0.86 Jul 5, 2021
@bep bep modified the milestones: v0.86, v0.87, v0.88 Jul 26, 2021
@bep bep modified the milestones: v0.88, v0.89 Sep 2, 2021
@bep bep modified the milestones: v0.89, v0.90 Nov 2, 2021
@bep bep modified the milestones: v0.90, v0.91.0 Dec 13, 2021
@shafiemukhre
Copy link

I really need this, is anyone is working on this? I would like to work on this and appreciate any guidance or direction and files that I need to update/modified? Thank you so much

This will be my first issue

@bep bep modified the milestones: v0.91.0, v0.92.0 Dec 22, 2021
@bep bep modified the milestones: v0.92.0, v0.93.0 Jan 12, 2022
@bep bep self-assigned this Feb 23, 2022
bep added a commit to bep/hugo that referenced this issue Feb 24, 2022
You can now create custom hook templates for code blocks, either one for all (`render-codeblock.html`) or for a given code language (e.g. `render-codeblock-go.html`).

We also used this new hook to add support for diagrams in Hugo:

* Goat (Go ASCII Tool) is built-in and enabled by default; just create a fenced code block with the language `goat` and start draw your Ascii diagrams.
* Another popular alternative for diagrams in Markdown, Mermaid (supported by GitHub), can also be implemented with a simple template. See the Hugo documentation for more information.

Updates gohugoio#7765
Closes gohugoio#9538
Fixes gohugoio#9553
Fixes gohugoio#8520
Fixes gohugoio#6702
Fixes gohugoio#9558
bep added a commit to bep/hugo that referenced this issue Feb 24, 2022
You can now create custom hook templates for code blocks, either one for all (`render-codeblock.html`) or for a given code language (e.g. `render-codeblock-go.html`).

We also used this new hook to add support for diagrams in Hugo:

* Goat (Go ASCII Tool) is built-in and enabled by default; just create a fenced code block with the language `goat` and start draw your Ascii diagrams.
* Another popular alternative for diagrams in Markdown, Mermaid (supported by GitHub), can also be implemented with a simple template. See the Hugo documentation for more information.

Updates gohugoio#7765
Closes gohugoio#9538
Fixes gohugoio#9553
Fixes gohugoio#8520
Fixes gohugoio#6702
Fixes gohugoio#9558
bep added a commit to bep/hugo that referenced this issue Feb 24, 2022
You can now create custom hook templates for code blocks, either one for all (`render-codeblock.html`) or for a given code language (e.g. `render-codeblock-go.html`).

We also used this new hook to add support for diagrams in Hugo:

* Goat (Go ASCII Tool) is built-in and enabled by default; just create a fenced code block with the language `goat` and start draw your Ascii diagrams.
* Another popular alternative for diagrams in Markdown, Mermaid (supported by GitHub), can also be implemented with a simple template. See the Hugo documentation for more information.

Updates gohugoio#7765
Closes gohugoio#9538
Fixes gohugoio#9553
Fixes gohugoio#8520
Fixes gohugoio#6702
Fixes gohugoio#9558
@bep bep closed this as completed in 08fdca9 Feb 24, 2022
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants