@@ -46,20 +46,28 @@ type CodeSnippetBlock struct {
4646 Snippets []CodeSnippet
4747}
4848
49- func markdownRenders (ctx context.Context , staticMdPath string ) (mdElementRenderers map [string ]string , mdAnchors map [string ][]string , err error ) {
49+ type MarkdownData struct {
50+ Anchors []string
51+ Title string
52+ Description string
53+ Contents string
54+ }
55+ type MarkdownDataset map [string ]* MarkdownData
56+
57+ func markdownRenders (ctx context.Context , staticMdPath string ) (MarkdownDataset , error ) {
5058 if mdRenderer == nil {
5159 htmlFormatter := html .New (html .WithClasses (true ), html .TabWidth (2 ))
5260 if htmlFormatter == nil {
53- return nil , nil , fmt .Errorf ("couldn't create html formatter" )
61+ return nil , fmt .Errorf ("couldn't create html formatter" )
5462 }
5563 styleName := "nord"
5664 highlightStyle := styles .Get (styleName )
5765 if highlightStyle == nil {
58- return nil , nil , fmt .Errorf ("couldn't find style %s" , styleName )
66+ return nil , fmt .Errorf ("couldn't find style %s" , styleName )
5967 }
6068 highlightCSSBuffer := & bytes.Buffer {}
6169 if err := htmlFormatter .WriteCSS (highlightCSSBuffer , highlightStyle ); err != nil {
62- return nil , nil , fmt .Errorf ("error writing highlight css: %w" , err )
70+ return nil , fmt .Errorf ("error writing highlight css: %w" , err )
6371 }
6472 highlightCSS = templ .ComponentFunc (func (ctx context.Context , w io.Writer ) error {
6573 _ , err := io .WriteString (w , fmt .Sprintf (`<style>%s</style>` , highlightCSSBuffer .String ()))
@@ -124,7 +132,7 @@ func markdownRenders(ctx context.Context, staticMdPath string) (mdElementRendere
124132 mdDir := "static/md/" + staticMdPath
125133 docs , err := staticFS .ReadDir (mdDir )
126134 if err != nil {
127- return nil , nil , fmt .Errorf ("error reading docs dir: %w" , err )
135+ return nil , fmt .Errorf ("error reading docs dir: %w" , err )
128136 }
129137
130138 // regExpImg := regexp.MustCompile(`(?P<whole>!\[[^\]]+]\((?P<path>[^)]+)\))`)
@@ -133,14 +141,16 @@ func markdownRenders(ctx context.Context, staticMdPath string) (mdElementRendere
133141 codeSnippets := regexp .MustCompile (`!!!CODE_SNIPPET:(?<basePath>[^!]*)!!!` )
134142 // Icon or mascot from https://icones.js.org/collection/vscode-icons
135143
136- mdElementRenderers = map [string ]string {}
137- mdAnchors = map [string ][]string {}
144+ res := MarkdownDataset {}
145+
146+ titleTrimmer := regexp .MustCompile (`^#+\s*` )
147+
138148 for _ , de := range docs {
139149 fullPath := mdDir + "/" + de .Name ()
140150
141151 b , err := staticFS .ReadFile (fullPath )
142152 if err != nil {
143- return nil , nil , fmt .Errorf ("error reading doc %s: %w" , de .Name (), err )
153+ return nil , fmt .Errorf ("error reading doc %s: %w" , de .Name (), err )
144154 }
145155
146156 // Package version
@@ -155,10 +165,10 @@ func markdownRenders(ctx context.Context, staticMdPath string) (mdElementRendere
155165 baseDir := filepath .Dir (fullWithTestExtension )
156166 fileEntries , err := staticFS .ReadDir (baseDir )
157167 if err != nil {
158- return nil , nil , fmt .Errorf ("error reading code snippet dir %s: %w" , baseDir , err )
168+ return nil , fmt .Errorf ("error reading code snippet dir %s: %w" , baseDir , err )
159169 }
160170 if len (fileEntries ) == 0 {
161- return nil , nil , fmt .Errorf ("no files found in code snippet dir %s" , baseDir )
171+ return nil , fmt .Errorf ("no files found in code snippet dir %s" , baseDir )
162172 }
163173
164174 snippetBlock := CodeSnippetBlock {
@@ -178,15 +188,15 @@ func markdownRenders(ctx context.Context, staticMdPath string) (mdElementRendere
178188
179189 codeSnippetRaw , err := staticFS .ReadFile (fileFullPath )
180190 if err != nil {
181- return nil , nil , fmt .Errorf ("error reading code snippet %s: %w" , fileFullPath , err )
191+ return nil , fmt .Errorf ("error reading code snippet %s: %w" , fileFullPath , err )
182192 }
183193 codeSnippet := string (codeSnippetRaw )
184194
185195 buf := bytebufferpool .Get ()
186196 defer bytebufferpool .Put (buf )
187197
188198 if err := htmlHighlight (buf , codeSnippet , ext , "" ); err != nil {
189- return nil , nil , fmt .Errorf ("error highlighting code snippet %s: %w" , fileFullPath , err )
199+ return nil , fmt .Errorf ("error highlighting code snippet %s: %w" , fileFullPath , err )
190200 }
191201
192202 icon := ""
@@ -220,11 +230,16 @@ func markdownRenders(ctx context.Context, staticMdPath string) (mdElementRendere
220230 b = bytes .ReplaceAll (b , fullMatch , buf .Bytes ())
221231 }
222232
233+ title := ""
234+
223235 // Get all anchors
224236 anchors := []string {}
225237 lines := strings .Split (string (b ), "\n " )
226238 for _ , line := range lines {
227239 if strings .HasPrefix (line , "#" ) {
240+ if title == "" {
241+ title = titleTrimmer .ReplaceAllString (line , "" )
242+ }
228243 parts := strings .Split (line , " " )
229244 anchor := strings .Join (parts [1 :], " " )
230245 anchors = append (anchors , anchor )
@@ -236,11 +251,16 @@ func markdownRenders(ctx context.Context, staticMdPath string) (mdElementRendere
236251 renderedHTML := string (markdown .Render (doc , mdRenderer ()))
237252
238253 name := de .Name ()[0 : len (de .Name ())- 3 ]
239- mdElementRenderers [name ] = renderedHTML
240- mdAnchors [name ] = anchors
254+
255+ res [name ] = & MarkdownData {
256+ Anchors : anchors ,
257+ Title : title ,
258+ Description : "" ,
259+ Contents : renderedHTML ,
260+ }
241261 }
242262
243- return mdElementRenderers , mdAnchors , nil
263+ return res , nil
244264}
245265
246266func KVPairsAttrs (kvPairs ... string ) templ.Attributes {
0 commit comments