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.29k stars 272 forks source link

WikiLinkNodeFormatter duplicates pageRef for links w/o text but with anchor #524

Open protogenes opened 2 years ago

protogenes commented 2 years ago

When formatting a WikiLink or WikiImage the link is modified:

[[my Page#anchor]]

turns into

[[my Pagemy Page#anchor]]

WikiLinks with explicit text or without anchor are fine.

To Reproduce

String md = "[[my Page#anchor]]";
Parser.Builder parser = new Parser.Builder();
Parser.addExtensions(parser, WikiLinkExtension.create())
      .set(WikiLinkExtension.ALLOW_ANCHORS, true);
Document doc = parser.build().parse(md);
String newmd = new Formatter.Builder(parser).build().render(doc);
Assert.assertEquals(md, newmd);

References:

  1. WikiNode uses the pageRef as the node's text during parsing
  2. WikiLinkNodeFormatter only checks for missing text for simple formatting
  3. but it also falls back to the pageRef if no text is present
  4. and for WikiLinkNodeRenderer there is the same fallback but as comment

Either point 1+4 or 2 need to change. I would prefer a null text, when there is in fact no explicit text input (thus removing point 1 from WikiNode).