wooorm / markdown-rs

CommonMark compliant markdown parser in Rust with ASTs and extensions
https://docs.rs/markdown/1.0.0-alpha.18/markdown/
MIT License
836 stars 41 forks source link

Math flow parsed inline in the syntax tree #68

Closed rambip closed 11 months ago

rambip commented 1 year ago

Here is a small reproducible example:

use markdown::{to_mdast, ParseOptions, Constructs};

fn main() {
    let options = ParseOptions {
              constructs: Constructs {
                math_text: true,
                ..Constructs::default()
              },
              ..ParseOptions::default()
            };
    let ast = to_mdast("$$math$$", &options).unwrap();
    println!("{:?}", ast);
}

I get

Root { children: [Paragraph { children: [InlineMath { value: "math", position: Some(1:1-1:9 (0-8)) }], position: Some(1:1-1:9 (0-8)) }], position: Some(1:1-1:9 (0-8)) }

This was not what I was expecting

After a few tests, It seems that math is parsed "Inline" when there is no newline between the $$ and the content, but I think it is almost never the expected behavior: a math equation between $$ on a single line should not be rendered "inline"

Does it add complexity to the implementation or was it a deliberate decision ?

wooorm commented 1 year ago

This is intended, code works the same way. ``code``

rambip commented 1 year ago

ok

I just tried it on stackedit, and $$x$$ is not displayed inline. On ovearleaf (the online latex editor) same thing And even with github: $$x$$ see ?

Do you consider changing (or a least adding an option to change) that behavior ?

wooorm commented 1 year ago

No. There is no spec for math in markdown, so everyone is going to do their own thing. It's best IMO, if there is no spec, to stick with how the spec works in similar cases

rambip commented 1 year ago

ok I somewhat agree So I guess I will preprocess my markdown to add line breaks whenever there is $$

rambip commented 1 year ago

Ho no, I just realized that I needed the Positons on the syntax tree relative to the original source, so this solution does not work at all for me :disappointed:

ChristianMurphy commented 11 months ago

@rambip similar to https://github.com/wooorm/markdown-rs/issues/62, I don't see this as something that would be a part of core markdown-rs. But, a plugin could look at the positions of content, and convert inline math to math blocks based on your desired (though non-standard) rules. (depends on https://github.com/wooorm/markdown-rs/issues/32) This could be done directly from rust with https://github.com/wooorm/markdown-rs/issues/32 Or in a more round about way working on the mdast tree (already supported in the API https://github.com/wooorm/markdown-rs#api)

Efforts towards supporting a plugin API, or enhancing the APIs around mdast handling like https://github.com/wooorm/markdown-rs/issues/64 are welcome!