Skip to content

Commit

Permalink
feat: generate sequence diagrams from code blocks (#35)
Browse files Browse the repository at this point in the history
Close #35
  • Loading branch information
olOwOlo committed Mar 12, 2018
1 parent 7043881 commit 5bcf92d
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 3 deletions.
4 changes: 4 additions & 0 deletions archetypes/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ flowchartDiagrams:
enable: false
options: ""

sequenceDiagrams:
enable: false
options: ""

---

<!--more-->
6 changes: 5 additions & 1 deletion exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ copyright = "" # default: author.name ↓ # 默认为下面配
clientId = "" # Your client ID
clientSecret = "" # Your client secret

[params.flowchartDiagrams]# see https://blog.olowolo.com/example-site/post/js-sequence-diagrams/
[params.flowchartDiagrams]# see https://blog.olowolo.com/example-site/post/js-flowchart-diagrams/
enable = false
options = ""

[params.sequenceDiagrams] # see https://blog.olowolo.com/example-site/post/js-sequence-diagrams/
enable = false
options = "" # default: "{theme: 'simple'}"

[params.busuanzi] # count web traffic by busuanzi # 是否使用不蒜子统计站点访问量
enable = false
siteUV = true
Expand Down
95 changes: 95 additions & 0 deletions exampleSite/content/post/js-sequence-diagrams.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title: "JS Sequence Diagrams"
date: 2015-03-04T21:57:45+08:00
draft: false

sequenceDiagrams:
enable: true
options: "{theme: 'hand'}"
---

## Usage

```sequence
Andrew->China: Says Hello
Note right of China: China thinks\nabout it
China-->Andrew: How are you?
Andrew->>China: I am good thanks!
```

<!--more-->

```sequence
Andrew->China: Says Hello
Note right of China: China thinks\nabout it
China-->Andrew: How are you?
Andrew->>China: I am good thanks!
```

## Configuration

Configure for all home and regular pages:

```toml
[params.sequenceDiagrams]
enable = true
options = "{theme: 'hand'}"
```

Configure for a single post in the front matter (**Params in front matter have higher precedence**):

```yml
sequenceDiagrams:
enable: true
options: "{theme: 'hand'}"
```
### Options
```js
options = {
// Change the styling of the diagram, typically one of 'simple', 'hand'. New themes can be registered with registerTheme(...).
theme: string,

// CSS style to apply to the diagram's svg tag. (Only supported if using snap.svg)
css_class: string,
}
```

See more information from https://github.com/bramp/js-sequence-diagrams.

## Examples

```sequence
Title: Here is a title
A->B: Normal line
B-->C: Dashed line
C->>D: Open arrow
D-->>A: Dashed open arrow
```

```sequence
Title: Here is a title
A->B: Normal line
B-->C: Dashed line
C->>D: Open arrow
D-->>A: Dashed open arrow
```

---

```sequence
# Example of a comment.
Note left of A: Note to the\n left of A
Note right of A: Note to the\n right of A
Note over A: Note over A
Note over A,B: Note over both A and B
```

```sequence
# Example of a comment.
Note left of A: Note to the\n left of A
Note right of A: Note to the\n right of A
Note over A: Note over A
Note over A,B: Note over both A and B
```
16 changes: 16 additions & 0 deletions layouts/partials/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/raphael.min.js" integrity="sha256-67By+NpOtm9ka1R6xpUefeGOY8kWWHHRAKlvaTJ7ONI=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/release/flowchart.min.js" integrity="sha256-zNGWjubXoY6rb5MnmpBNefO0RgoVYfle9p0tvOQM+6k=" crossorigin="anonymous"></script>
{{- end -}}

<!-- js-sequence-diagrams -->
{{- if and (or .Params.sequenceDiagrams.enable (and .Site.Params.sequenceDiagrams.enable (ne .Params.sequenceDiagrams.enable false))) (or .IsPage .IsHome) -}}
<script>
{{- if .Params.sequenceDiagrams.options -}}
window.sequenceDiagramsOptions = {{ .Params.sequenceDiagrams.options | safeJS }};
{{- else if .Site.Params.sequenceDiagrams.options -}}
window.sequenceDiagramsOptions = {{ .Site.Params.sequenceDiagrams.options | safeJS }};
{{- end -}}
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/webfontloader.js" integrity="sha256-4O4pS1SH31ZqrSO2A/2QJTVjTPqVe+jnYgOWUVr7EEc=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/snap.svg-min.js" integrity="sha256-oI+elz+sIm+jpn8F/qEspKoKveTc5uKeFHNNVexe6d8=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/underscore-min.js" integrity="sha256-obZACiHd7gkOk9iIL/pimWMTJ4W/pBsKu+oZnSeBIek=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/gh/bramp/[email protected]/dist/sequence-diagram-min.js" integrity="sha384-8748Vn52gHJYJI0XEuPB2QlPVNUkJlJn9tHqKec6J3q2r9l8fvRxrgn/E5ZHV0sP" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/bramp/[email protected]/dist/sequence-diagram-min.css" integrity="sha384-6QbLKJMz5dS3adWSeINZe74uSydBGFbnzaAYmp+tKyq60S7H2p6V7g1TysM5lAaF" crossorigin="anonymous">
{{- end }}
<script type="text/javascript" src="{{ "dist/even.min.js?v=3.0.1" | relURL }}"></script>

Expand Down
19 changes: 19 additions & 0 deletions src/js/even.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,23 @@ Even.flowchart = function () {
}
}

Even.sequence = function () {
if (!window.Diagram) return

const blocks = document.querySelectorAll('pre code.language-sequence')
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i]
const rootElement = block.parentElement

const container = document.createElement('div')
const id = `js-sequence-diagrams-${i}`
container.id = id
container.className = 'align-center'
rootElement.parentElement.replaceChild(container, rootElement)

const diagram = Diagram.parse(block.childNodes[0].nodeValue)
diagram.drawSVG(id, window.sequenceDiagramsOptions ? window.sequenceDiagramsOptions : {theme: 'simple'})
}
}

export {Even}
1 change: 1 addition & 0 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ $(document).ready(function () {
})

Even.flowchart()
Even.sequence()

hljs.initHighlighting()
Even.highlight()
2 changes: 1 addition & 1 deletion static/dist/even.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5bcf92d

Please sign in to comment.