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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ They are of the form:
This is only useful when the TOC itself should be placed at some other
location with the `toc_only` filter.

#### `inject_destination_anchors` filter

```html
<a class="anchor" id="#heading1-1"></a>
```

Inserts destination anchors alongside header tags. Useful if you are not using markdown (which does this automatically). Outputs HTML with destination anchor tags.


## Generated HTML

jekyll-toc generates an unordered list by default. The HTML output is as follows.
Expand Down
6 changes: 6 additions & 0 deletions lib/jekyll-toc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def inject_anchors(html)
TableOfContents::Parser.new(html, toc_config).inject_anchors_into_html
end

def inject_destination_anchors(html)
return html unless toc_enabled?

TableOfContents::Parser.new(html, toc_config).inject_destination_anchors_into_html
end

def toc(html)
return html unless toc_enabled?

Expand Down
11 changes: 11 additions & 0 deletions lib/table_of_contents/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ def inject_anchors_into_html
@doc.inner_html
end

def inject_destination_anchors_into_html
@entries.each do |entry|
# NOTE: `entry[:id]` is automatically URL encoded by Nokogiri
entry[:header_content].add_previous_sibling(
%(<a class="anchor" id="#{entry[:id]}"></a>)
)
end

@doc.inner_html
end

private

# parse logic is from html-pipeline toc_filter
Expand Down