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

d2js: support unicode characters #2393

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
- fixes panic when comment lines appear in arrays [#2378](https://github.com/terrastruct/d2/pull/2378)
- fixes inconsistencies when objects were double quoted [#2390](https://github.com/terrastruct/d2/pull/2390)
- CLI: fetch and render remote images of mimetype octet-stream correctly [#2370](https://github.com/terrastruct/d2/pull/2370)
- d2js: handle unicode characters [#2393](https://github.com/terrastruct/d2/pull/2393)
5 changes: 4 additions & 1 deletion d2js/js/src/worker.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export function setupMessageHandler(isNode, port, initWasm) {
const result = await d2.render(JSON.stringify(data));
const response = JSON.parse(result);
if (response.error) throw new Error(response.error.message);
currentPort.postMessage({ type: "result", data: atob(response.data) });
const decoded = new TextDecoder().decode(
Uint8Array.from(atob(response.data), (c) => c.charCodeAt(0))
);
currentPort.postMessage({ type: "result", data: decoded });
} catch (err) {
currentPort.postMessage({ type: "error", error: err.message });
}
Expand Down
5 changes: 4 additions & 1 deletion d2js/js/src/worker.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export function setupMessageHandler(isNode, port, initWasm) {
const result = await d2.render(JSON.stringify(data));
const response = JSON.parse(result);
if (response.error) throw new Error(response.error.message);
currentPort.postMessage({ type: "result", data: atob(response.data) });
const decoded = new TextDecoder().decode(
Uint8Array.from(atob(response.data), (c) => c.charCodeAt(0))
);
currentPort.postMessage({ type: "result", data: decoded });
} catch (err) {
currentPort.postMessage({ type: "error", error: err.message });
}
Expand Down
11 changes: 11 additions & 0 deletions d2js/js/test/unit/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ layers: {
await d2.worker.terminate();
}, 20000);

test("unicode characters work", async () => {
const d2 = new D2();
const result = await d2.compile("こんにちは -> ♒️");
const svg = await d2.render(result.diagram);
expect(svg).toContain("<svg");
expect(svg).toContain("</svg>");
expect(svg).toContain("こんにちは");
expect(svg).toContain("♒️");
await d2.worker.terminate();
}, 20000);

test("handles syntax errors correctly", async () => {
const d2 = new D2();
try {
Expand Down