Skip to content

Commit

Permalink
Merge pull request #34 from nao1215/feat/arch
Browse files Browse the repository at this point in the history
Add Architecture Diagrams
  • Loading branch information
nao1215 authored Dec 18, 2024
2 parents 25fbbce + 791d32e commit 09f9d46
Show file tree
Hide file tree
Showing 15 changed files with 711 additions and 1 deletion.
134 changes: 134 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,140 @@ pie showData
"C" : 30
```

### Architecture Diagrams (beta feature)

[The mermaid provides a feature to visualize infrastructure architecture as a beta version](https://mermaid.js.org/syntax/architecture.html), and that feature has been introduced.

```go
package main

import (
"io"
"os"

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

//go:generate go run main.go

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

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

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

if err != nil {
panic(err)
}
```
Plain text output: [markdown is here](./doc/architecture/generated.md)
````
## Architecture Diagram
```mermaid
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
```
````
![Architecture Diagram](./doc/architecture/image.png)
## 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
18 changes: 18 additions & 0 deletions doc/architecture/generated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Architecture Diagram
```mermaid
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
```
Binary file added doc/architecture/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
107 changes: 107 additions & 0 deletions doc/architecture/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//go:build linux || darwin

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

import (
"io"
"os"

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

//go:generate go run main.go

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

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

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

if err != nil {
panic(err)
}
}
66 changes: 66 additions & 0 deletions mermaid/arch/architecture.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Package arch is mermaid architecture diagram builder.
// The building blocks of an architecture are groups, services, edges, and junctions.
// The arch package incorporates beta features of Mermaid, so the specifications are subject to significant changes.
package arch

import (
"fmt"
"io"
"strings"

"github.com/nao1215/markdown/internal"
)

// Architecture is a architecture diagram builder.
type Architecture struct {
// body is architecture diagram body.
body []string
// config is the configuration for the architecture diagram.
config *config
// dest is output destination for architecture diagram body.
dest io.Writer
// err manages errors that occur in all parts of the architecture building.
err error
}

// NewArchitecture returns a new Architecture.
func NewArchitecture(w io.Writer, opts ...Option) *Architecture {
c := newConfig()

for _, opt := range opts {
opt(c)
}

return &Architecture{
body: []string{"architecture-beta"},
dest: w,
config: c,
}
}

// String returns the architecture diagram body.
func (a *Architecture) String() string {
return strings.Join(a.body, internal.LineFeed())
}

// Build writes the architecture diagram body to the output destination.
func (a *Architecture) Build() error {
if _, err := a.dest.Write([]byte(a.String())); err != nil {
if a.err != nil {
return fmt.Errorf("failed to write: %w: %s", err, a.err.Error()) //nolint:wrapcheck
}
return fmt.Errorf("failed to write: %w", err)
}
return nil
}

// Error returns the error that occurred during the architecture diagram building.
func (a *Architecture) Error() error {
return a.err
}

// LF add a line feed to the architecture diagram body.
func (a *Architecture) LF() *Architecture {
a.body = append(a.body, "")
return a
}
Loading

0 comments on commit 09f9d46

Please sign in to comment.