|
| 1 | +{{ $version := .Site.Params.jstoc.version | default "4.34.0" -}} |
| 2 | +{{ $hash := .Site.Params.jstoc.hash | default "sha512-EMZRx2Hno6csUwgZoOMjyKDp1UdNRrCQH0R97VEPYynqgSUrRItJn73HnRUwqujCat2/Zg731UEw+7xDXAL1ag==" -}} |
| 3 | +{{ $css_hash := .Site.Params.jstoc.css_hash | default "sha512-4q0OX9NAYcRTFEfy9nTK0AV9N7MxM665neDXEW3CjAj1pXc6+8Bcd6ryXl6cY8mTBBXt0aXepnSDLLQZSuJRww==" -}} |
| 4 | +{{ $css_url := printf "https://cdnjs.cloudflare.com/ajax/libs/tocbot/%s/tocbot.min.css" $version -}} |
| 5 | +{{ $cdn_url := printf "https://cdnjs.cloudflare.com/ajax/libs/tocbot/%s/tocbot.min.js" $version -}} |
| 6 | + |
| 7 | +{{ with try (resources.GetRemote $css_url) -}} |
| 8 | + {{ with .Err -}} |
| 9 | + {{ errorf "Could not retrieve Tocbot css file from CDN. Reason: %s." . -}} |
| 10 | + {{ else with.Value -}} |
| 11 | + {{ with resources.Copy (printf "css/tocbot.min.css" ) . }} |
| 12 | + {{ $secureCSS := . | resources.Fingerprint "sha512" -}} |
| 13 | +<link rel="stylesheet" href="{{- .RelPermalink -}}" integrity="{{- $secureCSS.Data.Integrity -}}" crossorigin="anonymous" referrerpolicy="no-referrer"> |
| 14 | + {{ end -}} |
| 15 | + {{ end -}} |
| 16 | +{{ else -}} |
| 17 | + {{ errorf "Invalid Tocbot version %s, could not retrieve this version from CDN." $version -}} |
| 18 | +{{ end -}} |
| 19 | + |
| 20 | +{{ with try (resources.GetRemote $cdn_url) -}} |
| 21 | + {{ with .Err -}} |
| 22 | + {{ errorf "Could not retrieve Tocbot script from CDN. Reason: %s." . -}} |
| 23 | + {{ else with.Value -}} |
| 24 | + {{ with resources.Copy (printf "js/tocbot.min.js" ) . }} |
| 25 | + {{ $secureJS := . | resources.Fingerprint "sha512" -}} |
| 26 | +<script src="{{- .RelPermalink -}}" integrity="{{- $secureJS.Data.Integrity -}}" crossorigin="anonymous" referrerpolicy="no-referrer"></script> |
| 27 | + {{ end -}} |
| 28 | + {{ end -}} |
| 29 | +{{ else -}} |
| 30 | + {{ errorf "Invalid Tocbot version %s, could not retrieve this version from CDN." $version -}} |
| 31 | +{{ end -}} |
| 32 | + |
| 33 | +<!-- Initialize tocbot --> |
| 34 | +<script type="text/javascript"> |
| 35 | + tocbot.init({ |
| 36 | + // Where to render the table of contents. |
| 37 | + tocSelector: '.td-toc', |
| 38 | + // Where to grab the headings to build the table of contents. |
| 39 | + contentSelector: '.td-content', |
| 40 | + // Which headings to grab inside of the contentSelector element. |
| 41 | + headingSelector: '{{ with .Site.Params.jstoc.custom_headings }}{{ . }}{{ else }}h2, h3, h4{{ end }}', |
| 42 | + // ignore headings that are hidden in DOM |
| 43 | + ignoreHiddenElements: true |
| 44 | + }); |
| 45 | +</script> |
0 commit comments