Compare commits

...

1 commit

Author SHA1 Message Date
drew
5e424e9079
feat: github admonitions
Signed-off-by: drew <me@andrinoff.com>
2026-10-02 17:17:41 +04:00
9 changed files with 194 additions and 10 deletions

View file

@ -188,6 +188,20 @@ glow --help
Check out the [Glamour Style Section](https://github.com/charmbracelet/glamour/blob/master/styles/gallery/README.md) Check out the [Glamour Style Section](https://github.com/charmbracelet/glamour/blob/master/styles/gallery/README.md)
to find more styles. Or [make your own](https://github.com/charmbracelet/glamour/tree/master/styles)! to find more styles. Or [make your own](https://github.com/charmbracelet/glamour/tree/master/styles)!
### GitHub Alerts
GitHub [alerts](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts)
render as callouts, so the READMEs that use them read the way they do on GitHub:
```markdown
> [!NOTE]
> Useful information that users should know, even when skimming content.
```
Alert icons use the same octicons GitHub does when your terminal renders Nerd
Font glyphs, and fall back to Unicode icons otherwise. Set `NERDFONT=1` or
`NERDFONT=0` to override the detection.
## The Config File ## The Config File
If you find yourself supplying the same flags to `glow` all the time, it's If you find yourself supplying the same flags to `glow` all the time, it's

7
go.mod
View file

@ -5,8 +5,9 @@ go 1.26.7
require ( require (
charm.land/bubbles/v2 v2.1.1 charm.land/bubbles/v2 v2.1.1
charm.land/bubbletea/v2 v2.0.8 charm.land/bubbletea/v2 v2.0.8
charm.land/glamour/v2 v2.0.1 charm.land/glamour/v2 v2.0.2-0.20261002130030-25e45f0d196f
charm.land/lipgloss/v2 v2.0.6 charm.land/lipgloss/v2 v2.0.6
charm.land/x/nerdfont v0.1.0
github.com/atotto/clipboard v0.1.4 github.com/atotto/clipboard v0.1.4
github.com/caarlos0/env/v11 v11.3.1 github.com/caarlos0/env/v11 v11.3.1
github.com/charmbracelet/log v0.4.2 github.com/charmbracelet/log v0.4.2
@ -28,7 +29,7 @@ require (
github.com/spf13/viper v1.21.0 github.com/spf13/viper v1.21.0
golang.org/x/sys v0.47.0 golang.org/x/sys v0.47.0
golang.org/x/term v0.44.0 golang.org/x/term v0.44.0
golang.org/x/text v0.39.0 golang.org/x/text v0.41.0
mvdan.cc/sh/v3 v3.13.1 mvdan.cc/sh/v3 v3.13.1
) )
@ -66,7 +67,7 @@ require (
github.com/spf13/pflag v1.0.10 // indirect github.com/spf13/pflag v1.0.10 // indirect
github.com/subosito/gotenv v1.6.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/yuin/goldmark v1.7.17 // indirect github.com/yuin/goldmark v1.8.5 // indirect
github.com/yuin/goldmark-emoji v1.0.5 // indirect github.com/yuin/goldmark-emoji v1.0.5 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 // indirect golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 // indirect

14
go.sum
View file

@ -2,10 +2,12 @@ charm.land/bubbles/v2 v2.1.1 h1:7r55WzBxpo/R3z98hGmY7KKPd3ET6vsf0Fb9sDHOV60=
charm.land/bubbles/v2 v2.1.1/go.mod h1:GE6M31gaWZVXzGw73OeuTTgy4lX+OtkH0E5ymnNsHxo= charm.land/bubbles/v2 v2.1.1/go.mod h1:GE6M31gaWZVXzGw73OeuTTgy4lX+OtkH0E5ymnNsHxo=
charm.land/bubbletea/v2 v2.0.8 h1:SxTJMhCAI3lbPmy4SgX5LWZ24AdINr4I6UEqzZvYJuY= charm.land/bubbletea/v2 v2.0.8 h1:SxTJMhCAI3lbPmy4SgX5LWZ24AdINr4I6UEqzZvYJuY=
charm.land/bubbletea/v2 v2.0.8/go.mod h1:2SkdgoTXluXJHOUwAoRlRXF/28vklb1rFl6GcgV1/ss= charm.land/bubbletea/v2 v2.0.8/go.mod h1:2SkdgoTXluXJHOUwAoRlRXF/28vklb1rFl6GcgV1/ss=
charm.land/glamour/v2 v2.0.1 h1:xl+r00A4aJWU0z8fgwKd9fQQ4rsphqGUzuEiXZP5n+c= charm.land/glamour/v2 v2.0.2-0.20261002130030-25e45f0d196f h1:Gs1ba0H6zN9QKildWmhqVEMdxduhmdDCRJWwIAN5fBI=
charm.land/glamour/v2 v2.0.1/go.mod h1:jo9z8XqVKPeEFMVdvCRLGk++RyJ3CdUwgNr7EvXLw3k= charm.land/glamour/v2 v2.0.2-0.20261002130030-25e45f0d196f/go.mod h1:wqGcmrnBmbunm0/LszemUKpYwWxvPPBkEno7KusMy6s=
charm.land/lipgloss/v2 v2.0.6 h1:EaGKeuA8FvF+v2BT5VmZd2LoYLaMZJXA5n34th8nCIQ= charm.land/lipgloss/v2 v2.0.6 h1:EaGKeuA8FvF+v2BT5VmZd2LoYLaMZJXA5n34th8nCIQ=
charm.land/lipgloss/v2 v2.0.6/go.mod h1:ipDDJNSGa1hlwDtSfW1s2/xR8Vdhbut4PXh2zEKZd0Q= charm.land/lipgloss/v2 v2.0.6/go.mod h1:ipDDJNSGa1hlwDtSfW1s2/xR8Vdhbut4PXh2zEKZd0Q=
charm.land/x/nerdfont v0.1.0 h1:R3Dj1o+7ZynFhrCwYcAjRng1XebyfDMzzJTb0kjfi4Y=
charm.land/x/nerdfont v0.1.0/go.mod h1:8os2oa2fBZFm1Xhk9Iu1YyodLXV2M73csjSRoA9287Y=
github.com/alecthomas/assert/v2 v2.7.0 h1:QtqSACNS3tF7oasA8CU6A6sXZSBDqnm7RfpLl9bZqbE= github.com/alecthomas/assert/v2 v2.7.0 h1:QtqSACNS3tF7oasA8CU6A6sXZSBDqnm7RfpLl9bZqbE=
github.com/alecthomas/assert/v2 v2.7.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= github.com/alecthomas/assert/v2 v2.7.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/chroma/v2 v2.14.0 h1:R3+wzpnUArGcQz7fCETQBzO5n9IMNi13iIs46aU4V9E= github.com/alecthomas/chroma/v2 v2.14.0 h1:R3+wzpnUArGcQz7fCETQBzO5n9IMNi13iIs46aU4V9E=
@ -153,8 +155,8 @@ github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSW
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
github.com/yuin/goldmark v1.7.17 h1:p36OVWwRb246iHxA/U4p8OPEpOTESm4n+g+8t0EE5uA= github.com/yuin/goldmark v1.8.5 h1:r6N5afV5qj/5S4UTch8agZHJ8UxNCMwX7WjkkJam2NA=
github.com/yuin/goldmark v1.7.17/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg= github.com/yuin/goldmark v1.8.5/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
github.com/yuin/goldmark-emoji v1.0.5 h1:EMVWyCGPlXJfUXBXpuMu+ii3TIaxbVBnEX9uaDC4cIk= github.com/yuin/goldmark-emoji v1.0.5 h1:EMVWyCGPlXJfUXBXpuMu+ii3TIaxbVBnEX9uaDC4cIk=
github.com/yuin/goldmark-emoji v1.0.5/go.mod h1:tTkZEbwu5wkPmgTcitqddVxY9osFZiavD+r4AzQrh1U= github.com/yuin/goldmark-emoji v1.0.5/go.mod h1:tTkZEbwu5wkPmgTcitqddVxY9osFZiavD+r4AzQrh1U=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
@ -170,8 +172,8 @@ golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/term v0.44.0 h1:0rLvDRCtNj0gZkyIXhCyOb2OAzEhLVqc4B+hrsBhrmc= golang.org/x/term v0.44.0 h1:0rLvDRCtNj0gZkyIXhCyOb2OAzEhLVqc4B+hrsBhrmc=
golang.org/x/term v0.44.0/go.mod h1:7ze4MdzUzLXpSAoFP1H0bOI9aXDqveSvatT5vKcFh2Y= golang.org/x/term v0.44.0/go.mod h1:7ze4MdzUzLXpSAoFP1H0bOI9aXDqveSvatT5vKcFh2Y=
golang.org/x/text v0.39.0 h1:UbZz4pLOvn600D6Oh6GGEI6VAmndrEBLv8/6BEXzyus= golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8=
golang.org/x/text v0.39.0/go.mod h1:3UwRclnC2g0TU9x8PZiyfOajCd1zaUNHF9cvqcQZ+ZM= golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

View file

@ -295,6 +295,7 @@ func executeCLI(cmd *cobra.Command, src *source, w io.Writer) error {
glamour.WithWordWrap(int(width)), //nolint:gosec glamour.WithWordWrap(int(width)), //nolint:gosec
glamour.WithBaseURL(baseURL), glamour.WithBaseURL(baseURL),
glamour.WithPreservedNewLines(), glamour.WithPreservedNewLines(),
glamour.WithAlerts(),
) )
if err != nil { if err != nil {
return fmt.Errorf("unable to create renderer: %w", err) return fmt.Errorf("unable to create renderer: %w", err)

14
test.md Normal file
View file

@ -0,0 +1,14 @@
> [!NOTE]
> this is a note
> [!CAUTION]
> be careful
> [!WARNING]
> Hello
> [!IMPORTANT]
> Hello
> [!TIP]
> Hello

View file

@ -367,6 +367,7 @@ func glamourRender(m pagerModel, markdown string) (string, error) {
options := []glamour.TermRendererOption{ options := []glamour.TermRendererOption{
utils.GlamourStyle(m.common.cfg.GlamourStyle, isCode), utils.GlamourStyle(m.common.cfg.GlamourStyle, isCode),
glamour.WithWordWrap(width), glamour.WithWordWrap(width),
glamour.WithAlerts(),
} }
if m.common.cfg.PreserveNewLines { if m.common.cfg.PreserveNewLines {

View file

@ -41,3 +41,58 @@ func TestGlamourRenderTextSizing(t *testing.T) {
t.Errorf("expected no leftover markers in pager output, got %q", out) t.Errorf("expected no leftover markers in pager output, got %q", out)
} }
} }
func TestGlamourRenderAlerts(t *testing.T) {
common := &commonModel{
cfg: Config{
GlamourEnabled: true,
GlamourStyle: "dark",
GlamourMaxWidth: 80,
},
styles: newStyles(true),
width: 80,
height: 24,
}
m := newPagerModel(common)
m.currentDocument = markdown{Note: "test.md"}
m.setSize(80, 24)
md := "> [!NOTE]\n> Useful information.\n\n> [!WARNING]\n> Careful.\n"
out, err := glamourRender(m, md)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out, "Note") || !strings.Contains(out, "Warning") {
t.Errorf("expected alert titles in pager output, got %q", out)
}
if strings.Contains(out, "[!NOTE]") || strings.Contains(out, "[!WARNING]") {
t.Errorf("expected alert markers to be consumed, got %q", out)
}
}
func TestGlamourRenderAlertsNoWrap(t *testing.T) {
common := &commonModel{
cfg: Config{
GlamourEnabled: true,
GlamourStyle: "dark",
},
styles: newStyles(true),
width: 0,
height: 24,
}
m := newPagerModel(common)
m.currentDocument = markdown{Note: "test.md"}
m.setSize(0, 24)
out, err := glamourRender(m, "> [!NOTE]\n> Useful information.\n")
if err != nil {
t.Fatal(err)
}
// Words are styled separately at zero width, so don't look for a whole
// sentence here.
if !strings.Contains(out, "Note") || !strings.Contains(out, "Useful") {
t.Errorf("expected the alert in pager output, got %q", out)
}
}

View file

@ -13,6 +13,7 @@ import (
"charm.land/glamour/v2/ansi" "charm.land/glamour/v2/ansi"
"charm.land/glamour/v2/styles" "charm.land/glamour/v2/styles"
"charm.land/lipgloss/v2" "charm.land/lipgloss/v2"
"charm.land/x/nerdfont"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
) )
@ -94,7 +95,9 @@ func styleConfigFor(style string, isCode bool) (ansi.StyleConfig, error) {
style = styles.DarkStyle style = styles.DarkStyle
} }
if cfg, ok := styles.DefaultStyles[style]; ok { if cfg, ok := styles.DefaultStyles[style]; ok {
return *cfg, nil builtin := *cfg
useNerdFontAlertIcons(&builtin, style)
return builtin, nil
} }
return styleConfigFromFile(style) return styleConfigFromFile(style)
} }
@ -136,3 +139,37 @@ func styleConfigFromFile(path string) (ansi.StyleConfig, error) {
} }
return styleConfig, nil return styleConfig, nil
} }
// Nerd Font glyphs for the GitHub alerts, matching the octicons GitHub itself
// uses. Each one is followed by a space, like the Unicode fallback.
const (
nerdFontNote = "\uf449 " // nf-oct-info
nerdFontTip = "\uf400 " // nf-oct-light_bulb
nerdFontImportant = "\uf50a " // nf-oct-report
nerdFontWarning = "\uf421 " // nf-oct-alert
nerdFontCaution = "\uf46e " // nf-oct-stop
)
// useNerdFontAlertIcons swaps the alert icons of a built-in style for their
// Nerd Font counterparts when the terminal renders them, keeping the style's
// Unicode icon as the fallback. The plain styles are left alone, since they
// are meant to be portable.
func useNerdFontAlertIcons(cfg *ansi.StyleConfig, style string) {
if style == styles.NoTTYStyle || style == styles.AsciiStyle {
return
}
icons := []struct {
alert *ansi.StyleAlert
icon string
}{
{&cfg.Alerts.Note, nerdFontNote},
{&cfg.Alerts.Tip, nerdFontTip},
{&cfg.Alerts.Important, nerdFontImportant},
{&cfg.Alerts.Warning, nerdFontWarning},
{&cfg.Alerts.Caution, nerdFontCaution},
}
for _, i := range icons {
i.alert.Title.Prefix = nerdfont.Glyph(i.icon, i.alert.Title.Prefix)
}
}

59
utils/utils_test.go Normal file
View file

@ -0,0 +1,59 @@
package utils
import (
"testing"
"charm.land/glamour/v2/ansi"
"charm.land/glamour/v2/styles"
"charm.land/x/nerdfont"
)
func TestNerdFontAlertIcons(t *testing.T) {
t.Cleanup(nerdfont.Reset)
nerdfont.SetOverride(true)
cfg, err := styleConfigFor(styles.DarkStyle, false)
if err != nil {
t.Fatal(err)
}
for kind, want := range map[ansi.AlertKind]string{
ansi.AlertNote: nerdFontNote,
ansi.AlertTip: nerdFontTip,
ansi.AlertImportant: nerdFontImportant,
ansi.AlertWarning: nerdFontWarning,
ansi.AlertCaution: nerdFontCaution,
} {
if got := cfg.Alerts.For(kind).Title.Prefix; got != want {
t.Errorf("expected the %s Nerd Font icon, got %q", kind, got)
}
}
// The default styles themselves must not be modified.
if got := styles.DarkStyleConfig.Alerts.Note.Title.Prefix; got != "ℹ " {
t.Errorf("expected the default style to be left alone, got %q", got)
}
nerdfont.SetOverride(false)
cfg, err = styleConfigFor(styles.DarkStyle, false)
if err != nil {
t.Fatal(err)
}
if got := cfg.Alerts.Note.Title.Prefix; got != "ℹ " {
t.Errorf("expected the Unicode fallback, got %q", got)
}
}
func TestNerdFontAlertIconsPlainStyles(t *testing.T) {
t.Cleanup(nerdfont.Reset)
nerdfont.SetOverride(true)
for _, style := range []string{styles.NoTTYStyle, styles.AsciiStyle} {
cfg, err := styleConfigFor(style, false)
if err != nil {
t.Fatal(err)
}
if got := cfg.Alerts.Note.Title.Prefix; got != "" {
t.Errorf("expected no icon for the %s style, got %q", style, got)
}
}
}