Your browser was unable to load all of the resources. They may have been blocked by your firewall, proxy or browser configuration.
Press Ctrl+F5 or Ctrl+Shift+R to have your browser try again.

pegdown markdownToHtml and LinkRenderer class replacement in flexmark java #1

#1

GitHub Issue: 105

Hi,
I am looking for the replacement for the following logic in flexmark java, can you please suggest?

PegDownProcessor pegDownProcessor = new PegDownProcessor(Extensions.ALL - (headerLinks ? 0 : Extensions.ANCHORLINKS)
- (hardwrap ? 0 : Extensions.HARDWRAPS) + (allowHtml ? 0 : Extensions.SUPPRESS_ALL_HTML));
try {
processed = pegDownProcessor.markdownToHtml(data, new ConfluenceWikiLinkRenderer(info, xhtmlContent));
} catch (Exception exception) {
exception.printStackTrace();
throw new MacroExecutionException("Conversion of Markdown markup failed: " + exception.toString());
}

And also for one of our other requirement we are extending LinkRenderer.java of pegdown for Confluence WikiLink Renderer implementation. Can we have a replacement for this one as well?

Currently we are getting compilation errors for Rendering, WikiLinkNodeclasses if we migrate from pegdown to flexmark java.

Please help us resolve these issue would be greatly appreciated. Let me know if anything is not clear.

Thanks
RK

  • replies 2
  • views 4.4K
  • likes 0
#2
static final MutableDataHolder OPTIONS = PegdownOptionsAdapter.flexmarkOptions(
            Extensions.ALL - (headerLinks ? 0 : Extensions.ANCHORLINKS)
                    - (hardwrap ? 0 : Extensions.HARDWRAPS) + (allowHtml ? 0 : Extensions.SUPPRESS_ALL_HTML)
            // add your extra extensions here
            //, new ConfluenceWikiLinkExtension()
    ).toMutable()
            // set additional options here:
            //.set(HtmlRenderer.FENCED_CODE_LANGUAGE_CLASS_PREFIX,"")
            ;

    static final Parser PARSER = Parser.builder(OPTIONS).build();
    static final HtmlRenderer RENDERER = HtmlRenderer.builder(OPTIONS).build();
    // use the PARSER to parse and RENDERER to render with pegdown compatibility
    static {
        Node document = PARSER.parse(data);
        processed = RENDERER.render(document);
    }

What does your ConfluenceWikiLinkRenderer do that is different from GitHub Wiki links?

You can use the code above which will have wiki links enabled and will convert [[wiki page]] to references.

To have custom link resolving logic added you need a custom link resolver. For example see: PegdownCustomLinkResolverOptions

An overview of parsing and rendering phases with available extension points can be found in the wiki: https://github.com/vsch/flexmark-java/wiki/Writing-Extensions

#3

You can pass any information you want through options DataHolder instance used to create Parser and HtmlRenderer.

If you re-use the parser and html renderer instances and need to change options for each invokation of html renderer, you can pass it using the Document node, which is a MutableDataHolder.

Define your own DataKey<T>:

public static final DataKey<XhtmlContent> XHTML_CONTENT = new DataKey<XhtmlContent>("XHTML_CONTENT", (XhtmlContent)null);

Then pass it in the Document node before rendering the document:

Document document = (Document) parser.parse(markdown);
document.set(XHTML_CONTENT, xhtmlContent);
String html = renderer.render(document);

In your link resolver or anywhere in the rendering process you can get this data through the document node:

public ResolvedLink resolveLink(final Node node, final NodeRendererContext context, final ResolvedLink link) {
    Document document = context.getDocument();
    XhtmlContent xhtmlContent = document.get(XhtmlContent);
    ...;
}

A sample source of this is available at CustomContextDataSample