Compare commits

..

No commits in common. "feat/mermaid-diagrams" and "main" have entirely different histories.

9 changed files with 9 additions and 319 deletions

View file

@ -213,17 +213,8 @@ all: false
showLineNumbers: false
# preserve newlines in the output
preserveNewLines: false
# render mermaid diagrams as box-drawing art
mermaid: true
```
Mermaid support is on by default: ` ```mermaid ` blocks are drawn as
diagrams sized to the terminal width. Set `mermaid: false` to render them
as regular code blocks instead. Flowcharts (including cyclic ones),
sequence, state, class, ER, gantt, pie, mindmap, timeline, journey,
quadrant, xychart and gitGraph diagrams are supported; the rare remainder
falls back to its source with a note explaining why.
## Contributing
See [contributing][contribute].

View file

@ -23,8 +23,6 @@ pager: false
width: 80
# show all files, including hidden and ignored.
all: false
# render mermaid diagrams as box-drawing art.
mermaid: true
`
var configCmd = &cobra.Command{

247
demo.md
View file

@ -1,247 +0,0 @@
---
title: Glow Mermaid Playground
---
# Glow Mermaid Playground
Glow draws ` ```mermaid ` blocks as diagrams, sized to the terminal width.
Flowcharts (including cyclic ones), sequence, state, class, ER, gantt, pie,
mindmap, timeline, journey, quadrant, xychart and gitGraph diagrams are
rendered; the rare remainder falls back to the source. The icons in the
labels are [Nerd Font][nf] glyphs and need a Nerd Font-patched terminal
font to show up.
[nf]: https://www.nerdfonts.com
## CI pipeline
Top-down flowchart, dotted smoke tests, a thick fast lane, spaced edge
labels, a decision diamond and a dashed retry loop:
```mermaid
flowchart TD
L[" lint"] --> T[" test"]
T --> B[" build"]
B -.-> S[" smoke"]
S --> G{{" pass?"}}
G -- yes --> P[" deploy"]
G -- no --> F[" fix"]
F ==> P
F -.-> T
```
## Service architecture
Left-to-right flowchart with every shape, fanning out to workers and back
into the store:
```mermaid
flowchart LR
U(( user)) --> A( auth)
A --> Q([ queue])
Q --> W1[" worker A"]
Q --> W2[" worker B"]
W1 --> S{{" store"}}
W2 --> S
```
## Sequence diagram
Participants, solid and dashed messages, every arrowhead style, notes and
a loop frame:
```mermaid
sequenceDiagram
participant U as  user
participant A as  auth
U->>A: login
A-->>U: token
Note over U,A: refresh before expiry
U->U: refresh
loop until valid
U->>A: request
A--xU: denied
end
A-)U: welcome
```
## State machine
Start and end markers, aliased states and transitions:
```mermaid
stateDiagram-v2
[*] --> Idle
state "Long idle" as Idle
Idle --> Running : start
Running --> Paused : pause
Paused --> Running : resume
Running --> Idle : stop
Idle --> [*]
```
## Class diagram
Members, stereotypes, inheritance, realization and aggregation:
```mermaid
classDiagram
class Animal {
+String name
+makeSound()
}
class Duck {
+swim()
}
class Flyable {
<<interface>>
+fly()
}
Animal <|-- Duck
Duck ..|> Flyable
Duck "1" o-- "2" Feet : has
```
## ER diagram
Entities with attributes and labelled relationships:
```mermaid
erDiagram
CUSTOMER {
string name PK
}
ORDER {
int total
}
ITEM {
string sku
}
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ ITEM : contains
```
## Gantt chart
Sections, task flags and a milestone on a scaled time axis:
```mermaid
gantt
title Release plan
dateFormat YYYY-MM-DD
section Prep
 Design :done, des1, 2024-01-01, 5d
 Build :crit, 2024-01-06, 10d
section Ship
 Test :active, 2024-01-16, 4d
 Launch :milestone, 2024-01-20, 0d
```
## Pie chart
```mermaid
pie title Response codes
"2xx" : 812
"3xx" : 44
"4xx" : 130
"5xx" : 14
```
## Mindmap
```mermaid
mindmap
root((glow features))
Rendering
Markdown
Diagrams
TUI
Pager
Stash
```
## Timeline
```mermaid
timeline
title Terminal era
1970s : VT100 : Bourne shell
1990s : Linux : xterm
2020s : GPUs in the shell : ligatures
```
## Journey
```mermaid
journey
title Reading a README
section Setup
Clone repo: 5: Me
Install glow: 4: Me
section Reading
Open file: 8: Me, You
```
## Quadrant chart
```mermaid
quadrantChart
title Feature planning
x-axis Low effort --> High effort
y-axis Low value --> High value
quadrant-1 Do later
quadrant-2 Quick wins
quadrant-3 Drop
quadrant-4 Bet big
diagrams: [0.25, 0.8]
theming: [0.15, 0.5]
```
## XY chart
```mermaid
xychart-beta
title Issues closed per month
x-axis [jan, feb, mar, apr, may]
bar [12, 19, 8, 24, 31]
```
## Git graph
```mermaid
gitGraph
commit
commit
branch feature
commit
checkout main
commit
merge feature
commit
```
## Error handling
A deeper flowchart tree with an early exit:
```mermaid
flowchart TD
R[" request"] --> V[" validate"]
V --> E{{" format<br/>error?"}}
E -- yes --> H[" 400 bad request"]
E -- no --> X[" execute"]
X --> O({{" 500?"}})
O -- no --> K[" 200 ok"]
O -- retry --> R2[" retry 3"]
R2 --> H2[" 503 give up"]
```
## Unsupported diagrams
The rare remainder, like C4 diagrams, sankeys and packet charts, degrade
to the source plus a note explaining why:
```mermaid
C4Context
person(user, User)
```

6
go.mod
View file

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

12
go.sum
View file

@ -2,8 +2,8 @@ charm.land/bubbles/v2 v2.1.1 h1:7r55WzBxpo/R3z98hGmY7KKPd3ET6vsf0Fb9sDHOV60=
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/go.mod h1:2SkdgoTXluXJHOUwAoRlRXF/28vklb1rFl6GcgV1/ss=
charm.land/glamour/v2 v2.0.2-0.20261005110102-e687bdb00297 h1:1g0qI17CH9t0OZlqlApglKwX6aumJ9FVrwMWPBKgA2I=
charm.land/glamour/v2 v2.0.2-0.20261005110102-e687bdb00297/go.mod h1:wqGcmrnBmbunm0/LszemUKpYwWxvPPBkEno7KusMy6s=
charm.land/glamour/v2 v2.0.1 h1:xl+r00A4aJWU0z8fgwKd9fQQ4rsphqGUzuEiXZP5n+c=
charm.land/glamour/v2 v2.0.1/go.mod h1:jo9z8XqVKPeEFMVdvCRLGk++RyJ3CdUwgNr7EvXLw3k=
charm.land/lipgloss/v2 v2.0.6 h1:EaGKeuA8FvF+v2BT5VmZd2LoYLaMZJXA5n34th8nCIQ=
charm.land/lipgloss/v2 v2.0.6/go.mod h1:ipDDJNSGa1hlwDtSfW1s2/xR8Vdhbut4PXh2zEKZd0Q=
github.com/alecthomas/assert/v2 v2.7.0 h1:QtqSACNS3tF7oasA8CU6A6sXZSBDqnm7RfpLl9bZqbE=
@ -153,8 +153,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/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
github.com/yuin/goldmark v1.8.5 h1:r6N5afV5qj/5S4UTch8agZHJ8UxNCMwX7WjkkJam2NA=
github.com/yuin/goldmark v1.8.5/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
github.com/yuin/goldmark v1.7.17 h1:p36OVWwRb246iHxA/U4p8OPEpOTESm4n+g+8t0EE5uA=
github.com/yuin/goldmark v1.7.17/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
github.com/yuin/goldmark-emoji v1.0.5 h1:EMVWyCGPlXJfUXBXpuMu+ii3TIaxbVBnEX9uaDC4cIk=
github.com/yuin/goldmark-emoji v1.0.5/go.mod h1:tTkZEbwu5wkPmgTcitqddVxY9osFZiavD+r4AzQrh1U=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
@ -170,8 +170,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/term v0.44.0 h1:0rLvDRCtNj0gZkyIXhCyOb2OAzEhLVqc4B+hrsBhrmc=
golang.org/x/term v0.44.0/go.mod h1:7ze4MdzUzLXpSAoFP1H0bOI9aXDqveSvatT5vKcFh2Y=
golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8=
golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M=
golang.org/x/text v0.39.0 h1:UbZz4pLOvn600D6Oh6GGEI6VAmndrEBLv8/6BEXzyus=
golang.org/x/text v0.39.0/go.mod h1:3UwRclnC2g0TU9x8PZiyfOajCd1zaUNHF9cvqcQZ+ZM=
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/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

View file

@ -42,7 +42,6 @@ var (
showAllFiles bool
showLineNumbers bool
preserveNewLines bool
mermaid bool
mouse bool
rootCmd = &cobra.Command{
@ -173,7 +172,6 @@ func validateOptions(cmd *cobra.Command) error {
showAllFiles = viper.GetBool("all")
preserveNewLines = viper.GetBool("preserveNewLines")
showLineNumbers = viper.GetBool("showLineNumbers")
mermaid = viper.GetBool("mermaid")
if pager && tui {
return errors.New("cannot use both pager and tui")
@ -297,7 +295,6 @@ func executeCLI(cmd *cobra.Command, src *source, w io.Writer) error {
glamour.WithWordWrap(int(width)), //nolint:gosec
glamour.WithBaseURL(baseURL),
glamour.WithPreservedNewLines(),
glamour.WithMermaid(mermaid),
)
if err != nil {
return fmt.Errorf("unable to create renderer: %w", err)
@ -370,7 +367,6 @@ func runTUI(path string, content string) error {
cfg.GlamourMaxWidth = width
cfg.EnableMouse = mouse
cfg.PreserveNewLines = preserveNewLines
cfg.Mermaid = mermaid
// Run Bubble Tea program
prog, cleanup := ui.NewProgram(cfg, content)
@ -433,7 +429,6 @@ func init() {
viper.SetDefault("style", "auto")
viper.SetDefault("width", 0)
viper.SetDefault("all", true)
viper.SetDefault("mermaid", true)
rootCmd.AddCommand(configCmd, manCmd)
}

View file

@ -10,7 +10,6 @@ type Config struct {
GlamourStyle string `env:"GLAMOUR_STYLE"`
EnableMouse bool
PreserveNewLines bool
Mermaid bool
// Working directory or file path
Path string

View file

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

View file

@ -41,48 +41,3 @@ func TestGlamourRenderTextSizing(t *testing.T) {
t.Errorf("expected no leftover markers in pager output, got %q", out)
}
}
func TestGlamourRenderMermaid(t *testing.T) {
t.Setenv("LC_ALL", "en_US.UTF-8")
md := "# Plan\n\n```mermaid\nflowchart LR\nA[Build] --> B{Gate}\nB -->|yes| C[Ship]\n```\n"
newTestPager := func(mermaid bool) pagerModel {
common := &commonModel{
cfg: Config{
GlamourEnabled: true,
GlamourStyle: "dark",
GlamourMaxWidth: 80,
Mermaid: mermaid,
},
styles: newStyles(true),
width: 80,
height: 24,
}
m := newPagerModel(common)
m.currentDocument = markdown{Note: "test.md"}
m.setSize(80, 24)
return m
}
out, err := glamourRender(newTestPager(true), md)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out, "┌") || !strings.Contains(out, "Build") {
t.Errorf("expected a rendered diagram, got %q", out)
}
if strings.Contains(out, "flowchart LR") {
t.Errorf("expected the source not to be shown, got %q", out)
}
out, err = glamourRender(newTestPager(false), md)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out, "flowchart LR") {
t.Errorf("expected the source to be shown when disabled, got %q", out)
}
if strings.ContainsAny(out, "┌┐└┘") {
t.Errorf("expected no diagram when disabled, got %q", out)
}
}