jgm / djot

A light markup language
https://djot.net
MIT License
1.67k stars 43 forks source link

Consider adding "AST in JSON" output fromat #58

Closed matklad closed 1 year ago

matklad commented 1 year ago

Problem: I'd love to experiment with djot, but I don't know lua, and would rather use a language I am already familiar with (Rust or TypeScript).

Proposed solution: add djot -a -j to output AST in some JSON format which then could be easily consumed by other programs.

I think I probably can get something like this via pandoc, but I'd rather avoid adding one more tool to the pipeline.

Yeah, the appropriate pandoc spell is pandoc -f djot-reader.lua -t json example.djot. There's another drawback with that approach: today's pandoc json output is a rather low-level encoding of Haskell data structures, it's not something you can just JSON.parse in javascript and get a natural API. I think there's some benefit to defining a more first-class JSON AST encoding for djot.

jgm commented 1 year ago

That's a good idea, and I don't think this would be too difficult. However, I'm reluctant to make this library depend on a json library -- right now it has no dependencies. One option would be an optional module that consumes the output of to_ast and produces json. Or maybe I can just write the necessary json-generating code without using a library.

matklad commented 1 year ago

Yeah, I don't think a library is required to emit json, we can just push stuff to string, the same what we do for HTML (I think?)

Very much appreciate the zero-dependency nature of the reference impl!

matklad commented 1 year ago

I think this is mostly done now, via -a -j. Most of discussion here happened in #60 :)

jgm commented 1 year ago

I've added this to the live sandbox, too