Skip to content

Commit

Permalink
fix: only use glamour for mk files
Browse files Browse the repository at this point in the history
Use Glamour to render readmes when file is markdown formatted

Fixes: #463
  • Loading branch information
aymanbagabas committed Jan 29, 2024
1 parent b06b555 commit 2564c50
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
15 changes: 15 additions & 0 deletions pkg/ui/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/alecthomas/chroma/lexers"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/charmbracelet/soft-serve/git"
Expand Down Expand Up @@ -106,3 +107,17 @@ func (c *Common) CloneCmd(publicURL, name string) string {
}
return fmt.Sprintf("git clone %s", RepoURL(publicURL, name))
}

// IsFileMarkdown returns true if the file is markdown.
// It uses chroma lexers to analyze and determine the language.
func IsFileMarkdown(content, ext string) bool {
var lang string
lexer := lexers.Match(ext)
if lexer == nil {
lexer = lexers.Analyse(content)
}

Check warning on line 118 in pkg/ui/common/common.go

View check run for this annotation

Codecov / codecov/patch

pkg/ui/common/common.go#L117-L118

Added lines #L117 - L118 were not covered by tests
if lexer != nil && lexer.Config() != nil {
lang = lexer.Config().Name
}
return lang == "markdown"
}
3 changes: 1 addition & 2 deletions pkg/ui/components/code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (r *Code) Init() tea.Cmd {
// 4-spaces.
content = strings.ReplaceAll(content, "\t", strings.Repeat(" ", r.TabWidth))

if r.UseGlamour {
if r.UseGlamour && common.IsFileMarkdown(content, r.extension) {
md, err := r.glamourize(w, content)
if err != nil {
return common.ErrorCmd(err)
Expand Down Expand Up @@ -213,7 +213,6 @@ func (r *Code) glamourize(w int, md string) (string, error) {
glamour.WithStyles(r.styleConfig),
glamour.WithWordWrap(w),
)

if err != nil {
return "", err
}
Expand Down
21 changes: 5 additions & 16 deletions pkg/ui/pages/repo/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path/filepath"
"strings"

"github.com/alecthomas/chroma/lexers"
gitm "github.com/aymanbagabas/git-module"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
Expand Down Expand Up @@ -159,7 +158,8 @@ func (f *Files) FullHelp() [][]key.Binding {
actionKeys = append(actionKeys, lineNo)
}
actionKeys = append(actionKeys, blameView)
if f.isSelectedMarkdown() && !f.blameView {
if common.IsFileMarkdown(f.currentContent.content, f.currentContent.ext) &&
!f.blameView {

Check warning on line 162 in pkg/ui/pages/repo/files.go

View check run for this annotation

Codecov / codecov/patch

pkg/ui/pages/repo/files.go#L161-L162

Added lines #L161 - L162 were not covered by tests
actionKeys = append(actionKeys, preview)
}
switch f.activeView {
Expand Down Expand Up @@ -240,7 +240,7 @@ func (f *Files) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case FileContentMsg:
f.activeView = filesViewContent
f.currentContent = msg
f.code.UseGlamour = f.isSelectedMarkdown()
f.code.UseGlamour = common.IsFileMarkdown(f.currentContent.content, f.currentContent.ext)

Check warning on line 243 in pkg/ui/pages/repo/files.go

View check run for this annotation

Codecov / codecov/patch

pkg/ui/pages/repo/files.go#L243

Added line #L243 was not covered by tests
cmds = append(cmds, f.code.SetContent(msg.content, msg.ext))
f.code.GotoTop()
case FileBlameMsg:
Expand Down Expand Up @@ -293,7 +293,8 @@ func (f *Files) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, f.code.SetSideNote(""))
}
cmds = append(cmds, f.spinner.Tick)
case key.Matches(msg, preview) && f.isSelectedMarkdown() && !f.blameView:
case key.Matches(msg, preview) &&
common.IsFileMarkdown(f.currentContent.content, f.currentContent.ext) && !f.blameView:

Check warning on line 297 in pkg/ui/pages/repo/files.go

View check run for this annotation

Codecov / codecov/patch

pkg/ui/pages/repo/files.go#L297

Added line #L297 was not covered by tests
f.code.UseGlamour = !f.code.UseGlamour
cmds = append(cmds, f.code.SetContent(f.currentContent.content, f.currentContent.ext))
}
Expand Down Expand Up @@ -545,15 +546,3 @@ func (f *Files) setItems(items []selector.IdentifiableItem) tea.Cmd {
return FileItemsMsg(items)
}
}

func (f *Files) isSelectedMarkdown() bool {
var lang string
lexer := lexers.Match(f.currentContent.ext)
if lexer == nil {
lexer = lexers.Analyse(f.currentContent.content)
}
if lexer != nil && lexer.Config() != nil {
lang = lexer.Config().Name
}
return lang == "markdown"
}

0 comments on commit 2564c50

Please sign in to comment.