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

Request support for heading offset #888

Open
xpe opened this issue May 15, 2024 · 1 comment
Open

Request support for heading offset #888

xpe opened this issue May 15, 2024 · 1 comment

Comments

@xpe
Copy link

xpe commented May 15, 2024

Would you be open to adding a configuration option to allow for offsetting of headings?

For example, adding an offset of 2 would map #-><h3> and ##-><h4> and so on.

Why

Hand-editing Markdown levels is often described as a workaround. However, this workaround fails if the same Markdown file gets included by different templates using different nesting levels for headers.

Related Work

For example, this has been implemented in at least one markdown processor; see BlackFriday (GoLang):

// HTMLRendererParameters is a collection of supplementary parameters tweaking
// the behavior of various parts of HTML renderer.
type HTMLRendererParameters struct {
	// ... <content snipped by xpe> ...      
	// Increase heading levels: if the offset is 1, <h1> becomes <h2> etc.
	// Negative offset is also valid.
	// Resulting levels are clipped between 1 and 6.
	HeadingLevelOffset int
}
@ollpu
Copy link
Collaborator

ollpu commented May 16, 2024

This can be done in a postprocessor (this one only does positive offsets, but you get the idea):

fn heading_offset<'a>(
    parser: impl Iterator<Item = Event<'a>>,
    offset: usize,
) -> impl Iterator<Item = Event<'a>> {
    parser.map(move |mut event| {
        match &mut event {
            Event::Start(Tag::Heading { level, .. }) | Event::End(TagEnd::Heading(level)) => {
                *level = (*level as usize + offset)
                    .try_into()
                    .unwrap_or(HeadingLevel::H6);
            }
            _ => {}
        }
        event
    })
}

I don't know if we want this in the main crate, but we could have a pulldown-cmark-contrib crate for things like this.

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

No branches or pull requests

2 participants