forked from tangled.org/core
Monorepo for Tangled — https://tangled.org

appview/pages: add renderers for readme markdown and code blocks

Signed-off-by: oppiliappan <me@oppi.li>

authored by oppi.li and committed by Tangled 1473ec3e 4268abba

Changed files
+41
appview
pages
+41
appview/pages/funcmap.go
··· 1 1 package pages 2 2 3 3 import ( 4 + "bytes" 4 5 "context" 5 6 "crypto/hmac" 6 7 "crypto/sha256" ··· 17 18 "strings" 18 19 "time" 19 20 21 + "github.com/alecthomas/chroma/v2" 22 + chromahtml "github.com/alecthomas/chroma/v2/formatters/html" 23 + "github.com/alecthomas/chroma/v2/lexers" 24 + "github.com/alecthomas/chroma/v2/styles" 20 25 "github.com/bluesky-social/indigo/atproto/syntax" 21 26 "github.com/dustin/go-humanize" 22 27 "github.com/go-enry/go-enry/v2" ··· 245 250 htmlString := p.rctx.RenderMarkdown(text) 246 251 sanitized := p.rctx.SanitizeDescription(htmlString) 247 252 return template.HTML(sanitized) 253 + }, 254 + "readme": func(text string) template.HTML { 255 + p.rctx.RendererType = markup.RendererTypeRepoMarkdown 256 + htmlString := p.rctx.RenderMarkdown(text) 257 + sanitized := p.rctx.SanitizeDefault(htmlString) 258 + return template.HTML(sanitized) 259 + }, 260 + "code": func(content, path string) string { 261 + var style *chroma.Style = styles.Get("catpuccin-latte") 262 + formatter := chromahtml.New( 263 + chromahtml.InlineCode(false), 264 + chromahtml.WithLineNumbers(true), 265 + chromahtml.WithLinkableLineNumbers(true, "L"), 266 + chromahtml.Standalone(false), 267 + chromahtml.WithClasses(true), 268 + ) 269 + 270 + lexer := lexers.Get(filepath.Base(path)) 271 + if lexer == nil { 272 + lexer = lexers.Fallback 273 + } 274 + 275 + iterator, err := lexer.Tokenise(nil, content) 276 + if err != nil { 277 + p.logger.Error("chroma tokenize", "err", "err") 278 + return "" 279 + } 280 + 281 + var code bytes.Buffer 282 + err = formatter.Format(&code, style, iterator) 283 + if err != nil { 284 + p.logger.Error("chroma format", "err", "err") 285 + return "" 286 + } 287 + 288 + return code.String() 248 289 }, 249 290 "trimUriScheme": func(text string) string { 250 291 text = strings.TrimPrefix(text, "https://")