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 hook - table #9316

Closed
coliff opened this issue Dec 23, 2021 · 12 comments · Fixed by #12809
Closed

Markdown render hook - table #9316

coliff opened this issue Dec 23, 2021 · 12 comments · Fixed by #12809

Comments

@coliff
Copy link
Member

coliff commented Dec 23, 2021

Markdown render hooks are awesome!

I'd be very happy to see support added for tables too. Some use cases could be to add specific position:sticky classes to the headers, default CSS classes, wrapping the table in a div for horizontal scrolling of the the table on smaller screens etc.

@bep
Copy link
Member

bep commented Dec 23, 2021

I agree about the awesomeness, but they are a "hammer solution" that I would prefer to limit its use... Could you not add CSS classes using attributes?

@bep bep added the Proposal label Dec 23, 2021
@coliff
Copy link
Member Author

coliff commented Dec 23, 2021

Users can easily add classes to the <table> itself like so:

| Language        | Setting |
| --------------- | ------- |
| U.S. English    | en-us   |
| Korean          | ko      |
| Turkish         | tr      |
{.table-bordered}

but often I'd like to add JavaScript interactivity to elements within the table - e.g. needing to use specific CSS classes on the <thead> or th etc... or sometimes adding specific IDs for integrating JavaScript enhancements for table sorting etc.

@maxammann
Copy link

There is also another issue which could be solved that way: https://discourse.gohugo.io/t/responsive-tables-in-markdown/10639/7

Users can easily add classes to the <table> itself like so:

I agree, but sometimes its required to apply changes globally. In the issue mentioned above the only proper solution is to add a div around a table.

@github-actions
Copy link

github-actions bot commented Jan 8, 2023

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.

@github-actions github-actions bot added the Stale label Jan 8, 2023
@michaeltlombardi
Copy link

A use case for this that I have is turning markdown tables into interactive roll tables - a visitor can click on a column to select a random row or random cells in each row based on a dice roll called by JavaScript.

I have workarounds for this, but they move my module users further from "normal" Markdown and my user base is indie tabletop game authors rather than web devs or tech writers.

There's other use cases I can think of, like marking a table as sortable or filterable, or otherwise adding more interactivity to the table. AFAIK, any of these sorts of things requires writing a shortcode today.

@github-actions github-actions bot removed the Stale label Feb 4, 2023
@bep bep added this to the v0.131.0 milestone Jul 22, 2024
@bep bep modified the milestones: v0.131.0, v0.133.0 Aug 9, 2024
@sebastiancarlos
Copy link

My two cents: Tables are one of those things that are straightforward in Markdown but can get messy when you need to style them and add functionality for multiple platforms.

A table render hook would be a game-changer here. It'd let writers keep doing their thing in Markdown, while allowing devs to transform the output without touching the content.

@jmooring
Copy link
Member

jmooring commented Aug 29, 2024

Some thoughts about how this might work...

The render hook would receive the following context:

  • .Attributes -- The Markdown attributes, if any.
  • .Ordinal -- The zero-based ordinal of the table on the page.
  • .Page -- A reference to the current page.
  • .PageInner -- A reference to a page nested via the RenderShortcodes method.. (not sure if we need this; perhaps it's available by default)
  • .Position -- The position of the table within the page content.
  • .THead -- A slice of table header rows, where each row is a slice of table cells.
  • .TBody -- A slice of table body rows, where each row is a slice of table cells.

Each table cell would be a struct:

type TableCell struct {
	Text        string (string instead of template.HTML to be consistent with other render hooks)
	Alignment   string // left, center, or right
}

The render hook would look something like this:

<table
  {{- range $k, $v := .Attributes }}
    {{- if $v }}
      {{- printf " %s=%q" $k $v | safeHTMLAttr }}
    {{- end }}
  {{- end }}>
  <thead>
    {{- range .THead }}
      <tr>
        {{- range . }}
          <th {{ printf "style=%q" (printf "text-align: %s" .Alignment) | safeHTMLAttr }}>
            {{- .Text | safeHTML -}}
          </th>
        {{- end }}
      </tr>
    {{- end }}
  </thead>
  <tbody>
    {{- range .TBody }}
      <tr>
        {{- range . }}
          <td {{ printf "style=%q" (printf "text-align: %s" .Alignment) | safeHTMLAttr }}>
            {{- .Text | safeHTML -}}
          </td>
        {{- end }}
      </tr>
    {{- end }}
  </tbody>
</table>

Note that the GFM table spec has no provision for footer rows.

@bep
Copy link
Member

bep commented Aug 29, 2024

So, while @jmooring 's proposed API makes sense, this seems, to me at least, like a lot of effort needed for marginal gain/interest. Hugo's proposal process lacks some ways to measure interest, but I see a handful of "thumbs up" in the above comments, which to me sounds like a marginal feature.

@jmooring
Copy link
Member

jmooring commented Aug 29, 2024

I don't have a sense of how many developers would take advantage of this, but I know we've had a few forum topics related to Bootstrap tables. For example, it would be very convenient to render every table like this:

<div class="table-responsive">
  <table class="table">
    ...
  </table>
</div>

@bep bep modified the milestones: v0.133.0, Unscheduled Aug 29, 2024
@bep bep self-assigned this Aug 29, 2024
@bep bep added Enhancement and removed Proposal labels Aug 29, 2024
@bep bep modified the milestones: Unscheduled, v0.134.0 Aug 29, 2024
@bep bep added the S0 label Aug 29, 2024
@sebastiancarlos
Copy link

Bootstrap tables. For example, it would be very convenient to render every table like this:

<div class="table-responsive">
  <table class="table">
    ...
  </table>
</div>

That's exactly my use-case, by the way.

bep added a commit to bep/hugo that referenced this issue Aug 30, 2024
bep added a commit to bep/hugo that referenced this issue Aug 30, 2024
bep added a commit to bep/hugo that referenced this issue Aug 30, 2024
bep added a commit to bep/hugo that referenced this issue Aug 30, 2024
bep added a commit to bep/hugo that referenced this issue Aug 30, 2024
bep added a commit to bep/hugo that referenced this issue Aug 30, 2024
bep added a commit to bep/hugo that referenced this issue Aug 30, 2024
bep added a commit to bep/hugo that referenced this issue Aug 30, 2024
bep added a commit to bep/hugo that referenced this issue Aug 30, 2024
bep added a commit to bep/hugo that referenced this issue Aug 31, 2024
bep added a commit to bep/hugo that referenced this issue Aug 31, 2024
bep added a commit to bep/hugo that referenced this issue Aug 31, 2024
bep added a commit to bep/hugo that referenced this issue Aug 31, 2024
@scopartu
Copy link

Wrapping the tables with a division is my main use case. Right now I am manipulating the .Content with replaceRE to achieve that. Kudos on the progress here!

bep added a commit to bep/hugo that referenced this issue Aug 31, 2024
@bep bep closed this as completed in f738669 Aug 31, 2024
sebastiancarlos pushed a commit to sebastiancarlos/ti.net that referenced this issue Sep 7, 2024
This commit upgdraes to Hugo 0.134.1 and enables Bootstrap 5's responsive
tables. This effectively makes the entire site responsive.

Hugo added support for table render hooks in version 111 ([release
notes](https://github.com/gohugoio/hugo/releases/tag/v0.134.0),
[docs](https://gohugo.io/render-hooks/tables/)), which are needed to support
Bootstrap 5's responsive tables
([source](gohugoio/hugo#9316 (comment))).
sebastiancarlos pushed a commit to sebastiancarlos/ti.net that referenced this issue Sep 7, 2024
This commit upgrades to Hugo 0.134.1 and enables Bootstrap 5's responsive
tables. This effectively makes the entire site responsive.

Hugo added support for table render hooks in version 0.134.1 ([release
notes](https://github.com/gohugoio/hugo/releases/tag/v0.134.0),
[docs](https://gohugo.io/render-hooks/tables/)), which are needed to support
Bootstrap 5's responsive tables
([source](gohugoio/hugo#9316 (comment))).
sebastiancarlos pushed a commit to sebastiancarlos/tw.org that referenced this issue Sep 7, 2024
This commit upgrades to Hugo 0.134.1 and enables Bootstrap 5's
responsive tables. This effectively makes the entire site responsive.

Hugo added support for table render hooks in version 0.134.1 ([release
notes](https://github.com/gohugoio/hugo/releases/tag/v0.134.0),
[docs](https://gohugo.io/render-hooks/tables/)), which are needed to
support Bootstrap 5's responsive tables ([source](gohugoio/hugo#9316
(comment))).

Also, make sure the rendered SHAs break correctly on mobile by adding a
text-break shortcode.
sebastiancarlos pushed a commit to sebastiancarlos/tw.org that referenced this issue Sep 7, 2024
This commit upgrades to Hugo 0.134.1 and enables Bootstrap 5's
responsive tables. This effectively makes the entire site responsive.

Hugo added support for table render hooks in version 0.134.1 ([release
notes](https://github.com/gohugoio/hugo/releases/tag/v0.134.0),
[docs](https://gohugo.io/render-hooks/tables/)), which are needed to
support Bootstrap 5's responsive tables ([source](gohugoio/hugo#9316
(comment))).

Also, make sure the rendered SHAs break correctly on mobile by adding a
text-break shortcode.
sebastiancarlos pushed a commit to sebastiancarlos/tw.org that referenced this issue Sep 7, 2024
This commit upgrades to Hugo 0.134.1 and enables Bootstrap 5's
responsive tables. This effectively makes the entire site responsive.

Hugo added support for table render hooks in version 0.134.1 ([release
notes](https://github.com/gohugoio/hugo/releases/tag/v0.134.0),
[docs](https://gohugo.io/render-hooks/tables/)), which are needed to
support Bootstrap 5's responsive tables
([source](gohugoio/hugo#9316 (comment))).

Also, make sure the rendered SHAs break correctly on mobile by adding a
text-break shortcode.
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 Sep 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants