Last updated Apr 2, 2024

How do I convert wiki text to HTML?

To convert wiki text to HTML, you will need the XhtmlContent (see how to retrieve it).

The XhtmlContent has convertWikiToView() method. See the following examples on how to use it.

Convert wiki text in a macro

To convert wiki text to HTML in a macro, do the following:

1
2
public String execute(Map<String, String> map, String s, ConversionContext conversionContext) throws MacroExecutionException {
    String wiki = "## Some wiki";
    String xhtml = "";
    try {
        xhtml = xhtmlContent.convertWikiToView(wiki, conversionContext, new ArrayList<>());
    } catch (XMLStreamException | XhtmlException e) {
        e.printStackTrace();
    }
    return xhtml;
}

Convert wiki text in a different component

If you don't have ConversionContext, you can pass DefaultConversionContext, which takes RenderContext as an argument.

If you are converting the text in the context of some ContentEntityObject (for example, within a page or blog post), then you can call contentEntityObject.toPageContext() to retrieve its RenderContext. Otherwise, pass in a new PageContext(). For example:

1
2
public String someMethod() {
    String wiki = "## Some wiki";
    String xhtml = "";
    try {
        xhtml = xhtmlContent.convertWikiToView(wiki, new DefaultConversionContext(new PageContext()), new ArrayList<>());
    } catch (XMLStreamException | XhtmlException e) {
        e.printStackTrace();
    }
    return xhtml;
}

Rate this page: