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 example code #35

Merged
merged 1 commit into from
Dec 18, 2024
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# What is markdown package
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**.

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 (entity relationship diagram, sequence diagram, flowchart, pie chart)**, which was a necessary feature in spectest.
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 (entity relationship diagram, sequence diagram, flowchart, pie chart, architecture diagram)**, 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.

Expand All @@ -36,6 +36,7 @@ Additionally, complex code that increases the complexity of the library, such as
- [x] mermaid entity relationship diagram
- [x] mermaid flowchart
- [x] mermaid pie chart
- [x] mermaid architecture diagram (beta feature)

### Features not in Markdown syntax
- Generate badges; RedBadge(), YellowBadge(), GreenBadge().
Expand Down
2 changes: 1 addition & 1 deletion doc/architecture/generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ architecture-beta
junctionCenter:R -- L:junctionRight
top_gateway:B -- T:junctionRight
bottom_gateway:T -- B:junctionRight
```
```
32 changes: 16 additions & 16 deletions doc/er/generated.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
## Entity Relationship Diagram
```mermaid
erDiagram
teachers ||--o{ students : "Teacher has many students"
teachers }|..|| schools : "School has many teachers"
schools {
int id PK,UK "School ID"
string name "School Name"
int teacher_id FK,UK "Teacher ID"
}
students {
int id PK,UK "Student ID"
string name "Student Name"
int teacher_id FK,UK "Teacher ID"
}
teachers {
int id PK,UK "Teacher ID"
string name "Teacher Name"
}
teachers ||--o{ students : "Teacher has many students"
teachers }|..|| schools : "School has many teachers"
schools {
int id PK,UK "School ID"
string name "School Name"
int teacher_id FK,UK "Teacher ID"
}
students {
int id PK,UK "Student ID"
string name "Student Name"
int teacher_id FK,UK "Teacher ID"
}
teachers {
int id PK,UK "Teacher ID"
string name "Teacher Name"
}

```
16 changes: 8 additions & 8 deletions doc/flowchart/generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
title: mermaid flowchart builder
---
flowchart TB
A["Node A"]
B(["Node B"])
C[["Node C"]]
D[("Database")]
A-->B
B-->|"send original data"|D
B-->C
C-. "send filtered data" .-> D
A["Node A"]
B(["Node B"])
C[["Node C"]]
D[("Database")]
A-->B
B-->|"send original data"|D
B-->C
C-. "send filtered data" .-> D
```
118 changes: 118 additions & 0 deletions mermaid/arch/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//go:build linux || darwin

package arch_test

import (
"io"
"os"

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

// ExampleArchitecture 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 ExampleArchitecture() {
diagram := arch.NewArchitecture(io.Discard).
Service("left_disk", arch.IconDisk, "Disk").
Service("top_disk", arch.IconDisk, "Disk").
Service("bottom_disk", arch.IconDisk, "Disk").
Service("top_gateway", arch.IconInternet, "Gateway").
Service("bottom_gateway", arch.IconInternet, "Gateway").
Junction("junctionCenter").
Junction("junctionRight").
LF().
Edges(
arch.Edge{
ServiceID: "left_disk",
Position: arch.PositionRight,
Arrow: arch.ArrowNone,
},
arch.Edge{
ServiceID: "junctionCenter",
Position: arch.PositionLeft,
Arrow: arch.ArrowNone,
}).
Edges(
arch.Edge{
ServiceID: "top_disk",
Position: arch.PositionBottom,
Arrow: arch.ArrowNone,
},
arch.Edge{
ServiceID: "junctionCenter",
Position: arch.PositionTop,
Arrow: arch.ArrowNone,
}).
Edges(
arch.Edge{
ServiceID: "bottom_disk",
Position: arch.PositionTop,
Arrow: arch.ArrowNone,
},
arch.Edge{
ServiceID: "junctionCenter",
Position: arch.PositionBottom,
Arrow: arch.ArrowNone,
}).
Edges(
arch.Edge{
ServiceID: "junctionCenter",
Position: arch.PositionRight,
Arrow: arch.ArrowNone,
},
arch.Edge{
ServiceID: "junctionRight",
Position: arch.PositionLeft,
Arrow: arch.ArrowNone,
}).
Edges(
arch.Edge{
ServiceID: "top_gateway",
Position: arch.PositionBottom,
Arrow: arch.ArrowNone,
},
arch.Edge{
ServiceID: "junctionRight",
Position: arch.PositionTop,
Arrow: arch.ArrowNone,
}).
Edges(
arch.Edge{
ServiceID: "bottom_gateway",
Position: arch.PositionTop,
Arrow: arch.ArrowNone,
},
arch.Edge{
ServiceID: "junctionRight",
Position: arch.PositionBottom,
Arrow: arch.ArrowNone,
}).String() //nolint
nao1215 marked this conversation as resolved.
Show resolved Hide resolved

markdown.NewMarkdown(os.Stdout).
nao1215 marked this conversation as resolved.
Show resolved Hide resolved
H2("Architecture Diagram").
CodeBlocks(markdown.SyntaxHighlightMermaid, diagram).
Build() //nolint
nao1215 marked this conversation as resolved.
Show resolved Hide resolved

// Output:
//## Architecture Diagram
//```mermaid
nao1215 marked this conversation as resolved.
Show resolved Hide resolved
//architecture-beta
// service left_disk(disk)[Disk]
// service top_disk(disk)[Disk]
// service bottom_disk(disk)[Disk]
// service top_gateway(internet)[Gateway]
// service bottom_gateway(internet)[Gateway]
// junction junctionCenter
// junction junctionRight
//
// left_disk:R -- L:junctionCenter
// top_disk:B -- T:junctionCenter
// bottom_disk:T -- B:junctionCenter
// junctionCenter:R -- L:junctionRight
// top_gateway:B -- T:junctionRight
// bottom_gateway:T -- B:junctionRight
//```
}
8 changes: 4 additions & 4 deletions mermaid/er/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func (e *Entity) string() string {

return fmt.Sprintf(
"%s%s {%s%s%s%s}",
" ", // indent
" ", // indent
e.Name,
internal.LineFeed(),
strings.Join(attrs, internal.LineFeed()),
internal.LineFeed(),
" ", // indent
" ", // indent
)
}

Expand Down Expand Up @@ -70,7 +70,7 @@ func (a *Attribute) string() string {
keys = append(keys, "UK")
}

s := fmt.Sprintf(" %s %s %s \"%s\"", a.Type, a.Name, strings.Join(keys, ","), a.Comment)
s := fmt.Sprintf(" %s %s %s \"%s\"", a.Type, a.Name, strings.Join(keys, ","), a.Comment)
s = strings.TrimSuffix(s, " ")
return strings.ReplaceAll(s, "\"\"", "")
}
Expand All @@ -84,7 +84,7 @@ func (a *Attribute) string() string {
func (d *Diagram) Relationship(leftE, rightE Entity, leftR, rightR Relationship, identidy Identify, comment string) *Diagram {
d.body = append(
d.body,
fmt.Sprintf(" %s %s%s%s %s : \"%s\"",
fmt.Sprintf(" %s %s%s%s %s : \"%s\"",
leftE.Name,
leftR.string(left),
identidy.string(),
Expand Down
38 changes: 19 additions & 19 deletions mermaid/er/entity_relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,25 +133,25 @@ func TestDiagram_Build(t *testing.T) {
}

want := `erDiagram
teachers ||--o{ students : "Teacher has many students"
teachers }|..|| schools : "School has many teachers"
personal_computers {
int id PK,UK "Personal Computer ID"
}
schools {
int id PK,UK "School ID"
string name "School Name"
int teacher_id FK,UK "Teacher ID"
}
students {
int id PK,UK "Student ID"
string name "Student Name"
int teacher_id FK,UK "Teacher ID"
}
teachers {
int id PK,UK "Teacher ID"
string name "Teacher Name"
}
teachers ||--o{ students : "Teacher has many students"
teachers }|..|| schools : "School has many teachers"
personal_computers {
int id PK,UK "Personal Computer ID"
}
schools {
int id PK,UK "School ID"
string name "School Name"
int teacher_id FK,UK "Teacher ID"
}
students {
int id PK,UK "Student ID"
string name "Student Name"
int teacher_id FK,UK "Teacher ID"
}
teachers {
int id PK,UK "Teacher ID"
string name "Teacher Name"
}
`
want = strings.ReplaceAll(want, "\r\n", "\n")
got := strings.ReplaceAll(b.String(), "\r\n", "\n")
Expand Down
Loading
Loading