commonmark / commonmark-java

Java library for parsing and rendering CommonMark (Markdown)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue rendering link correctly with Liquid markup

jvanzyl opened this issue · comments

I have a Jekyll implementation in Java, which in turn uses a Liquid implementation Java. Normally Markdown is rendered first and then that output is processed through Liquid. It may very well be that this is not valid for commonmark as commonmark-java and Dingus both do not turn the example below into a link.

If this is the case is it possible to create a custom parser for links to handle this?

Steps to reproduce the problem (provide example Markdown if applicable):

[The first post]({{site.url}}{% post_url 2021-05-03-blog-post %})

Expected behavior:

<p><a href="{{site.url}}{% post_url 2021-05-03-blog-post %}">The first post</a></p>

Actual behavior:

<p>[The first post]({{site.url}}{% post_url 2021-05-03-blog-post %})</p>

(Also see what the reference implementation does: https://spec.commonmark.org/dingus/)

Dingus produces the same result.

It appears that I am wrong and the liquid expressions should be processed first as stated here: https://jekyllrb.com/docs/rendering-process/

At least for Jekyll, Liquid expressions shouldn't be present in Marked parsed content. So I'm not sure if this is really a problem for me or if it's a valid for commonmark, but I'll leave this here to see if there are any comments.

Yeah the problem is that the link destination (URL) is not allowed to contain spaces with that syntax, see spec:

a nonempty sequence of characters that does not start with <, does not include ASCII control characters or space character, and includes parentheses only if (a) they are backslash-escaped or (b) they are part of a balanced pair of unescaped parentheses. (Implementations may impose limits on parentheses nesting to avoid performance issues, but at least three levels of nesting should be supported.)

That's why the whole thing is not parsed as a link.

You can allow spaces by enclosing the URL in < > like this:

[The first post](<{{site.url}}{% post_url 2021-05-03-blog-post %}>)

Does that help?

If this is the case is it possible to create a custom parser for links to handle this?

That's not easy, link parsing is one of the most complex parts of commonmark.

I'll close this out as I need to process the liquid first but thanks for the help!