Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Fixes #13.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Oct 12, 2023
1 parent 8600341 commit 2d48d26
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ targetCompatibility = 11

description = 'Utilities to import diagrams and documentation into a Structurizr workspace'
group = 'com.structurizr'
version = '1.5.0'
version = '1.5.1'

test {
useJUnitPlatform()
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.5.1 (unreleased)

- Fixes https://github.com/structurizr/import/issues/13 (Mermaid diagrams not working).

## 1.5.0 (25th July 2023)

- Updated dependencies and minimum Java version (11).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
*/
public class MermaidEncoder {

private static final String TEMPLATE = "{ \"code\":\"%s\", \"mermaid\":{\"theme\":\"default\"}}";

public String encode(String mermaidDefinition) {
String s = String.format(TEMPLATE, mermaidDefinition.replaceAll("\n", "\\\\n").replaceAll("\"", "\\\\\""));
return Base64.getEncoder().encodeToString(s.getBytes(StandardCharsets.UTF_8));
return Base64.getUrlEncoder().encodeToString(mermaidDefinition.getBytes(StandardCharsets.UTF_8));
}

}
6 changes: 5 additions & 1 deletion src/test/diagrams/mermaid/flowchart.mmd
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
flowchart TD
Start --> Stop
A[Christmas] -->|Get money| B(Go shopping)
B --> C{Let me think}
C -->|One| D[Laptop]
C -->|Two| E[iPhone]
C -->|Three| F[fa:fa-car Car]
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public class MermaidEncoderTests {
@Test
public void encode_flowchart() throws Exception {
File file = new File("./src/test/diagrams/mermaid/flowchart.mmd");
String mermaid = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
assertEquals("eyAiY29kZSI6ImZsb3djaGFydCBURFxuICAgIFN0YXJ0IC0tPiBTdG9wIiwgIm1lcm1haWQiOnsidGhlbWUiOiJkZWZhdWx0In19", new MermaidEncoder().encode(mermaid));
String mermaid = Files.readString(file.toPath());
assertEquals("Zmxvd2NoYXJ0IFRECiAgICBBW0NocmlzdG1hc10gLS0-fEdldCBtb25leXwgQihHbyBzaG9wcGluZykKICAgIEIgLS0-IEN7TGV0IG1lIHRoaW5rfQogICAgQyAtLT58T25lfCBEW0xhcHRvcF0KICAgIEMgLS0-fFR3b3wgRVtpUGhvbmVdCiAgICBDIC0tPnxUaHJlZXwgRltmYTpmYS1jYXIgQ2FyXQ==", new MermaidEncoder().encode(mermaid));
}

@Test
public void encode_class() throws Exception {
File file = new File("./src/test/diagrams/mermaid/class.mmd");
String mermaid = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
assertEquals("eyAiY29kZSI6ImNsYXNzRGlhZ3JhbVxuICAgIEFuaW1hbCA8fC0tIER1Y2tcbiAgICBBbmltYWwgPHwtLSBGaXNoXG4gICAgQW5pbWFsIDx8LS0gWmVicmFcbiAgICBBbmltYWwgOiAraW50IGFnZVxuICAgIEFuaW1hbCA6ICtTdHJpbmcgZ2VuZGVyXG4gICAgQW5pbWFsOiAraXNNYW1tYWwoKVxuICAgIEFuaW1hbDogK21hdGUoKVxuICAgIGNsYXNzIER1Y2t7XG4gICAgICArU3RyaW5nIGJlYWtDb2xvclxuICAgICAgK3N3aW0oKVxuICAgICAgK3F1YWNrKClcbiAgICB9XG4gICAgY2xhc3MgRmlzaHtcbiAgICAgIC1pbnQgc2l6ZUluRmVldFxuICAgICAgLWNhbkVhdCgpXG4gICAgfVxuICAgIGNsYXNzIFplYnJhe1xuICAgICAgK2Jvb2wgaXNfd2lsZFxuICAgICAgK3J1bigpXG4gICAgfVxuIiwgIm1lcm1haWQiOnsidGhlbWUiOiJkZWZhdWx0In19", new MermaidEncoder().encode(mermaid));
String mermaid = Files.readString(file.toPath());
assertEquals("Y2xhc3NEaWFncmFtCiAgICBBbmltYWwgPHwtLSBEdWNrCiAgICBBbmltYWwgPHwtLSBGaXNoCiAgICBBbmltYWwgPHwtLSBaZWJyYQogICAgQW5pbWFsIDogK2ludCBhZ2UKICAgIEFuaW1hbCA6ICtTdHJpbmcgZ2VuZGVyCiAgICBBbmltYWw6ICtpc01hbW1hbCgpCiAgICBBbmltYWw6ICttYXRlKCkKICAgIGNsYXNzIER1Y2t7CiAgICAgICtTdHJpbmcgYmVha0NvbG9yCiAgICAgICtzd2ltKCkKICAgICAgK3F1YWNrKCkKICAgIH0KICAgIGNsYXNzIEZpc2h7CiAgICAgIC1pbnQgc2l6ZUluRmVldAogICAgICAtY2FuRWF0KCkKICAgIH0KICAgIGNsYXNzIFplYnJhewogICAgICArYm9vbCBpc193aWxkCiAgICAgICtydW4oKQogICAgfQo=", new MermaidEncoder().encode(mermaid));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void importDiagram() throws Exception {
assertNull(view.getElement());
assertNull(view.getElementId());
assertEquals("flowchart.mmd", view.getTitle());
assertEquals("https://mermaid.ink/img/eyAiY29kZSI6ImZsb3djaGFydCBURFxuICAgIFN0YXJ0IC0tPiBTdG9wIiwgIm1lcm1haWQiOnsidGhlbWUiOiJkZWZhdWx0In19?type=png", view.getContent());
assertEquals("https://mermaid.ink/img/Zmxvd2NoYXJ0IFRECiAgICBBW0NocmlzdG1hc10gLS0-fEdldCBtb25leXwgQihHbyBzaG9wcGluZykKICAgIEIgLS0-IEN7TGV0IG1lIHRoaW5rfQogICAgQyAtLT58T25lfCBEW0xhcHRvcF0KICAgIEMgLS0-fFR3b3wgRVtpUGhvbmVdCiAgICBDIC0tPnxUaHJlZXwgRltmYTpmYS1jYXIgQ2FyXQ==?type=png", view.getContent());
assertEquals("image/png", view.getContentType());
}

Expand All @@ -37,7 +37,7 @@ public void importDiagram_AsPNG() throws Exception {
assertNull(view.getElement());
assertNull(view.getElementId());
assertEquals("flowchart.mmd", view.getTitle());
assertEquals("https://mermaid.ink/img/eyAiY29kZSI6ImZsb3djaGFydCBURFxuICAgIFN0YXJ0IC0tPiBTdG9wIiwgIm1lcm1haWQiOnsidGhlbWUiOiJkZWZhdWx0In19?type=png", view.getContent());
assertEquals("https://mermaid.ink/img/Zmxvd2NoYXJ0IFRECiAgICBBW0NocmlzdG1hc10gLS0-fEdldCBtb25leXwgQihHbyBzaG9wcGluZykKICAgIEIgLS0-IEN7TGV0IG1lIHRoaW5rfQogICAgQyAtLT58T25lfCBEW0xhcHRvcF0KICAgIEMgLS0-fFR3b3wgRVtpUGhvbmVdCiAgICBDIC0tPnxUaHJlZXwgRltmYTpmYS1jYXIgQ2FyXQ==?type=png", view.getContent());
assertEquals("image/png", view.getContentType());
}

Expand All @@ -53,7 +53,7 @@ public void importDiagram_AsSVG() throws Exception {
assertNull(view.getElement());
assertNull(view.getElementId());
assertEquals("flowchart.mmd", view.getTitle());
assertEquals("https://mermaid.ink/svg/eyAiY29kZSI6ImZsb3djaGFydCBURFxuICAgIFN0YXJ0IC0tPiBTdG9wIiwgIm1lcm1haWQiOnsidGhlbWUiOiJkZWZhdWx0In19", view.getContent());
assertEquals("https://mermaid.ink/svg/Zmxvd2NoYXJ0IFRECiAgICBBW0NocmlzdG1hc10gLS0-fEdldCBtb25leXwgQihHbyBzaG9wcGluZykKICAgIEIgLS0-IEN7TGV0IG1lIHRoaW5rfQogICAgQyAtLT58T25lfCBEW0xhcHRvcF0KICAgIEMgLS0-fFR3b3wgRVtpUGhvbmVdCiAgICBDIC0tPnxUaHJlZXwgRltmYTpmYS1jYXIgQ2FyXQ==", view.getContent());
assertEquals("image/svg+xml", view.getContentType());
}

Expand Down

0 comments on commit 2d48d26

Please sign in to comment.