Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,19 @@ func (p *Markdown) prefixHeading(data []byte) int {
if id == "" && p.extensions&AutoHeadingIDs != 0 {
id = SanitizedAnchorName(string(data[i:end]))
}

block := p.addBlock(Heading, data[i:end])
block.HeadingID = id
block.Level = level

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

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)
}

}
return skip
}
Expand Down
33 changes: 33 additions & 0 deletions block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1916,3 +1916,36 @@ func TestSanitizedAnchorName(t *testing.T) {
}
}
}

func TestPrefixHeaderAnchorLinksExtension(t *testing.T) {
t.Parallel()
var tests = []string{
"# Header 1\n",
"<h1 id=\"header-1\"><a href=\"#header-1\">#</a>Header 1</h1>\n",

"# Header 1 \n",
"<h1 id=\"header-1\"><a href=\"#header-1\">#</a>Header 1</h1>\n",

"## Header 2\n",
"<h2 id=\"header-2\"><a href=\"#header-2\">#</a>Header 2</h2>\n",

"### Header 3\n",
"<h3 id=\"header-3\"><a href=\"#header-3\">#</a>Header 3</h3>\n",

"#### Header 4\n",
"<h4 id=\"header-4\"><a href=\"#header-4\">#</a>Header 4</h4>\n",

"##### Header 5\n",
"<h5 id=\"header-5\"><a href=\"#header-5\">#</a>Header 5</h5>\n",

"###### Header 6\n",
"<h6 id=\"header-6\"><a href=\"#header-6\">#</a>Header 6</h6>\n",

"####### Header 7\n",
"<h6 id=\"header-7\"><a href=\"#header-7\">#</a># Header 7</h6>\n",

"Hello\n# Header 1\nGoodbye\n",
"<p>Hello</p>\n\n<h1 id=\"header-1\"><a href=\"#header-1\">#</a>Header 1</h1>\n\n<p>Goodbye</p>\n",
}
doTestsBlock(t, tests, AutoHeadingIDs|AutoHeadingAnchorLinks)
}
1 change: 1 addition & 0 deletions markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
AutoHeadingIDs // Create the heading ID from the text
BackslashLineBreak // Translate trailing backslashes into line breaks
DefinitionLists // Render definition lists
AutoHeadingAnchorLinks // Create anchor links for headings

CommonHTMLFlags HTMLFlags = UseXHTML | Smartypants |
SmartypantsFractions | SmartypantsDashes | SmartypantsLatexDashes
Expand Down