Skip to content

Commit

Permalink
Render SVG file
Browse files Browse the repository at this point in the history
  • Loading branch information
thomiceli committed Oct 14, 2024
1 parent e268fef commit 34ecb1b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
28 changes: 24 additions & 4 deletions internal/render/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package render
import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/formatters/html"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/thomiceli/opengist/internal/db"
"github.com/thomiceli/opengist/internal/git"
"path"
"sync"
)

Expand All @@ -28,18 +30,22 @@ type RenderedGist struct {
}

func HighlightFile(file *git.File) (RenderedFile, error) {
rendered := RenderedFile{
File: file,
}

style := newStyle()
lexer := newLexer(file.Filename)

if lexer.Config().Name == "markdown" {
return MarkdownFile(file)
}
if lexer.Config().Name == "XML" && path.Ext(file.Filename) == ".svg" {
return RenderSvgFile(file), nil
}

formatter := html.New(html.WithClasses(true), html.PreventSurroundingPre(true))

rendered := RenderedFile{
File: file,
}

iterator, err := lexer.Tokenise(nil, file.Content+"\n")
if err != nil {
return rendered, err
Expand Down Expand Up @@ -140,6 +146,20 @@ func HighlightGistPreview(gist *db.Gist) (RenderedGist, error) {
return rendered, err
}

func RenderSvgFile(file *git.File) RenderedFile {
rendered := RenderedFile{
File: file,
}

encoded := base64.StdEncoding.EncodeToString([]byte(file.Content))
content := `<img src="data:image/svg+xml;base64,` + encoded + `" />`

rendered.HTML = content
rendered.Type = "SVG"

return rendered
}

func parseFileTypeName(config chroma.Config) string {
fileType := config.Name
if fileType == "fallback" || fileType == "plaintext" {
Expand Down
3 changes: 3 additions & 0 deletions internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ var (
"isCsv": func(i string) bool {
return strings.ToLower(filepath.Ext(i)) == ".csv"
},
"isSvg": func(i string) bool {
return strings.ToLower(filepath.Ext(i)) == ".svg"
},
"csvFile": func(file *git.File) *git.CsvFile {
if strings.ToLower(filepath.Ext(file.Filename)) != ".csv" {
return nil
Expand Down
4 changes: 4 additions & 0 deletions public/embed.scss
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ dl.dl-config dd {
@apply overflow-auto whitespace-pre;
}

.markdown-body img {
@apply bg-transparent dark:bg-transparent;
}

.chroma.preview.markdown pre code {
@apply p-4;
}
4 changes: 4 additions & 0 deletions public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ dl.dl-config dd {
@apply overflow-auto whitespace-pre !important;
}

.markdown-body img {
@apply bg-transparent dark:bg-transparent !important;
}

.chroma.preview.markdown pre code {
@apply p-4 !important;
}
Expand Down
2 changes: 2 additions & 0 deletions templates/pages/gist.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
</table>
{{ else if isMarkdown $file.Filename }}
<div class="chroma markdown markdown-body p-8">{{ $file.HTML | safe }}</div>
{{ else if isSvg $file.Filename }}
<div class="p-8 flex justify-center">{{ $file.HTML | safe }}</div>
{{ else }}
<div class="code">
{{ $fileslug := slug $file.Filename }}
Expand Down
2 changes: 2 additions & 0 deletions templates/pages/gist_embed.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
</table>
{{ else if isMarkdown $file.Filename }}
<div class="chroma markdown markdown-body p-8">{{ $file.HTML | safe }}</div>
{{ else if isSvg $file.Filename }}
<div class="p-8 flex justify-center">{{ $file.HTML | safe }}</div>
{{ else }}
<div class="code dark:bg-gray-900">
{{ $fileslug := slug $file.Filename }}
Expand Down

0 comments on commit 34ecb1b

Please sign in to comment.