mirror of
https://github.com/charmbracelet/glow.git
synced 2026-08-09 09:49:09 +02:00
feat!: cleanup and updated (#619)
* feat!: cleanup Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * more cleanup Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: more cleanup Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: more cleanup --------- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
parent
d2e7742a6a
commit
fce3edf7db
27 changed files with 692 additions and 2962 deletions
|
|
@ -2,8 +2,13 @@ package utils
|
|||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/glamour"
|
||||
"github.com/charmbracelet/glamour/ansi"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
)
|
||||
|
||||
|
|
@ -32,3 +37,73 @@ func ExpandPath(path string) string {
|
|||
}
|
||||
return os.ExpandEnv(path)
|
||||
}
|
||||
|
||||
// WrapCodeBlock wraps a string in a code block with the given language.
|
||||
func WrapCodeBlock(s, language string) string {
|
||||
return "```" + language + "\n" + s + "```"
|
||||
}
|
||||
|
||||
var markdownExtensions = []string{
|
||||
".md", ".mdown", ".mkdn", ".mkd", ".markdown",
|
||||
}
|
||||
|
||||
// IsMarkdownFile returns whether the filename has a markdown extension.
|
||||
func IsMarkdownFile(filename string) bool {
|
||||
ext := filepath.Ext(filename)
|
||||
|
||||
if ext == "" {
|
||||
// By default, assume it's a markdown file.
|
||||
return true
|
||||
}
|
||||
|
||||
for _, v := range markdownExtensions {
|
||||
if strings.EqualFold(ext, v) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// Has an extension but not markdown
|
||||
// so assume this is a code file.
|
||||
return false
|
||||
}
|
||||
|
||||
func GlamourStyle(style string, isCode bool) glamour.TermRendererOption {
|
||||
if !isCode {
|
||||
if style == glamour.AutoStyle {
|
||||
return glamour.WithAutoStyle()
|
||||
} else {
|
||||
return glamour.WithStylePath(style)
|
||||
}
|
||||
}
|
||||
|
||||
// If we are rendering a pure code block, we need to modify the style to
|
||||
// remove the indentation.
|
||||
|
||||
var styleConfig ansi.StyleConfig
|
||||
|
||||
switch style {
|
||||
case glamour.AutoStyle:
|
||||
if lipgloss.HasDarkBackground() {
|
||||
styleConfig = glamour.DarkStyleConfig
|
||||
} else {
|
||||
styleConfig = glamour.LightStyleConfig
|
||||
}
|
||||
case glamour.DarkStyle:
|
||||
styleConfig = glamour.DarkStyleConfig
|
||||
case glamour.LightStyle:
|
||||
styleConfig = glamour.LightStyleConfig
|
||||
case glamour.PinkStyle:
|
||||
styleConfig = glamour.PinkStyleConfig
|
||||
case glamour.NoTTYStyle:
|
||||
styleConfig = glamour.NoTTYStyleConfig
|
||||
case glamour.DraculaStyle:
|
||||
styleConfig = glamour.DraculaStyleConfig
|
||||
default:
|
||||
return glamour.WithStylesFromJSONFile(style)
|
||||
}
|
||||
|
||||
var margin uint
|
||||
styleConfig.CodeBlock.Margin = &margin
|
||||
|
||||
return glamour.WithStyles(styleConfig)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue