vsch / flexmark-java

CommonMark/Markdown Java parser with source level AST. CommonMark 0.28, emulation of: pegdown, kramdown, markdown.pl, MultiMarkdown. With HTML to MD, MD to PDF, MD to DOCX conversion modules.
BSD 2-Clause "Simplified" License
2.28k stars 270 forks source link

Image links using link references render incorrectly? #551

Open lread opened 1 year ago

lread commented 1 year ago

Describe the bug

First, thanks so much for flexmark-java! We've been using it for ages to render markdown for cljdoc.

One of our users reported that an .md doc he had authored was rendering image links oddly.

To Reproduce I thought maybe we might just not have the right options set for flexmark-java. And that might still be the case.

Here's a minimal reproduction of what we are seeing using flexmark-all v0.64.0:


import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.data.MutableDataSet;
import com.vladsch.flexmark.util.ast.Node;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        MutableDataSet options = new MutableDataSet();
        Parser parser = Parser.builder(options).build();
        HtmlRenderer renderer = HtmlRenderer.builder(options).build();
        Node document = parser.parse("""
                    [![alt text][img-url]][target-url]

                    [target-url]: http://www.example.com/index.html
                    [img-url]: https://picsum.photos/200
                    """);
        String html = renderer.render(document);
        System.out.println(html);
    }
}

Expected behavior I would have expected to see:

<p><a href="http://www.example.com/index.html"><img src="https://picsum.photos/200" alt="alt text" /></a></p>

This is how GitHub renders this (I pasted the example above here):

alt text

and also how the CommonMark demo/try-it feature does.

Resulting Output But I see instead:

<p>[<img src="https://picsum.photos/200" alt="alt text" />]<a href="http://www.example.com/index.html">target-url</a></p>

What do you think? Are we missing some config option/extension? Is the markdown technically valid?

Many thanks in advance for any help you can provide.

lread commented 1 year ago

Any tips on what flexmark-java extension points I would need to explore to add support for this syntax?

lread commented 1 year ago

I stumbled on babelmark3 and noticed that it shows flexmark-java is rendering as I would expect, but it is using flexmark-java version 0.50.44.

I'll retry my test above with this old version and report back.

lread commented 1 year ago

Ah interesting:

expected: <p><a href="http://www.example.com/index.html"><img src="https://picsum.photos/200" alt="alt text" /></a></p>

flex-mark v0.5.44 :heavy_check_mark: <p><a href="http://www.example.com/index.html"><img src="https://picsum.photos/200" alt="alt text" /></a></p>

flex-mark v0.64.0 :x: <p>[<img src="https://picsum.photos/200" alt="alt text" />]<a href="http://www.example.com/index.html">target-url</a></p>

I'll sample some other releases:

flexmark v0.50.50 :heavy_check_mark: (the last 0.50.x release) <p><a href="http://www.example.com/index.html"><img src="https://picsum.photos/200" alt="alt text" /></a></p>

flexmark v0.60.0 :heavy_check_mark: (the first 0.60.x release) <p><a href="http://www.example.com/index.html"><img src="https://picsum.photos/200" alt="alt text" /></a></p>

flexmark v0.60.2 :heavy_check_mark: (the last 0.60.x release) <p><a href="http://www.example.com/index.html"><img src="https://picsum.photos/200" alt="alt text" /></a></p>

flexmark v0.61.0 :heavy_check_mark: (the fist 0.61.x release) <p><a href="http://www.example.com/index.html"><img src="https://picsum.photos/200" alt="alt text" /></a></p>

flexmark v0.61.34 :x: (the last 0.61.x release) <p>[<img src="https://picsum.photos/200" alt="alt text" />]<a href="http://www.example.com/index.html">target-url</a></p>

Ok, it looks like the expected behaviour last occurred for:

flexmark v0.61.24 :heavy_check_mark: <p><a href="http://www.example.com/index.html"><img src="https://picsum.photos/200" alt="alt text" /></a></p>

And first changed to current behaviour for:

flexmark v0.61.26 :x: <p>[<img src="https://picsum.photos/200" alt="alt text" />]<a href="http://www.example.com/index.html">target-url</a></p>

lread commented 1 year ago

Relevant commit log: https://github.com/vsch/flexmark-java/compare/0.61.24...0.61.26

lread commented 1 year ago

@vsch I am happy to help in any way that makes sense to you here. When you find a moment, please let me know what you think.

lread commented 1 year ago

Oops me referencing this issue in a commit closed it, re-opening!

lread commented 1 year ago

@vsch I'm wondering if I should take a stab at a PR here, but before I invest that effort: