Skip to content

Commit ac0e229

Browse files
DOC-6122 first working ToC metadata format
1 parent 0cf595f commit ac0e229

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

layouts/_default/section.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"key_specs": {{ .Params.key_specs | jsonify }}{{ end }}{{ if .Params.topics }},
1717
"topics": {{ .Params.topics | jsonify }}{{ end }}{{ if .Params.relatedPages }},
1818
"relatedPages": {{ .Params.relatedPages | jsonify }}{{ end }}{{ if .Params.scope }},
19-
"scope": {{ .Params.scope | jsonify }}{{ end }}
19+
"scope": {{ .Params.scope | jsonify }}{{ end }},
20+
"tableOfContents": {{ partial "toc-json-regex.html" . }}
2021
}
2122
```
2223

layouts/_default/single.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"key_specs": {{ .Params.key_specs | jsonify }}{{ end }}{{ if .Params.topics }},
1717
"topics": {{ .Params.topics | jsonify }}{{ end }}{{ if .Params.relatedPages }},
1818
"relatedPages": {{ .Params.relatedPages | jsonify }}{{ end }}{{ if .Params.scope }},
19-
"scope": {{ .Params.scope | jsonify }}{{ end }}
19+
"scope": {{ .Params.scope | jsonify }}{{ end }},
20+
"tableOfContents": {{ partial "toc-json-regex.html" . }}
2021
}
2122
```
2223

layouts/partials/ai-metadata-body.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@
3737
{{- $metadata = merge $metadata (dict "scope" .Params.scope) -}}
3838
{{- end -}}
3939
{{- $json := $metadata | jsonify -}}
40+
{{- $toc := partial "toc-json-regex.html" . -}}
41+
{{- /* Manually insert tableOfContents into JSON string */ -}}
42+
{{- $json = $json | replaceRE `}$` (printf `,"tableOfContents":%s}` $toc) -}}
4043
{{- printf `<div hidden data-redis-metadata="page">%s</div>` $json | safeHTML -}}
4144

layouts/partials/ai-metadata.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@
3737
{{- $metadata = merge $metadata (dict "scope" .Params.scope) -}}
3838
{{- end -}}
3939
{{- $json := $metadata | jsonify -}}
40+
{{- $toc := partial "toc-json-regex.html" . -}}
41+
{{- /* Manually insert tableOfContents into JSON string */ -}}
42+
{{- $json = $json | replaceRE `}$` (printf `,"tableOfContents":%s}` $toc) -}}
4043
{{- printf `<script type="application/json" data-ai-metadata>%s</script>` $json | safeHTML -}}
4144

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 }}
36+

0 commit comments

Comments
 (0)