Skip to content

Commit ecfa7ba

Browse files
authored
Better generation of md urls (#3472)
* remove file renaming from build script * allow ugly URLs for markdown pages only * add markdown as alternative output format for each page * make markdown great again * fix a typo
1 parent 39f07a9 commit ecfa7ba

4 files changed

Lines changed: 134 additions & 14 deletions

File tree

build.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,3 @@ cd "${0%/*}"
77
rm -rf public/
88

99
hugo --minify
10-
11-
# Rename slug/index.md → slug.md to match the clean .md URL convention.
12-
# Only processes directories that also contain an index.html, so section
13-
# indexes and other non-page artifacts are left untouched.
14-
find public -mindepth 2 -name "index.md" | while IFS= read -r f; do
15-
dir=$(dirname "$f")
16-
if [ -f "$dir/index.html" ]; then
17-
mv "$f" "$(dirname "$dir")/$(basename "$dir").md"
18-
fi
19-
done

config.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ imaging:
2424
outputFormats:
2525
MarkdownPage:
2626
mediaType: text/markdown
27-
baseName: index
27+
isHTML: false
2828
isPlainText: true
29-
notAlternative: true
29+
notAlternative: false
30+
ugly: true
3031
LLMSTxt:
3132
mediaType: text/plain
3233
baseName: llms

layouts/_default/baseof.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
{{ .Render "head" }}
4+
{{ .Render "head" }}
5+
{{- with .OutputFormats.Get "MarkdownPage" }}
6+
<link rel="alternate" type="text/markdown" href="{{ .Permalink }}" title="{{ $.Title }} Markdown" />
7+
{{- end }}
58
</head>
69
<body class="page page--home">
710
{{ partial "header" . }}

layouts/_default/single.markdownpage.md

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,130 @@
66
{{ with .Params.description }}
77
> {{ . }}
88
{{ end }}
9-
{{ .RawContent -}}
9+
10+
{{- /* Initialize scratchpad */ -}}
11+
{{- $.Scratch.Set "content" .RawContent -}}
12+
13+
{{/* =========================================================
14+
STAGE 1: EXPAND NESTED INCLUDES (RECURSIVE & SCOPE-SAFE)
15+
========================================================= */}}
16+
{{- range seq 10 -}}
17+
{{- $currentContent := $.Scratch.Get "content" -}}
18+
{{- if in $currentContent "{{< include \"" -}}
19+
{{- $chunks := split $currentContent "{{< include \"" -}}
20+
{{- $.Scratch.Set "finalContent" (index $chunks 0) -}}
21+
22+
{{- range $idx, $chunk := $chunks -}}
23+
{{- if gt $idx 0 -}}
24+
{{- $parts := split $chunk "\" >}}" -}}
25+
{{- $path := index $parts 0 -}}
26+
{{- $restOfText := index $parts 1 -}}
27+
28+
{{- $includePage := $.Site.GetPage $path -}}
29+
{{- $injectedContent := "" -}}
30+
{{- if $includePage -}}
31+
{{- $injectedContent = $includePage.RawContent -}}
32+
{{- else -}}
33+
{{- $cleanPath := replace $path "/partials/" "" -}}
34+
{{- if templates.Exists (printf "partials/%s" $cleanPath) -}}
35+
{{- $injectedContent = partial $cleanPath $ -}}
36+
{{- end -}}
37+
{{- end -}}
38+
39+
{{- $runningContent := $.Scratch.Get "finalContent" -}}
40+
{{- $.Scratch.Set "finalContent" (printf "%s%s%s" $runningContent $injectedContent $restOfText) -}}
41+
{{- end -}}
42+
{{- end -}}
43+
44+
{{- $.Scratch.Set "content" ($.Scratch.Get "finalContent") -}}
45+
{{- end -}}
46+
{{- end -}}
47+
48+
49+
{{/* =========================================================
50+
STAGE 2: FLATTEN TABS & CLEAN WRAPPER TAGS
51+
========================================================= */}}
52+
{{- $processedContent := $.Scratch.Get "content" -}}
53+
54+
{{- if in $processedContent "{{< tab header=\"" -}}
55+
{{- $tabChunks := split $processedContent "{{< tab header=\"" -}}
56+
{{- $.Scratch.Set "flattenedTabs" (index $tabChunks 0) -}}
57+
58+
{{- range $idx, $chunk := $tabChunks -}}
59+
{{- if gt $idx 0 -}}
60+
{{- $parts := split $chunk "\" >}}" -}}
61+
{{- $headerName := index $parts 0 -}}
62+
{{- $tabBody := index $parts 1 -}}
63+
64+
{{- $runningTabs := $.Scratch.Get "flattenedTabs" -}}
65+
{{- $.Scratch.Set "flattenedTabs" (printf "%s\n\n### Option: %s\n\n%s" $runningTabs $headerName $tabBody) -}}
66+
{{- end -}}
67+
{{- end -}}
68+
{{- $processedContent = $.Scratch.Get "flattenedTabs" -}}
69+
{{- end -}}
70+
71+
{{- $processedContent = replace $processedContent "{{< tabpane >}}" "" -}}
72+
{{- $processedContent = replace $processedContent "{{< /tabpane >}}" "" -}}
73+
{{- $processedContent = replace $processedContent "{{<markdown>}}" "" -}}
74+
{{- $processedContent = replace $processedContent "{{</markdown>}}" "" -}}
75+
{{- $processedContent = replace $processedContent "{{< /tab >}}" "" -}}
76+
77+
78+
{{/* =========================================================
79+
STAGE 3: CONVERT ALL HIGHLIGHT VARIANTS TO MARKDOWN CODEBLOCKS
80+
========================================================= */}}
81+
{{- /* Step A: Normalize all opening tags (highlight & highlight-editable) to uniform markdown syntax */ -}}
82+
{{- $processedContent = replaceRE `\{\{\s*<\s*highlight\s+([a-zA-Z0-9_-]+)[^>]*>\s*\}\}` "\n```$1\n" $processedContent -}}
83+
{{- $processedContent = replaceRE `\{\{\s*<\s*highlight-editable\s+([a-zA-Z0-9_-]+)[^>]*>\s*\}\}` "\n```$1\n" $processedContent -}}
84+
85+
{{- /* Step B: Normalize all closing tags to a temporary uniform string to guarantee we catch them all together */ -}}
86+
{{- $processedContent = replaceRE `\{\{\s*<\s*/highlight\s*>\s*\}\}` "{{</closeblock>}}" $processedContent -}}
87+
{{- $processedContent = replaceRE `\{\{\s*<\s*/highlight-editable\s*>\s*\}\}` "{{</closeblock>}}" $processedContent -}}
88+
89+
{{- /* Step C: Safely replace all uniform closing tags with clean markdown backticks */ -}}
90+
{{- $processedContent = replace $processedContent "{{</closeblock>}}" "\n```\n" -}}
91+
92+
93+
{{/* =========================================================
94+
STAGE 4: STRIP VARIABLES FROM EDITABLE CODE BLOCKS
95+
========================================================= */}}
96+
{{- if in $processedContent "$$$" -}}
97+
{{- /* Strip out the $$$variable-name$$$ formatting, leaving only the value */ -}}
98+
{{- /* Matches: $$$id$$$value$$$ -> value */ -}}
99+
{{- $processedContent = replaceRE `\$\$\$[a-zA-Z0-9_-]+\$\$\$(.*?)\$\$\$` "$1" $processedContent -}}
100+
{{- end -}}
101+
102+
103+
{{/* =========================================================
104+
STAGE 5: CONVERT NOTEBOXES TO MARKDOWN BLOCKQUOTES
105+
========================================================= */}}
106+
{{- if in $processedContent "notebox" -}}
107+
{{- /* Standardize tags */ -}}
108+
{{- $processedContent = replaceRE `\{\{\s*<\s*notebox\s*>\s*\}\}` "{{<notebox>}}" $processedContent -}}
109+
{{- $processedContent = replaceRE `\{\{\s*<\s*/notebox\s*>\s*\}\}` "{{</notebox>}}" $processedContent -}}
110+
111+
{{- /* Split by the opening notebox tag to cleanly prepend blockquote markers to every line inside */ -}}
112+
{{- $noteChunks := split $processedContent "{{<notebox>}}" -}}
113+
{{- $.Scratch.Set "cleanNotes" (index $noteChunks 0) -}}
114+
115+
{{- range $idx, $chunk := $noteChunks -}}
116+
{{- if gt $idx 0 -}}
117+
{{- $parts := split $chunk "{{</notebox>}}" -}}
118+
{{- $noteBody := index $parts 0 -}}
119+
{{- $restOfText := index $parts 1 -}}
120+
121+
{{- /* Prepend > to each line within the note block */ -}}
122+
{{- $blockquoteLines := slice -}}
123+
{{- range (split $noteBody "\n") -}}
124+
{{- $blockquoteLines = $blockquoteLines | append (printf "> %s" .) -}}
125+
{{- end -}}
126+
{{- $formattedNote := delimit $blockquoteLines "\n" -}}
127+
128+
{{- $runningNotes := $.Scratch.Get "cleanNotes" -}}
129+
{{- $.Scratch.Set "cleanNotes" (printf "%s\n\n%s\n\n%s" $runningNotes $formattedNote $restOfText) -}}
130+
{{- end -}}
131+
{{- end -}}
132+
{{- $processedContent = $.Scratch.Get "cleanNotes" -}}
133+
{{- end -}}
134+
135+
{{ $processedContent }}

0 commit comments

Comments
 (0)