-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move code rendering to the backend & frontend improvements (#176)
Added Chroma & Goldmark Added Mermaidjs More languages supported Add default values for gist links input Added copy code from markdown blocks
- Loading branch information
Showing
24 changed files
with
511 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package render | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"github.com/alecthomas/chroma/v2" | ||
"github.com/alecthomas/chroma/v2/formatters/html" | ||
"github.com/alecthomas/chroma/v2/lexers" | ||
"github.com/alecthomas/chroma/v2/styles" | ||
"github.com/thomiceli/opengist/internal/db" | ||
"github.com/thomiceli/opengist/internal/git" | ||
) | ||
|
||
type RenderedFile struct { | ||
*git.File | ||
Type string | ||
Lines []string | ||
HTML string | ||
} | ||
|
||
type RenderedGist struct { | ||
*db.Gist | ||
Lines []string | ||
HTML string | ||
} | ||
|
||
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) | ||
} | ||
|
||
formatter := html.New(html.WithClasses(true), html.PreventSurroundingPre(true)) | ||
|
||
iterator, err := lexer.Tokenise(nil, file.Content) | ||
if err != nil { | ||
return rendered, err | ||
} | ||
|
||
htmlbuf := bytes.Buffer{} | ||
w := bufio.NewWriter(&htmlbuf) | ||
|
||
tokensLines := chroma.SplitTokensIntoLines(iterator.Tokens()) | ||
lines := make([]string, 0, len(tokensLines)) | ||
for _, tokens := range tokensLines { | ||
iterator = chroma.Literator(tokens...) | ||
err = formatter.Format(&htmlbuf, style, iterator) | ||
if err != nil { | ||
return rendered, fmt.Errorf("unable to format code: %w", err) | ||
} | ||
lines = append(lines, htmlbuf.String()) | ||
htmlbuf.Reset() | ||
} | ||
|
||
_ = w.Flush() | ||
|
||
rendered.Lines = lines | ||
rendered.Type = parseFileTypeName(*lexer.Config()) | ||
|
||
return rendered, err | ||
} | ||
|
||
func HighlightGistPreview(gist *db.Gist) (RenderedGist, error) { | ||
rendered := RenderedGist{ | ||
Gist: gist, | ||
} | ||
|
||
style := newStyle() | ||
lexer := newLexer(gist.PreviewFilename) | ||
if lexer.Config().Name == "markdown" { | ||
return MarkdownGistPreview(gist) | ||
} | ||
|
||
formatter := html.New(html.WithClasses(true), html.PreventSurroundingPre(true)) | ||
|
||
iterator, err := lexer.Tokenise(nil, gist.Preview) | ||
if err != nil { | ||
return rendered, err | ||
} | ||
|
||
htmlbuf := bytes.Buffer{} | ||
w := bufio.NewWriter(&htmlbuf) | ||
|
||
tokensLines := chroma.SplitTokensIntoLines(iterator.Tokens()) | ||
lines := make([]string, 0, len(tokensLines)) | ||
for _, tokens := range tokensLines { | ||
iterator = chroma.Literator(tokens...) | ||
err = formatter.Format(&htmlbuf, style, iterator) | ||
if err != nil { | ||
return rendered, fmt.Errorf("unable to format code: %w", err) | ||
} | ||
lines = append(lines, htmlbuf.String()) | ||
htmlbuf.Reset() | ||
} | ||
|
||
_ = w.Flush() | ||
|
||
rendered.Lines = lines | ||
|
||
return rendered, err | ||
} | ||
|
||
func parseFileTypeName(config chroma.Config) string { | ||
fileType := config.Name | ||
if fileType == "fallback" || fileType == "plaintext" { | ||
return "Text" | ||
} | ||
|
||
return fileType | ||
} | ||
|
||
func newLexer(filename string) chroma.Lexer { | ||
var lexer chroma.Lexer | ||
if lexer = lexers.Get(filename); lexer == nil { | ||
lexer = lexers.Fallback | ||
} | ||
|
||
return lexer | ||
} | ||
|
||
func newStyle() *chroma.Style { | ||
var style *chroma.Style | ||
if style = styles.Get("catppuccin-latte"); style == nil { | ||
style = styles.Fallback | ||
} | ||
|
||
return style | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package render | ||
|
||
import ( | ||
"bytes" | ||
"github.com/alecthomas/chroma/v2/formatters/html" | ||
"github.com/thomiceli/opengist/internal/db" | ||
"github.com/thomiceli/opengist/internal/git" | ||
"github.com/yuin/goldmark" | ||
emoji "github.com/yuin/goldmark-emoji" | ||
highlighting "github.com/yuin/goldmark-highlighting/v2" | ||
"github.com/yuin/goldmark/extension" | ||
"go.abhg.dev/goldmark/mermaid" | ||
) | ||
|
||
func MarkdownGistPreview(gist *db.Gist) (RenderedGist, error) { | ||
var buf bytes.Buffer | ||
err := newMarkdown().Convert([]byte(gist.Preview), &buf) | ||
|
||
return RenderedGist{ | ||
Gist: gist, | ||
HTML: buf.String(), | ||
}, err | ||
} | ||
|
||
func MarkdownFile(file *git.File) (RenderedFile, error) { | ||
var buf bytes.Buffer | ||
err := newMarkdown().Convert([]byte(file.Content), &buf) | ||
|
||
return RenderedFile{ | ||
File: file, | ||
HTML: buf.String(), | ||
Type: "Markdown", | ||
}, err | ||
} | ||
|
||
func newMarkdown() goldmark.Markdown { | ||
return goldmark.New( | ||
goldmark.WithExtensions( | ||
extension.GFM, | ||
highlighting.NewHighlighting( | ||
highlighting.WithStyle("catppuccin-latte"), | ||
highlighting.WithFormatOptions(html.WithClasses(true))), | ||
emoji.Emoji, | ||
&mermaid.Extender{}, | ||
), | ||
) | ||
} |
Oops, something went wrong.