Skip to content

Commit

Permalink
feat: add shape links support
Browse files Browse the repository at this point in the history
  • Loading branch information
h0rv committed Oct 30, 2024
1 parent 41754e6 commit 4f661a4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ See the [tests](/tests/test_py_d2) for more detailed usage examples.
- [x] Markdown / block strings / code in shapes
- [x] Icons in shapes
- [x] Support for empty labels
- [x] Shape links
- [ ] SQL table shapes
- [ ] Class shapes
- [ ] Comments
- [ ] Layers


## Development
Expand Down
6 changes: 6 additions & 0 deletions src/py_d2/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def __init__(
connections: Optional[List[D2Connection]] = None,
# A shape this is near
near: Optional[str] = None,
# A link for a shape (when clicked)
link: Optional[str] = None,
**kwargs: D2Text,
):
self.name = name
Expand All @@ -88,6 +90,7 @@ def __init__(
self.icon = icon
self.connections = connections or []
self.near = near
self.link = link
self.kwargs = kwargs

def add_shape(self, shape: D2Shape):
Expand All @@ -107,6 +110,9 @@ def lines(self) -> List[str]:
if self.near:
properties.append(f"near: {self.near}")

if self.link:
properties.append(f"link: {self.link}")

if self.style:
properties += self.style.lines()

Expand Down
5 changes: 5 additions & 0 deletions tests/test_py_d2/test_d2_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ def test_d2_shape_near():
assert str(shape) == "shape_name: {\n near: some_other_shape\n}"


def test_d2_shape_link():
shape = D2Shape(name="shape_name", link="https://github.com/MrBlenny/py-d2")
assert str(shape) == "shape_name: {\n link: https://github.com/MrBlenny/py-d2\n}"


def test_d2_shape_other_properties():
text = "Some text"
shape = D2Shape(name="shape_name", thing=D2Text(text=text, formatting="md"))
Expand Down

0 comments on commit 4f661a4

Please sign in to comment.