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

Add AutoHeadingAnchorLinks extension #543

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from

Conversation

matt-allan
Copy link

This PR adds an extension 'AutoHeadingAnchorLinks' that can be used to automatically add an anchor link to each header.

This is used to generate HTML similar to these examples:

It's a pretty common request for Hugo. I'm personally looking to add this feature to my Hugo site as well.

It's possible to workaround the issue with JavaScript or regex but an extension is a little easier and should be less likely to fail.

When the AutoHeadingAnchorLinks and AutoHeadingIDs extensions are enabled this markdown:

# First

Some text

## Second

...would create this HTML:

<h1 id="first"><a href="#first">#</a>First</h1>

<p>Some text</p>

<h2 id="second"><a href="#second">#</a>Second</h2>

It's possible to enable just the AutoHeadingAnchorLinks extension without the AutoHeadingIDs or HeadingIDs extension, it just generates links like <a href="#">#</a>. Not that you would want to do that but it doesn't break anything 😁.

My only concern with this extension is it seems pretty common to want to use a SVG instead of # for the link. It is possible to override this with CSS, i.e.

h1 > a, h2 > a, h3 > a, h4 > a, h5 > a, h6 > a {
  visibility: hidden;
}

h1 > a:before, h2 > a:before, h3 > a:before, h4 > a:before, h5 > a:before, h6 > a:before {
  visibility: visible;
  content: '🔗';
}

@cbednarski
Copy link

@matt-allan, thanks for the PR! I didn't see this at first, and ended up writing another PR that's almost identical. I've declined to submit it since yours is already here.

Since the project seems to have slowed down (and you may have moved on, too), I looked for another way to solve this.

I copied the approach that bfchroma used to add highlighting and wrote a custom renderer. This can live in your own code and obviates changing blackfriday.

If you (the general you) still need this, the code is here: https://gist.github.com/cbednarski/8dcc0c90890613f2b74008dc264bd14f

Comment on lines +268 to +275
if p.extensions&AutoHeadingAnchorLinks != 0 {
link := NewNode(Link)
link.Destination = []byte("#" + id)
linkText := NewNode(Text)
linkText.Literal = []byte("#")
link.AppendChild(linkText)
block.AppendChild(link)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another section which handles headings of the ==== variety; example:

My Heading
==========

This code will need to be copied there, too. I recommend adding a function like this:

func anchorHeadingLink(node Node) {
	link := NewNode(Link)
	link.Destination = []byte("#" + id)
	linkText := NewNode(Text)
	linkText.Literal = []byte("#")
	link.AppendChild(linkText)
	node.AppendChild(link)
}

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

Successfully merging this pull request may close these issues.

2 participants