Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alert; NOTE, TIP, IMPORTANT, CAUTION, WARNING #12

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Gosec](https://github.com/go-spectest/markdown/actions/workflows/gosec.yml/badge.svg)](https://github.com/go-spectest/markdown/actions/workflows/gosec.yml)
![Coverage](https://raw.githubusercontent.com/go-spectest/octocovs-central-repo/main/badges/go-spectest/markdown/coverage.svg)
# What is markdown package
The Package markdown is a simple markdown builder in golang. This library assembles Markdown using method chaining, not uses a template engine like [html/template](https://pkg.go.dev/html/template).
The Package markdown is a simple markdown builder in golang. This library assembles Markdown using method chaining, not uses a template engine like [html/template](https://pkg.go.dev/html/template). The syntax of Markdown follows GitHub Markdown.

This library was initially developed to display test results in [go-spectest/spectest](https://github.com/go-spectest/spectest). Therefore, it implements the features required by spectest, but there are no plans to add additional functionalities unless requested by someone.

Expand All @@ -26,10 +26,11 @@ Additionally, complex code that increases the complexity of the library, such as
- [x] Horizontal rule
- [x] Table
- [x] Text formatting; bold, italic, code, strikethrough, bold italic
- [x] text with link
- [x] text with image
- [x] plain text
- [x] details
- [x] Text with link
- [x] Text with image
- [x] Plain text
- [x] Details
- [x] Alerts; NOTE, TIP, IMPORTANT, CAUTION, WARNING

### Features not in Markdown syntax
- Generate badges; RedBadge(), YellowBadge(), GreenBadge().
Expand Down Expand Up @@ -140,6 +141,56 @@ func main() {
If you want to see how it looks in Markdown, please refer to the following link.
- [sample.md](./doc/generated_example.md)

### Generate alerts
The markdown package can create alerts. Alerts are useful for displaying important information in Markdown. This syntax is supported by GitHub.
[Code example:](./doc/alert/main.go)
```go
md.NewMarkdown(f).
H1("Alert example").
Note("This is note").LF().
Tip("This is tip").LF().
Important("This is important").LF().
Warning("This is warning").LF().
Caution("This is caution").LF().
Build()
```

[Output:](./doc/alert/generated.md)
````text
# Alert example
> [!NOTE]
> This is note

> [!TIP]
> This is tip

> [!IMPORTANT]
> This is important

> [!WARNING]
> This is warning

> [!CAUTION]
> This is caution
````

Your alert will look like this;
# Alert example
> [!NOTE]
> This is note

> [!TIP]
> This is tip

> [!IMPORTANT]
> This is important

> [!WARNING]
> This is warning

> [!CAUTION]
> This is caution

### Generate status badge
The markdown package can create red, yellow, and green status badges.
[Code example:](./doc/badge/main.go)
Expand Down
58 changes: 58 additions & 0 deletions alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package markdown

import "fmt"

// Note set text with note format.
func (m *Markdown) Note(text string) *Markdown {
m.body = append(m.body, fmt.Sprintf("> [!NOTE] \n> %s", text))
return m
}

// Notef set text with note format. It is similar to fmt.Sprintf.
func (m *Markdown) Notef(format string, args ...interface{}) *Markdown {
return m.Note(fmt.Sprintf(format, args...))
}

// Tip set text with tip format.
func (m *Markdown) Tip(text string) *Markdown {
m.body = append(m.body, fmt.Sprintf("> [!TIP] \n> %s", text))
return m
}

// Tipf set text with tip format. It is similar to fmt.Sprintf.
func (m *Markdown) Tipf(format string, args ...interface{}) *Markdown {
return m.Tip(fmt.Sprintf(format, args...))
}

// Important set text with important format.
func (m *Markdown) Important(text string) *Markdown {
m.body = append(m.body, fmt.Sprintf("> [!IMPORTANT] \n> %s", text))
return m
}

// Importantf set text with important format. It is similar to fmt.Sprintf.
func (m *Markdown) Importantf(format string, args ...interface{}) *Markdown {
return m.Important(fmt.Sprintf(format, args...))
}

// Warning set text with warning format.
func (m *Markdown) Warning(text string) *Markdown {
m.body = append(m.body, fmt.Sprintf("> [!WARNING] \n> %s", text))
return m
}

// Warningf set text with warning format. It is similar to fmt.Sprintf.
func (m *Markdown) Warningf(format string, args ...interface{}) *Markdown {
return m.Warning(fmt.Sprintf(format, args...))
}

// Caution set text with caution format.
func (m *Markdown) Caution(text string) *Markdown {
m.body = append(m.body, fmt.Sprintf("> [!CAUTION] \n> %s", text))
return m
}

// Cautionf set text with caution format. It is similar to fmt.Sprintf.
func (m *Markdown) Cautionf(format string, args ...interface{}) *Markdown {
return m.Caution(fmt.Sprintf(format, args...))
}
77 changes: 77 additions & 0 deletions alert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package markdown

import (
"io"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestMarkdownAlerts(t *testing.T) {
t.Parallel()

t.Run("success Notef()", func(t *testing.T) {
t.Parallel()

m := NewMarkdown(io.Discard)
m.Notef("%s", "Hello")
want := []string{"> [!NOTE] \n> Hello"}
got := m.body

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})

t.Run("success Warningf()", func(t *testing.T) {
t.Parallel()

m := NewMarkdown(io.Discard)
m.Warningf("%s", "Hello")
want := []string{"> [!WARNING] \n> Hello"}
got := m.body

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})

t.Run("success Tipf()", func(t *testing.T) {
t.Parallel()

m := NewMarkdown(io.Discard)
m.Tipf("%s", "Hello")
want := []string{"> [!TIP] \n> Hello"}
got := m.body

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})

t.Run("success Importantf()", func(t *testing.T) {
t.Parallel()

m := NewMarkdown(io.Discard)
m.Importantf("%s", "Hello")
want := []string{"> [!IMPORTANT] \n> Hello"}
got := m.body

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})

t.Run("success Cautionf()", func(t *testing.T) {
t.Parallel()

m := NewMarkdown(io.Discard)
m.Cautionf("%s", "Hello")
want := []string{"> [!CAUTION] \n> Hello"}
got := m.body

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})
}
12 changes: 6 additions & 6 deletions badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@ package markdown

import "fmt"

// RedBadge return text with red badge format.
// RedBadge set text with red badge format.
func (m *Markdown) RedBadge(text string) *Markdown {
m.body = append(m.body, fmt.Sprintf("![Badge](https://img.shields.io/badge/%s-red)", text))
return m
}

// RedBadgef return text with red badge format.
// RedBadgef set text with red badge format. It is similar to fmt.Sprintf.
func (m *Markdown) RedBadgef(format string, args ...interface{}) *Markdown {
return m.RedBadge(fmt.Sprintf(format, args...))
}

// YellowBadge return text with yellow badge format.
// YellowBadge set text with yellow badge format.
func (m *Markdown) YellowBadge(text string) *Markdown {
m.body = append(m.body, fmt.Sprintf("![Badge](https://img.shields.io/badge/%s-yellow)", text))
return m
}

// YellowBadgef return text with yellow badge format.
// YellowBadgef set text with yellow badge format. It is similar to fmt.Sprintf.
func (m *Markdown) YellowBadgef(format string, args ...interface{}) *Markdown {
return m.YellowBadge(fmt.Sprintf(format, args...))
}

// GreenBadge return text with green badge format.
// GreenBadge set text with green badge format.
func (m *Markdown) GreenBadge(text string) *Markdown {
m.body = append(m.body, fmt.Sprintf("![Badge](https://img.shields.io/badge/%s-green)", text))
return m
}

// GreenBadgef return text with green badge format.
// GreenBadgef set text with green badge format. It is similar to fmt.Sprintf.
func (m *Markdown) GreenBadgef(format string, args ...interface{}) *Markdown {
return m.GreenBadge(fmt.Sprintf(format, args...))
}
16 changes: 16 additions & 0 deletions doc/alert/generated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Alert example
> [!NOTE]
> This is note

> [!TIP]
> This is tip

> [!IMPORTANT]
> This is important

> [!WARNING]
> This is warning

> [!CAUTION]
> This is caution

30 changes: 30 additions & 0 deletions doc/alert/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build linux || darwin

// Package main is generating markdown.
package main

import (
"os"

md "github.com/go-spectest/markdown"
)

//go:generate go run main.go

func main() {
f, err := os.Create("generated.md")
if err != nil {
panic(err)
}

if err := md.NewMarkdown(f).
H1("Alert example").
Note("This is note").LF().
Tip("This is tip").LF().
Important("This is important").LF().
Warning("This is warning").LF().
Caution("This is caution").LF().
Build(); err != nil {
panic(err)
}
}