File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -34,7 +34,8 @@ var imgSrcRegex = regexp.MustCompile(`(<img[^>]*\ssrc=)(["']?)([^"'\s>]+)(["']?)
3434
3535// cdnScriptRegex matches Mermaid CDN script tags followed by initialization script
3636// Goldmark's mermaid extension inserts these tags that need to be replaced with embedded version
37- var cdnScriptRegex = regexp .MustCompile (`<script\s+src\s*=\s*"https://cdn\.jsdelivr\.net/npm/mermaid[^"]*"\s*>\s*</script>\s*<script\s*>\s*mermaid\.initialize\s*\(\s*\{\s*startOnLoad\s*:\s*true\s*\}\s*\)\s*;\s*</script>` )
37+ // Pattern allows for flexible whitespace between elements
38+ var cdnScriptRegex = regexp .MustCompile (`<script\s+src\s*=\s*"https://cdn\.jsdelivr\.net/npm/mermaid[^"]*"\s*>\s*</script>\s*<script[^>]*>\s*mermaid\.initialize\s*\([^)]*\)\s*;\s*</script>` )
3839
3940//go:embed github-markdown.css
4041var style string
@@ -396,11 +397,8 @@ func getMimeType(path string) string {
396397
397398// embedMermaidScript replaces CDN-based Mermaid script tags with inline embedded script
398399func embedMermaidScript (htmlContent string ) string {
399- if cdnScriptRegex .MatchString (htmlContent ) {
400- // Replace CDN scripts with inline Mermaid.js
401- inlineScript := fmt .Sprintf ("<script>%s</script><script>mermaid.initialize({startOnLoad: true});</script>" , mermaidJS )
402- htmlContent = cdnScriptRegex .ReplaceAllString (htmlContent , inlineScript )
403- }
404-
405- return htmlContent
400+ // Replace CDN scripts with inline Mermaid.js
401+ // ReplaceAllString performs no replacement if no matches found, so no need to check first
402+ inlineScript := fmt .Sprintf ("<script>%s</script><script>mermaid.initialize({startOnLoad: true});</script>" , mermaidJS )
403+ return cdnScriptRegex .ReplaceAllString (htmlContent , inlineScript )
406404}
You can’t perform that action at this time.
0 commit comments