How to rewrite header elements #428

Closed
opened 2026-01-29 14:36:26 +00:00 by claunia · 3 comments
Owner

Originally created by @kaylumah on GitHub (Jan 24, 2021).

Hi,

I really like your library, but I am not really clear on which customization hooks I need to use, since there are a lot of hooks available. For many differnt variants, like for example the Figure extension creates custom types alongside parsers and renderers. So I am a bit lost and I thought I ask here for advise.

I think I need something like AutoIdentifiers but I am not sure.

The use case I want to do is make all headers clickable with anchors.

So

# heading one

Would become

<h1><a id="heading-one">heading one</a></h1>

Any pointers are appreciated

Originally created by @kaylumah on GitHub (Jan 24, 2021). Hi, I really like your library, but I am not really clear on which customization hooks I need to use, since there are a lot of hooks available. For many differnt variants, like for example the Figure extension creates custom types alongside parsers and renderers. So I am a bit lost and I thought I ask here for advise. I think I need something like AutoIdentifiers but I am not sure. The use case I want to do is make all headers clickable with anchors. So ``` # heading one ``` Would become ``` <h1><a id="heading-one">heading one</a></h1> ``` Any pointers are appreciated
claunia added the question label 2026-01-29 14:36:26 +00:00
Author
Owner

@xoofx commented on GitHub (Jan 25, 2021):

Autoidentifiers will help to add an id attribute to your heading, so you can then insert your link by post-processing the document before rendering it, something like that:

var pipeline = new MarkdownPipelineBuilder().UseAutoIdentifiers().Build();
var doc = Markdown.Parse("# This is a heading", pipeline);

// Process headings to insert an intermediate LinkInline
foreach (var headingBlock in doc.Descendants<HeadingBlock>())
{
    var inline = new LinkInline($"#{headingBlock.GetAttributes().Id}", null);
    var previousInline = headingBlock.Inline;
    headingBlock.Inline = null;
    inline.AppendChild(previousInline);
    headingBlock.Inline = inline;
}

// Render the doc
var writer = new StringWriter();
var renderer = new HtmlRenderer(writer);
pipeline.Setup(renderer);
renderer.Render(doc);

// Will print:
// <h1 id="this-is-a-heading"><a href="#this-is-a-heading">This is a heading</a></h1>
Console.WriteLine(writer.ToString());
@xoofx commented on GitHub (Jan 25, 2021): Autoidentifiers will help to add an id attribute to your heading, so you can then insert your link by post-processing the document before rendering it, something like that: ```csharp var pipeline = new MarkdownPipelineBuilder().UseAutoIdentifiers().Build(); var doc = Markdown.Parse("# This is a heading", pipeline); // Process headings to insert an intermediate LinkInline foreach (var headingBlock in doc.Descendants<HeadingBlock>()) { var inline = new LinkInline($"#{headingBlock.GetAttributes().Id}", null); var previousInline = headingBlock.Inline; headingBlock.Inline = null; inline.AppendChild(previousInline); headingBlock.Inline = inline; } // Render the doc var writer = new StringWriter(); var renderer = new HtmlRenderer(writer); pipeline.Setup(renderer); renderer.Render(doc); // Will print: // <h1 id="this-is-a-heading"><a href="#this-is-a-heading">This is a heading</a></h1> Console.WriteLine(writer.ToString()); ```
Author
Owner

@kaylumah commented on GitHub (Jan 25, 2021):

@xoofx awesome, can confirm this works, did not expect you to write the code at all, so many thanks :-)

My guess is I can do it like this, or use de BootstrapExtension as the basis to do in in a pipeline right?

@kaylumah commented on GitHub (Jan 25, 2021): @xoofx awesome, can confirm this works, did not expect you to write the code at all, so many thanks :-) My guess is I can do it like this, or use de BootstrapExtension as the basis to do in in a pipeline right?
Author
Owner

@xoofx commented on GitHub (Jan 25, 2021):

@xoofx awesome, can confirm this works, did not expect you to write the code at all, so many thanks :-)

My guess is I can do it like this, or use de BootstrapExtension as the basis to do in in a pipeline right?

Yep for example.

@xoofx commented on GitHub (Jan 25, 2021): > @xoofx awesome, can confirm this works, did not expect you to write the code at all, so many thanks :-) > > My guess is I can do it like this, or use de BootstrapExtension as the basis to do in in a pipeline right? Yep for example.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#428