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

Customize markdown headings rendering #3204

Open
fbeaufume opened this issue Feb 18, 2024 · 3 comments
Open

Customize markdown headings rendering #3204

fbeaufume opened this issue Feb 18, 2024 · 3 comments

Comments

@fbeaufume
Copy link

fbeaufume commented Feb 18, 2024

Operating system

Windows 10

Eleventy

1.0.2

Describe the bug

Because of some conflict between Bootstrap fixed navbar and HTML sections, I'd like to customize the HTML generation of level 3 headings.

I'd like ### Some title to produce <section id="some-title"><h3 class="py-3">Some title</h3></section>.

Currently I'm using a paired shortcode, but this is intrusive and not WYSIWYG friendly in my text editor (IntelliJ), as I have to use {% section %}Some title{% endsection %} in the markdown template.

So, is there a non intrusive solution, i.e. without impacting the markdown content ?

@pdehaan
Copy link
Contributor

pdehaan commented Feb 18, 2024

I don't think this is an Eleventy issue, and you'll probably want to solve it at a markdown-it level… if we can find the correct markdown-it plugins.

This might work: https://www.npmjs.com/package/markdown-it-header-sections; or at least partially solve it. But this example in their README looked relevant:

# great stuff {.jumbotron}
lorem
 
click me {.btn .btn-default}

OUTPUT

<section class="jumbotron">
  <h1 class="jumbotron">great stuff</h1>
  <p>lorem</p>
  <p class="btn btn-default">click me</p>
</section>

They also mentioned https://github.com/valeriangalliat/markdown-it-anchor which might be useful too.

@pdehaan
Copy link
Contributor

pdehaan commented Feb 18, 2024

And since I still had these tabs open, I wonder if this gets you any closer: https://www.npmjs.com/package/markdown-it-attrs#custom-rendering (and the linked https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer docs).

Not entirely sure how to specifically hook into rules for <h3>, but it may be related to https://github.com/markdown-it/markdown-it/blob/master/lib/rules_block/heading.mjs or maybe some tips in markdown-it/markdown-it#117.

UPDATE: Also https://github.com/markdown-it/markdown-it/blob/master/docs/examples/renderer_rules.md

@fbeaufume
Copy link
Author

fbeaufume commented Feb 27, 2024

Thank you for the suggestions.

Markdown-it-header-sections may do the trick,

Custom renderers also seems very nice to customize the produced HTML. That's what I was originally looking for.

I found a pure CSS solution (using scroll-margin-top), that does not require to wrap the <h3> in a <section> (or <div>), meaning that I no longer need to tweak the markdown files, using markdown-it-attrs is enough.

I guess this issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants