Skip to content

Commit

Permalink
Merge pull request #21 from nao1215/introduce/mermaid
Browse files Browse the repository at this point in the history
Introduce mermaid sequence
  • Loading branch information
nao1215 authored May 5, 2024
2 parents e758807 + dca28ed commit 97b5eb5
Show file tree
Hide file tree
Showing 17 changed files with 1,291 additions and 25 deletions.
139 changes: 116 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
[![Gosec](https://github.com/nao1215/markdown/actions/workflows/gosec.yml/badge.svg)](https://github.com/nao1215/markdown/actions/workflows/gosec.yml)
![Coverage](https://raw.githubusercontent.com/nao1215/octocovs-central-repo/main/badges/nao1215/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 syntax of Markdown follows GitHub Markdown.
The Package markdown is a simple markdown builder in golang. The markdown package 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 [nao1215/spectest](https://github.com/nao1215/spectest). Therefore, it implements the features required by spectest, but there are no plans to add additional functionalities unless requested by someone.
The markdown package was initially developed to save test results in [nao1215/spectest](https://github.com/nao1215/spectest). Therefore, the markdown package implements the features required by spectest. For example, the markdown package supports **mermaid sequence diagrams**, which was a necessary feature in spectest.

Additionally, complex code that increases the complexity of the library, such as generating nested lists, will not be added. I want to keep this library as simple as possible.

## Supported OS and go version
- OS: Linux, macOS, Windows
- Go: 1.18 or later
- Go: 1.20 or later

## Supported Markdown features
- [x] Heading; H1, H2, H3, H4, H5, H6
Expand All @@ -32,6 +32,7 @@ Additionally, complex code that increases the complexity of the library, such as
- [x] Plain text
- [x] Details
- [x] Alerts; NOTE, TIP, IMPORTANT, CAUTION, WARNING
- [x] mermaid (only support sequence diagram)

### Features not in Markdown syntax
- Generate badges; RedBadge(), YellowBadge(), GreenBadge().
Expand Down Expand Up @@ -142,6 +143,45 @@ 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 Markdown using `"go generate ./..."`
You can generate Markdown using `go generate`. Please define code to generate Markdown first. Then, run `"go generate ./..."` to generate Markdown.

[Code example:](./doc/generate/main.go)
```go
package main

import (
"os"

md "github.com/nao1215/markdown"
)

//go:generate go run main.go

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

md.NewMarkdown(f).
H1("go generate example").
PlainText("This markdown is generated by `go generate`").
Build()
}
```

Run below command:
```shell
go generate ./...
```

[Output:](./doc/generate/generated.md)
````text
# go generate example
This markdown is generated by `go generate`
````

### 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)
Expand Down Expand Up @@ -216,45 +256,98 @@ Your badge will look like this;
![Badge](https://img.shields.io/badge/yellow_badge-yellow)
![Badge](https://img.shields.io/badge/green_badge-green)

### Generate Markdown using `"go generate ./..."`
You can generate Markdown using `go generate`. Please define code to generate Markdown first. Then, run `"go generate ./..."` to generate Markdown.
### Generate mermaid sequence diagram
Generate mermaid in markdown. You can generate mermaid using go generate. Please define code to generate Mermaid first. Then, run "go generate ./..." to generate it.

[Code example:](./doc/generate/main.go)
```go
package main

import (
"os"

md "github.com/nao1215/markdown"
"github.com/nao1215/markdown"
"github.com/nao1215/mermaid/sequence"
)

//go:generate go run main.go

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

md.NewMarkdown(f).
H1("go generate example").
PlainText("This markdown is generated by `go generate`").
diagram := sequence.NewDiagram(os.Stdout).
Participant("Sophia").
Participant("David").
Participant("Subaru").
LF().
SyncRequest("Sophia", "David", "Please wake up Subaru").
SyncResponse("David", "Sophia", "OK").
LF().
LoopStart("until Subaru wake up").
SyncRequest("David", "Subaru", "Wake up!").
SyncResponse("Subaru", "David", "zzz").
SyncRequest("David", "Subaru", "Hey!!!").
BreakStart("if Subaru wake up").
SyncResponse("Subaru", "David", "......").
BreakEnd().
LoopEnd().
LF().
SyncResponse("David", "Sophia", "wake up, wake up").
String()

markdown.NewMarkdown(os.Stdout).
H2("Sequence Diagram").
CodeBlocks(markdown.SyntaxHighlightMermaid, diagram).
Build()
}
```

Run below command:
```shell
go generate ./...
Plain text output: [markdown is here](./doc/sequence/generated.md)
````
## Sequence Diagram
```mermaid
sequenceDiagram
participant Sophia
participant David
participant Subaru
Sophia->>David: Please wake up Subaru
David-->>Sophia: OK
loop until Subaru wake up
David->>Subaru: Wake up!
Subaru-->>David: zzz
David->>Subaru: Hey!!!
break if Subaru wake up
Subaru-->>David: ......
end
end
David-->>Sophia: wake up, wake up
```

[Output:](./doc/generate/generated.md)
````text
# go generate example
This markdown is generated by `go generate`
````

Mermaid output:
```mermaid
sequenceDiagram
participant Sophia
participant David
participant Subaru
Sophia->>David: Please wake up Subaru
David-->>Sophia: OK
loop until Subaru wake up
David->>Subaru: Wake up!
Subaru-->>David: zzz
David->>Subaru: Hey!!!
break if Subaru wake up
Subaru-->>David: ......
end
end
David-->>Sophia: wake up, wake up
```



## Creating an index for a directory full of markdown files
The markdown package can create an index for Markdown files within the specified directory. This feature was added to generate indexes for Markdown documents produced by [nao1215/spectest](https://github.com/nao1215/spectest).

Expand Down
21 changes: 21 additions & 0 deletions doc/sequence/generated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Sequence Diagram
```mermaid
sequenceDiagram
participant Sophia
participant David
participant Subaru
Sophia->>David: Please wake up Subaru
David-->>Sophia: OK
loop until Subaru wake up
David->>Subaru: Wake up!
Subaru-->>David: zzz
David->>Subaru: Hey!!!
break if Subaru wake up
Subaru-->>David: ......
end
end
David-->>Sophia: wake up, wake up
```
49 changes: 49 additions & 0 deletions doc/sequence/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//go:build linux || darwin

// Package main is generating mermaid sequence diagram.
package main

import (
"os"

"github.com/nao1215/markdown"
"github.com/nao1215/markdown/mermaid/sequence"
)

//go:generate go run main.go

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

diagram := sequence.NewDiagram(os.Stdout).
Participant("Sophia").
Participant("David").
Participant("Subaru").
LF().
SyncRequest("Sophia", "David", "Please wake up Subaru").
SyncResponse("David", "Sophia", "OK").
LF().
LoopStart("until Subaru wake up").
SyncRequest("David", "Subaru", "Wake up!").
SyncResponse("Subaru", "David", "zzz").
SyncRequest("David", "Subaru", "Hey!!!").
BreakStart("if Subaru wake up").
SyncResponse("Subaru", "David", "......").
BreakEnd().
LoopEnd().
LF().
SyncResponse("David", "Sophia", "wake up, wake up").
String() //nolint

err = markdown.NewMarkdown(f).
H2("Sequence Diagram").
CodeBlocks(markdown.SyntaxHighlightMermaid, diagram).
Build()

if err != nil {
panic(err)
}
}
59 changes: 57 additions & 2 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"os"

md "github.com/nao1215/markdown"
"github.com/nao1215/markdown/mermaid/sequence"
)

// Examle is example code. Skip this test on Windows.
// ExamleMarkdown skip this test on Windows.
// The newline codes in the comment section where
// the expected values are written are represented as '\n',
// causing failures when testing on Windows.
func Example() {
func ExampleMarkdown() {
md.NewMarkdown(os.Stdout).
H1("This is H1").
PlainText("This is plain text").
Expand Down Expand Up @@ -92,3 +93,57 @@ func main() {
// ## Image
// ![sample_image](./sample.png)
}

// ExampleSequence skip this test on Windows.
// The newline codes in the comment section where
// the expected values are written are represented as '\n',
// causing failures when testing on Windows.
func ExampleSequence() {
diagram := sequence.NewDiagram(os.Stdout).
Participant("Sophia").
Participant("David").
Participant("Subaru").
LF().
SyncRequest("Sophia", "David", "Please wake up Subaru").
SyncResponse("David", "Sophia", "OK").
LF().
LoopStart("until Subaru wake up").
SyncRequest("David", "Subaru", "Wake up!").
SyncResponse("Subaru", "David", "zzz").
SyncRequest("David", "Subaru", "Hey!!!").
BreakStart("if Subaru wake up").
SyncResponse("Subaru", "David", "......").
BreakEnd().
LoopEnd().
LF().
SyncResponse("David", "Sophia", "wake up, wake up").
String()

md.NewMarkdown(os.Stdout).
H2("Sequence Diagram").
CodeBlocks(md.SyntaxHighlightMermaid, diagram).
Build() //nolint

// Output:
// ## Sequence Diagram
// ```mermaid
// sequenceDiagram
// participant Sophia
// participant David
// participant Subaru
//
// Sophia->>David: Please wake up Subaru
// David-->>Sophia: OK
//
// loop until Subaru wake up
// David->>Subaru: Wake up!
// Subaru-->>David: zzz
// David->>Subaru: Hey!!!
// break if Subaru wake up
// Subaru-->>David: ......
// end
// end
//
// David-->>Sophia: wake up, wake up
// ```
}
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ github.com/karrick/godirwalk v1.17.0 h1:b4kY7nqDdioR/6qnbHQyDvmA17u5G1cZ6J+CZXwS
github.com/karrick/godirwalk v1.17.0/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/nao1215/mermaid v0.0.2 h1:wLJEqsMUhVFVMgz8Uw2qoXD3mK/KjHjZhO9Ho5N/igo=
github.com/nao1215/mermaid v0.0.2/go.mod h1:CqJBmXtlQycPzTWLK+P4nKKFUCp5Aam4nucy401sckM=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
Loading

0 comments on commit 97b5eb5

Please sign in to comment.