Skip to content

Commit

Permalink
feat: adding support for Markdown / latex / block strings / code in s…
Browse files Browse the repository at this point in the history
…hapes and icons
  • Loading branch information
Kreshnik committed Jun 20, 2023
1 parent ccd3ffb commit 72d4c00
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ alphabet -> google
- [x] Containers (nodes/links in nodes)
- [x] Shapes in shapes
- [x] Arrow directions
- [ ] Markdown / block strings / code in shapes
- [ ] Icons in shapes
- [x] Markdown / latex / block strings / code in shapes
- [x] Icons in shapes
- [ ] SQL table shapes
- [ ] Class shapes
- [ ] Comments
Expand Down
4 changes: 3 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.D2Connection = exports.D2Shape = exports.D2Diagram = exports.Direction = exports.Shape = void 0;
exports.D2Connection = exports.D2Text = exports.D2Shape = exports.D2Diagram = exports.Direction = exports.Shape = void 0;
const Shape_1 = __importDefault(require("./src/Enum/Shape"));
exports.Shape = Shape_1.default;
const Direction_1 = __importDefault(require("./src/Enum/Direction"));
Expand All @@ -12,5 +12,7 @@ const D2Diagram_1 = __importDefault(require("./src/D2Diagram"));
exports.D2Diagram = D2Diagram_1.default;
const D2Shape_1 = __importDefault(require("./src/D2Shape"));
exports.D2Shape = D2Shape_1.default;
const D2Text_1 = __importDefault(require("./src/D2Text"));
exports.D2Text = D2Text_1.default;
const D2Connection_1 = __importDefault(require("./src/D2Connection"));
exports.D2Connection = D2Connection_1.default;
18 changes: 16 additions & 2 deletions dist/src/D2Shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class D2Shape {
this.connections = [];
this.style = style;
this.near = near;
this.texts = [];
this.icon = "";
}
addShape(shape) {
// @ts-ignore
Expand All @@ -19,12 +21,25 @@ class D2Shape {
// @ts-ignore
this.connections.push(connection);
}
setIcon(icon) {
this.icon = icon;
}
addText(text) {
// @ts-ignore
this.texts.push(text);
}
lines() {
// @ts-ignore
let shapes = (0, helpers_1.flatten)(this.shapes.map(shape => shape.lines()));
// @ts-ignore
let connections = (0, helpers_1.flatten)(this.connections.map(connection => connection.lines()));
// @ts-ignore
let texts = (0, helpers_1.flatten)(this.texts.map(text => text.lines()));
let properties = shapes.concat(connections);
properties = texts.concat(properties);
if (this.icon !== "") {
properties.push(`icon: ${this.icon}`);
}
if (this.shape) {
properties.push(`shape: ${this.shape}`);
}
Expand All @@ -34,8 +49,7 @@ class D2Shape {
if (this.style) {
properties.push(...this.style.lines());
}
let lines = (0, helpers_1.addLabelAndProperties)(this.name, this.label, properties);
return lines;
return (0, helpers_1.addLabelAndProperties)(this.name, this.label, properties);
}
toString() {
return this.lines().join("\n");
Expand Down
5 changes: 3 additions & 2 deletions dist/src/D2Text.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class D2Text {
constructor(text, format, pipes = 1) {
constructor(property, text, format, pipes = 1) {
this.property = property;
this.text = text;
this.format = format;
this.pipes = pipes;
}
lines() {
let sep = "|".repeat(this.pipes);
return [`${sep}${this.format}`, ...this.text.split("\n"), sep];
return [`${this.property}:${sep}${this.format}`, ...this.text.split("\n"), sep];
}
toString() {
return this.lines().join("\n");
Expand Down
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Direction from "./src/Enum/Direction";

import D2Diagram from "./src/D2Diagram";
import D2Shape from "./src/D2Shape";
import D2Text from "./src/D2Text";
import D2Connection from "./src/D2Connection";

export {Shape, Direction, D2Diagram, D2Shape, D2Connection};
export {Shape, Direction, D2Diagram, D2Shape, D2Text, D2Connection};
36 changes: 36 additions & 0 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "d2lang-js",
"version": "0.1.0",
"version": "0.2.0",
"description": "An unofficial interface for building D2 diagram files in javascript",
"keywords": [
"d2lang",
Expand All @@ -22,7 +22,7 @@
},
"main": "./dist/index.js",
"devDependencies": {
"@types/node": "^18.11.18",
"typescript": "^4.9.4"
"@types/node": "^20.3.1",
"typescript": "^5.1.3"
}
}
30 changes: 26 additions & 4 deletions src/D2Shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ import Shape from "./Enum/Shape";
import D2Connection from "./D2Connection";
import D2Style from "./D2Style";
import {addLabelAndProperties, flatten} from "./helpers";
import D2Text from "./D2Text";

class D2Shape {
name: string;
label?: string;
shape?: Shape;
icon?: string;
shapes?: D2Shape[];
connections?: D2Connection[];
style?: D2Style;
near?: string;
texts?: D2Text[];

constructor(
name: string,
label?: string,
shape?: Shape,
style?: D2Style,
near?: string,
near?: string
) {
this.name = name;
this.label = label;
Expand All @@ -25,6 +29,8 @@ class D2Shape {
this.connections = [];
this.style = style;
this.near = near;
this.texts = [];
this.icon = "";
}
addShape(shape: D2Shape): void {
// @ts-ignore
Expand All @@ -36,14 +42,32 @@ class D2Shape {
this.connections.push(connection);
}

setIcon(icon: string): void {
this.icon = icon;
}

addText(text: D2Text): void {
// @ts-ignore
this.texts.push(text);
}

lines(): string[] {
// @ts-ignore
let shapes = flatten(this.shapes.map(shape => shape.lines()));
// @ts-ignore
let connections = flatten(this.connections.map(connection => connection.lines()));

// @ts-ignore
let texts = flatten(this.texts.map(text => text.lines()));

let properties = shapes.concat(connections);

properties = texts.concat(properties);

if (this.icon !== "") {
properties.push(`icon: ${this.icon}`);
}

if (this.shape) {
properties.push(`shape: ${this.shape}`);
}
Expand All @@ -56,9 +80,7 @@ class D2Shape {
properties.push(...this.style.lines());
}

let lines = addLabelAndProperties(this.name, this.label, properties);

return lines;
return addLabelAndProperties(this.name, this.label, properties);
}
toString(): string {
return this.lines().join("\n");
Expand Down
6 changes: 4 additions & 2 deletions src/D2Text.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
class D2Text {
property: string;
text: string;
format: string;
pipes: number;

constructor(text: string, format: string, pipes: number = 1) {
constructor(property: string, text: string, format: string, pipes: number = 1) {
this.property = property;
this.text = text;
this.format = format;
this.pipes = pipes;
}

lines(): string[] {
let sep = "|".repeat(this.pipes);
return [`${sep}${this.format}`, ...this.text.split("\n"), sep];
return [`${this.property}:${sep}${this.format}`, ...this.text.split("\n"), sep];
}

toString(): string {
Expand Down

0 comments on commit 72d4c00

Please sign in to comment.