Skip to content

Commit

Permalink
Update AddContent function to process code blocks in section descript…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
robert-cronin committed Jul 4, 2024
1 parent 55bce68 commit 2d40fad
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion gen-resourcesdocs/pkg/outputs/kwebsite/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package kwebsite

import (
"fmt"
"html"
"regexp"
"sort"
"strings"

Expand All @@ -19,7 +21,22 @@ type Section struct {
// AddContent adds content to a section
func (o Section) AddContent(s string) error {
i := len(o.chapter.data.Sections)
o.chapter.data.Sections[i-1].Description = s

// Regular expression to find code blocks
re := regexp.MustCompile("```(?s)(.*?)```")

// Replace code blocks with properly formatted HTML
processed := re.ReplaceAllStringFunc(s, func(match string) string {
// Remove the backticks and trim whitespace
code := strings.TrimSpace(strings.TrimPrefix(strings.TrimSuffix(match, "```"), "```"))
// Escape HTML special characters
code = html.EscapeString(code)
// Preserve line breaks
code = strings.ReplaceAll(code, "\n", "<br>")
return "<pre><code>" + code + "</code></pre>"
})

o.chapter.data.Sections[i-1].Description = processed
return nil
}

Expand Down

0 comments on commit 2d40fad

Please sign in to comment.