jgm / djot.js

JavaScript implementation of djot
MIT License
141 stars 15 forks source link

Can we remove auto generated anchor in renderDjot output? #69

Closed black-desk closed 6 months ago

black-desk commented 6 months ago

I am working on a lsp server for djot, and I want to use renderDjot as a formator. Keep these anchors is werid.

jgm commented 6 months ago

That would be a good idea.

By the way, you might be interested in my https://github.com/jgm/djoths

It has a djot renderer that passes round-trip tests for all the test cases, and it omits the autogenerated ids.

black-desk commented 6 months ago

I will make a PR later.

black-desk commented 6 months ago

To be used as a formatter, I think djot.js should both keep these three documents unchanged.

# a
{#a}
# a
# a

[a]: #a

So, we cannot just simply compare the attributes and reference to check if it should be omitted or not.

I think we might add an autoAttributes member to HasAttributes interface and an autoReference member to Doc, to omit auto generated attributes and references.

But we have to break API in this way. Do you have a better idea?

jgm commented 6 months ago

I think it is probably okay for the interface to change this way. @matklad do you have any worries about this?

Alternatively, you could do what I did in my Haskell parser/renderer: the parser adds a special attribute _autogen=1 to track these. I adjusted the HTML and Djot renderers so they'd be sensitive to this attribute (and wouldn't print it as a regular attribute). This is more conservative, in that it doesn't alter the interface, but maybe a bit uglier.

black-desk commented 6 months ago

I think it is probably okay for the interface to change this way. @matklad do you have any worries about this?

Alternatively, you could do what I did in my Haskell parser/renderer: the parser adds a special attribute _autogen=1 to track these. I adjusted the HTML and Djot renderers so they'd be sensitive to this attribute (and wouldn't print it as a regular attribute). This is more conservative, in that it doesn't alter the interface, but maybe a bit uglier.

Consider this:

{.b}
# a

add _autogen=1 seems not working here.

jgm commented 6 months ago

add _autogen=1 seems not working here.

If you're trying my Haskell djot renderer, then note that I've changed to using a different approach. Now I just try to figure out if the id is the one that would have been autogenerated.