|
1 | | -{{/* Convert Hugo's HTML table of contents to JSON using regex substitutions */}} |
2 | | -{{/* Input: page object */}} |
3 | | -{{/* Output: JSON structure representing the table of contents */}} |
4 | | - |
5 | | -{{ $toc := .TableOfContents }} |
6 | | - |
7 | | -{{/* Remove the nav wrapper and all newlines/extra whitespace */}} |
8 | | -{{ $toc = $toc | replaceRE "<nav[^>]*>" "" }} |
9 | | -{{ $toc = $toc | replaceRE "</nav>" "" }} |
10 | | -{{ $toc = $toc | replaceRE "\\n\\s*" "" }} |
11 | | - |
12 | | -{{/* Step 1: Replace <ul> with opening bracket for children array */}} |
13 | | -{{ $toc = $toc | replaceRE "<ul>" "[" }} |
14 | | -{{ $toc = $toc | replaceRE "</ul>" "]" }} |
15 | | - |
16 | | -{{/* Step 2: Replace <li><a href="#ID">TITLE</a> with {"id":"ID","title":"TITLE" */}} |
17 | | -{{ $toc = $toc | replaceRE "<li><a href=\"#([^\"]+)\">([^<]+)</a>" "{\"id\":\"$1\",\"title\":\"$2\"" }} |
18 | | - |
19 | | -{{/* Step 3: Replace </li> with } */}} |
20 | | -{{ $toc = $toc | replaceRE "</li>" "}" }} |
21 | | - |
22 | | -{{/* Step 4: Handle nested structure - replace ][ with ],[ (sibling arrays) */}} |
23 | | -{{ $toc = $toc | replaceRE "\\]\\[" "],[" }} |
24 | | - |
25 | | -{{/* Step 4b: Handle nested structure - replace [ with ,"children":[ (child arrays) */}} |
26 | | -{{ $toc = $toc | replaceRE "\"\\[" "\",\"children\":[" }} |
27 | | - |
28 | | -{{/* Step 5: Add commas between sibling objects - replace }{ with },{ */}} |
29 | | -{{ $toc = $toc | replaceRE "\\}\\{" "},{" }} |
30 | | - |
31 | | -{{/* Step 6: Wrap the entire structure */}} |
32 | | -{{ $toc = print "{\"sections\":" $toc "}" }} |
33 | | - |
34 | | -{{/* Output the JSON - use safeHTML to prevent quote escaping */}} |
35 | | -{{ $toc | safeHTML }} |
| 1 | +{{- /* Convert Hugo's HTML table of contents to JSON using regex substitutions */ -}} |
| 2 | +{{- /* Input: page object */ -}} |
| 3 | +{{- /* Output: JSON structure representing the table of contents */ -}} |
| 4 | +{{- $toc := .TableOfContents -}} |
| 5 | +{{- /* Remove the nav wrapper and all newlines/extra whitespace */ -}} |
| 6 | +{{- $toc = $toc | replaceRE "<nav[^>]*>" "" -}} |
| 7 | +{{- $toc = $toc | replaceRE "</nav>" "" -}} |
| 8 | +{{- $toc = $toc | replaceRE "\\n\\s*" "" -}} |
| 9 | +{{- /* Step 1: Replace <ul> with opening bracket for children array */ -}} |
| 10 | +{{- $toc = $toc | replaceRE "<ul>" "[" -}} |
| 11 | +{{- $toc = $toc | replaceRE "</ul>" "]" -}} |
| 12 | +{{- /* Step 2: Replace <li><a href="#ID">TITLE</a> with {"id":"ID","title":"TITLE" */ -}} |
| 13 | +{{- $toc = $toc | replaceRE "<li><a href=\"#([^\"]+)\">([^<]+)</a>" "{\"id\":\"$1\",\"title\":\"$2\"" -}} |
| 14 | +{{- /* Step 3: Replace </li> with } */ -}} |
| 15 | +{{- $toc = $toc | replaceRE "</li>" "}" -}} |
| 16 | +{{- /* Step 4: Handle nested structure - replace ][ with ],[ (sibling arrays) */ -}} |
| 17 | +{{- $toc = $toc | replaceRE "\\]\\[" "],[" -}} |
| 18 | +{{- /* Step 4b: Handle nested structure - replace [ with ,"children":[ (child arrays) */ -}} |
| 19 | +{{- $toc = $toc | replaceRE "\"\\[" "\",\"children\":[" -}} |
| 20 | +{{- /* Step 5: Add commas between sibling objects - replace }{ with },{ */ -}} |
| 21 | +{{- $toc = $toc | replaceRE "\\}\\{" "},{" -}} |
| 22 | +{{- /* Step 6: Wrap the entire structure */ -}} |
| 23 | +{{- $toc = print "{\"sections\":" $toc "}" -}} |
| 24 | +{{- /* Step 7: Remove all newlines and extra whitespace for clean output */ -}} |
| 25 | +{{- $toc = $toc | replaceRE "\\n\\s*" "" -}} |
| 26 | +{{- /* Output the JSON - use safeHTML to prevent quote escaping */ -}} |
| 27 | +{{- $toc | safeHTML -}} |
36 | 28 |
|
0 commit comments