flexmark-java icon indicating copy to clipboard operation
flexmark-java copied to clipboard

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

Open protogenes opened this issue 2 years ago • 0 comments

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

  • [x] Parser
  • [ ] HtmlRenderer
  • [x] Formatter
  • [ ] FlexmarkHtmlParser
  • [ ] DocxRenderer
  • [ ] PdfConverterExtension
  • [x] ext-wikilink
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).

protogenes avatar Sep 01 '22 12:09 protogenes