Skip to content

Commit 6f54292

Browse files
committed
Fix ToC with foreign chars
Note that I suspect you would need a recent Hugo version for this fix to work, as Goldmark (the new renderer) has a slightly different ID generator. Fixes #34
1 parent 54be3ea commit 6f54292

2 files changed

Lines changed: 4 additions & 5 deletions

File tree

layouts/_default/list.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
{{ $headers := partial "funcs/toc_from_pages" .Site.RegularPages }}
1010
{{ range $headers }}
1111
<li>
12-
<a href="#{{ .title | anchorize }}" class="toc-h{{ .level }} toc-link" data-title="{{ .title }}">{{ .title }}</a>
12+
<a href="#{{ .id }}" class="toc-h{{ .level }} toc-link" data-title="{{ .title }}">{{ .title }}</a>
1313
{{if .sub }}
1414
<ul class="toc-list-h2">
1515
{{range .sub}}
16-
<li><a href="#{{ .title | anchorize }}" class="toc-h{{ .level }} toc-link" data-title="{{ .title }}">{{ .title }}</a></li>
16+
<li><a href="#{{ .id }}" class="toc-h{{ .level }} toc-link" data-title="{{ .title }}">{{ .title }}</a></li>
1717
{{end}}
1818
</ul>
1919
{{end}}

layouts/partials/funcs/toc_from_pages.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
{{ range $combined }}
1313
{{ $level := int (substr . 2 1) }}
1414
{{ if le $level 2 }}
15-
{{ $title := . | replaceRE "</?h\\d.*?>" "" | htmlUnescape | safeHTML }}
16-
{{ $item := dict "level" $level "title" $title }}
17-
15+
{{ $idTitle := split (. | replaceRE "<h\\d id=\"(.*?)\">(.*)</h\\d>" "$1|$2") "|" }}
16+
{{ $item := dict "level" $level "id" (index $idTitle 0) "title" (index $idTitle 1) }}
1817
{{ if eq $level 1 }}
1918
{{ if ne $previousLevel 0 }}
2019
{{ $tocItem := merge $previousH1 (dict "sub" $h2s) }}

0 commit comments

Comments
 (0)