py-d2 icon indicating copy to clipboard operation
py-d2 copied to clipboard

Labels aren't escaped and/or quoted when necessary

Open smheidrich opened this issue 1 year ago • 0 comments

Description

Problem description

Consider e.g. a label with a newline in it:

from py_d2 import D2Diagram, D2Shape

shape = D2Shape(name="SomeShape", label="Some\nlabel")
diagram = D2Diagram(shapes=[shape])
print(str(diagram))

py-d2 renders this as

SomeShape: Some
label

which D2, in turn, renders as a node with label Some:

But surely, what the user wanted here was to have the newline escaped

SomeShape: Some\nlabel

so that D2 renders it as a node with a multi-line label:

The same issue applies to other characters interpreted specially by D2, specifically \, {, }, " and ' (there might be others, not sure).

Would fixing this constitute a breaking change?

One problem I foresee is that other users of this library might have started to work around this by quoting/escaping the labels they give to py-d2 themselves, so once this gets fixed, their \ characters are escaped to \\, " are escaped to \" etc., breaking their code.

I don't know if there is a good way around this. One option would be to leave label as it is and introduce a new parameter smart_label or whatever, but that makes things needlessly complicated for new users.

So maybe it should just get fixed in the normal, potentially-breaking manner but be held back until a new major version release?

Related issues / PRs

  • https://github.com/MrBlenny/py-d2/pull/19 attempts to fix empty labels, which are broken due to the same underlying issue of rendering labels verbatim

smheidrich avatar Sep 08 '24 15:09 smheidrich