Skip to content

Commit a5a88c6

Browse files
author
CHIKAMATSU Naohiro
committed
Feat generate status badge
1 parent 5f70811 commit a5a88c6

File tree

7 files changed

+149
-3
lines changed

7 files changed

+149
-3
lines changed

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ Additionally, complex code that increases the complexity of the library, such as
3030
- [x] text with image
3131
- [x] plain text
3232
- [x] details
33+
34+
Although not in Markdown syntax, there is a method to generate badges; RedBadge(), YellowBadge(), GreenBadge().
3335

3436
## Example
37+
### Basic usage
3538
```go
3639
package main
3740

@@ -135,10 +138,35 @@ func main() {
135138
If you want to see how it looks in Markdown, please refer to the following link.
136139
- [sample.md](./doc/generated_example.md)
137140

138-
## Generate Markdown using `"go generate ./..."`
141+
### Generate status badge
142+
The markdown package can create red, yellow, and green status badges.
143+
[Code example:](./doc/badge/main.go)
144+
```go
145+
md.NewMarkdown(os.Stdout).
146+
H1("badge example").
147+
RedBadge("red_badge").
148+
YellowBadge("yellow_badge").
149+
GreenBadge("green_badge").
150+
Build()
151+
```
152+
153+
[Output:](./doc/badge/generated.md)
154+
````text
155+
# badge example
156+
![Badge](https://img.shields.io/badge/red_badge-red)
157+
![Badge](https://img.shields.io/badge/yellow_badge-yellow)
158+
![Badge](https://img.shields.io/badge/green_badge-green)
159+
````
160+
161+
Your badge will look like this;
162+
![Badge](https://img.shields.io/badge/red_badge-red)
163+
![Badge](https://img.shields.io/badge/yellow_badge-yellow)
164+
![Badge](https://img.shields.io/badge/green_badge-green)
165+
166+
### Generate Markdown using `"go generate ./..."`
139167
You can generate Markdown using `go generate`. Please define code to generate Markdown first. Then, run `"go generate ./..."` to generate Markdown.
140168

141-
[Code example:](./doc/main.go)
169+
[Code example:](./doc/generate/main.go)
142170
```go
143171
package main
144172

@@ -168,7 +196,7 @@ Run below command:
168196
go generate ./...
169197
```
170198

171-
[Output:](./doc/generated.md)
199+
[Output:](./doc/generate/generated.md)
172200
````text
173201
# go generate example
174202
This markdown is generated by `go generate`

badge.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package markdown
2+
3+
import "fmt"
4+
5+
// RedBadge return text with red badge format.
6+
func (m *Markdown) RedBadge(text string) *Markdown {
7+
m.body = append(m.body, fmt.Sprintf("![Badge](https://img.shields.io/badge/%s-red)", text))
8+
return m
9+
}
10+
11+
// RedBadgef return text with red badge format.
12+
func (m *Markdown) RedBadgef(format string, args ...interface{}) *Markdown {
13+
return m.RedBadge(fmt.Sprintf(format, args...))
14+
}
15+
16+
// YellowBadge return text with yellow badge format.
17+
func (m *Markdown) YellowBadge(text string) *Markdown {
18+
m.body = append(m.body, fmt.Sprintf("![Badge](https://img.shields.io/badge/%s-yellow)", text))
19+
return m
20+
}
21+
22+
// YellowBadgef return text with yellow badge format.
23+
func (m *Markdown) YellowBadgef(format string, args ...interface{}) *Markdown {
24+
return m.YellowBadge(fmt.Sprintf(format, args...))
25+
}
26+
27+
// GreenBadge return text with green badge format.
28+
func (m *Markdown) GreenBadge(text string) *Markdown {
29+
m.body = append(m.body, fmt.Sprintf("![Badge](https://img.shields.io/badge/%s-green)", text))
30+
return m
31+
}
32+
33+
// GreenBadgef return text with green badge format.
34+
func (m *Markdown) GreenBadgef(format string, args ...interface{}) *Markdown {
35+
return m.GreenBadge(fmt.Sprintf(format, args...))
36+
}

badge_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package markdown
2+
3+
import (
4+
"io"
5+
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
)
9+
10+
func TestMarkdown_RedBadgef(t *testing.T) {
11+
t.Parallel()
12+
t.Run("success RedBadgef()", func(t *testing.T) {
13+
t.Parallel()
14+
15+
m := NewMarkdown(io.Discard)
16+
m.RedBadgef("%s", "Hello")
17+
want := []string{"![Badge](https://img.shields.io/badge/Hello-red)"}
18+
got := m.body
19+
20+
if diff := cmp.Diff(want, got); diff != "" {
21+
t.Errorf("value is mismatch (-want +got):\n%s", diff)
22+
}
23+
})
24+
25+
t.Run("success YellowBadgef()", func(t *testing.T) {
26+
t.Parallel()
27+
28+
m := NewMarkdown(io.Discard)
29+
m.YellowBadgef("%s", "Hello")
30+
want := []string{"![Badge](https://img.shields.io/badge/Hello-yellow)"}
31+
got := m.body
32+
33+
if diff := cmp.Diff(want, got); diff != "" {
34+
t.Errorf("value is mismatch (-want +got):\n%s", diff)
35+
}
36+
})
37+
38+
t.Run("success GreenBadgef()", func(t *testing.T) {
39+
t.Parallel()
40+
41+
m := NewMarkdown(io.Discard)
42+
m.GreenBadgef("%s", "Hello")
43+
want := []string{"![Badge](https://img.shields.io/badge/Hello-green)"}
44+
got := m.body
45+
46+
if diff := cmp.Diff(want, got); diff != "" {
47+
t.Errorf("value is mismatch (-want +got):\n%s", diff)
48+
}
49+
})
50+
}

doc/badge/generated.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# badge example
2+
![Badge](https://img.shields.io/badge/red_badge-red)
3+
![Badge](https://img.shields.io/badge/yellow_badge-yellow)
4+
![Badge](https://img.shields.io/badge/green_badge-green)

doc/badge/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//go:build linux || darwin
2+
3+
// Package main is generating markdown.
4+
package main
5+
6+
import (
7+
"os"
8+
9+
md "github.com/go-spectest/markdown"
10+
)
11+
12+
//go:generate go run main.go
13+
14+
func main() {
15+
f, err := os.Create("generated.md")
16+
if err != nil {
17+
panic(err)
18+
}
19+
20+
if err := md.NewMarkdown(f).
21+
H1("badge example").
22+
RedBadge("red_badge").
23+
YellowBadge("yellow_badge").
24+
GreenBadge("green_badge").
25+
Build(); err != nil {
26+
panic(err)
27+
}
28+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)