• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

charmbracelet / glow / 9766085360

02 Jul 2024 06:22PM UTC coverage: 10.134% (+3.5%) from 6.624%
9766085360

Pull #619

github

caarlos0
fix: more cleanup
Pull Request #619: feat!: cleanup and updated

63 of 372 new or added lines in 13 files covered. (16.94%)

33 existing lines in 7 files now uncovered.

189 of 1865 relevant lines covered (10.13%)

4.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

30.16
/utils/utils.go
1
package utils
2

3
import (
4
        "os"
5
        "path/filepath"
6
        "regexp"
7
        "strings"
8

9
        "github.com/charmbracelet/glamour"
10
        "github.com/charmbracelet/glamour/ansi"
11
        "github.com/charmbracelet/lipgloss"
12
        "github.com/mitchellh/go-homedir"
13
)
14

15
// RemoveFrontmatter removes the front matter header of a markdown file.
16
func RemoveFrontmatter(content []byte) []byte {
4✔
17
        if frontmatterBoundaries := detectFrontmatter(content); frontmatterBoundaries[0] == 0 {
4✔
18
                return content[frontmatterBoundaries[1]:]
×
19
        }
×
20
        return content
4✔
21
}
22

23
var yamlPattern = regexp.MustCompile(`(?m)^---\r?\n(\s*\r?\n)?`)
24

25
func detectFrontmatter(c []byte) []int {
4✔
26
        if matches := yamlPattern.FindAllIndex(c, 2); len(matches) > 1 {
4✔
27
                return []int{matches[0][0], matches[1][1]}
×
28
        }
×
29
        return []int{-1, -1}
4✔
30
}
31

32
// Expands tilde and all environment variables from the given path.
33
func ExpandPath(path string) string {
×
34
        s, err := homedir.Expand(path)
×
35
        if err == nil {
×
36
                return os.ExpandEnv(s)
×
37
        }
×
38
        return os.ExpandEnv(path)
×
39
}
40

41
// WrapCodeBlock wraps a string in a code block with the given language.
NEW
42
func WrapCodeBlock(s, language string) string {
×
NEW
43
        return "```" + language + "\n" + s + "```"
×
NEW
44
}
×
45

46
var markdownExtensions = []string{
47
        ".md", ".mdown", ".mkdn", ".mkd", ".markdown",
48
}
49

50
// IsMarkdownFile returns whether the filename has a markdown extension.
51
func IsMarkdownFile(filename string) bool {
4✔
52
        ext := filepath.Ext(filename)
4✔
53

4✔
54
        if ext == "" {
4✔
NEW
55
                // By default, assume it's a markdown file.
×
NEW
56
                return true
×
NEW
57
        }
×
58

59
        for _, v := range markdownExtensions {
8✔
60
                if strings.EqualFold(ext, v) {
8✔
61
                        return true
4✔
62
                }
4✔
63
        }
64

65
        // Has an extension but not markdown
66
        // so assume this is a code file.
NEW
67
        return false
×
68
}
69

70
func GlamourStyle(style string, isCode bool) glamour.TermRendererOption {
4✔
71
        if !isCode {
8✔
72
                if style == glamour.AutoStyle {
8✔
73
                        return glamour.WithAutoStyle()
4✔
74
                } else {
4✔
NEW
75
                        return glamour.WithStylePath(style)
×
NEW
76
                }
×
77
        }
78

79
        // If we are rendering a pure code block, we need to modify the style to
80
        // remove the indentation.
81

NEW
82
        var styleConfig ansi.StyleConfig
×
NEW
83

×
NEW
84
        switch style {
×
NEW
85
        case glamour.AutoStyle:
×
NEW
86
                if lipgloss.HasDarkBackground() {
×
NEW
87
                        styleConfig = glamour.DarkStyleConfig
×
NEW
88
                } else {
×
NEW
89
                        styleConfig = glamour.LightStyleConfig
×
NEW
90
                }
×
NEW
91
        case glamour.DarkStyle:
×
NEW
92
                styleConfig = glamour.DarkStyleConfig
×
NEW
93
        case glamour.LightStyle:
×
NEW
94
                styleConfig = glamour.LightStyleConfig
×
NEW
95
        case glamour.PinkStyle:
×
NEW
96
                styleConfig = glamour.PinkStyleConfig
×
NEW
97
        case glamour.NoTTYStyle:
×
NEW
98
                styleConfig = glamour.NoTTYStyleConfig
×
NEW
99
        case glamour.DraculaStyle:
×
NEW
100
                styleConfig = glamour.DraculaStyleConfig
×
NEW
101
        default:
×
NEW
102
                return glamour.WithStylesFromJSONFile(style)
×
103
        }
104

NEW
105
        var margin uint
×
NEW
106
        styleConfig.CodeBlock.Margin = &margin
×
NEW
107

×
NEW
108
        return glamour.WithStyles(styleConfig)
×
109
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc