Skip to content

Commit

Permalink
Add link anchor on each title.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Jan 22, 2024
1 parent 6e0beee commit f91f2a0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Add `before_markdown_render` and `after_markdown_render` hooks, allowing
content manipulation.
- Add link anchor on each title.

## v3.1.0

Expand Down
2 changes: 2 additions & 0 deletions lib/kitabu/toc/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def self.normalize(content)
end

tag.set_attribute("id", permalink)
tag["tabindex"] = "-1"
tag.prepend_child %[<a class="anchor" href="##{permalink}" aria-hidden="true" tabindex="-1"></a>]
end

html.css("body").first.inner_html
Expand Down
4 changes: 4 additions & 0 deletions spec/kitabu/toc/html_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ def input
expect(content).to have_tag("h2#title", regexp("Title"))
expect(content).to have_tag("h2#title-2", regexp("Title"))
end

it "adds content link" do
expect(content).to have_tag("h1#item-1 > a[href='#item-1'][tabindex='-1']")
end
end

0 comments on commit f91f2a0

Please sign in to comment.