Search in sources :

Example 1 with AttributeProviderContext

use of org.commonmark.renderer.html.AttributeProviderContext in project odysee-android by OdyseeTeam.

the class FileViewFragment method buildMarkdownHtml.

private String buildMarkdownHtml(String markdown) {
    Parser parser = Parser.builder().build();
    Node document = parser.parse(markdown);
    HtmlRenderer renderer = HtmlRenderer.builder().attributeProviderFactory(new AttributeProviderFactory() {

        @Override
        public AttributeProvider create(AttributeProviderContext context) {
            return new CodeAttributeProvider();
        }
    }).build();
    String markdownHtml = renderer.render(document);
    return "<!doctype html>\n" + "        <html>\n" + "          <head>\n" + "            <meta charset=\"utf-8\"/>\n" + "            <meta name=\"viewport\" content=\"width=device-width, user-scalable=no\"/>\n" + "            <style type=\"text/css\">\n" + "              body { font-family: 'Inter', sans-serif; margin: 16px }\n" + "              img { width: 100%; }\n" + "              pre { white-space: pre-wrap; word-wrap: break-word }\n" + "            </style>\n" + "          </head>\n" + "          <body>\n" + "            <div id=\"content\">\n" + markdownHtml + "            </div>\n" + "          </body>\n" + "        </html>";
}
Also used : AttributeProviderFactory(org.commonmark.renderer.html.AttributeProviderFactory) Node(org.commonmark.node.Node) AttributeProviderContext(org.commonmark.renderer.html.AttributeProviderContext) HtmlRenderer(org.commonmark.renderer.html.HtmlRenderer) Parser(org.commonmark.parser.Parser)

Example 2 with AttributeProviderContext

use of org.commonmark.renderer.html.AttributeProviderContext in project zserio by ndsev.

the class DocMarkdownToHtmlConverter method convert.

static String convert(DocResourceManager docResourceManager, AstLocation location, String markdown) {
    final List<Extension> extensions = Arrays.asList(AutolinkExtension.create(), HeadingAnchorExtension.create(), TablesExtension.create());
    final Parser parser = Parser.builder().extensions(extensions).build();
    final Node document = parser.parse(markdown);
    final HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).nodeRendererFactory(new HtmlNodeRendererFactory() {

        @Override
        public NodeRenderer create(HtmlNodeRendererContext context) {
            return new ResourcesRenderer(context, docResourceManager, location);
        }
    }).attributeProviderFactory(new AttributeProviderFactory() {

        @Override
        public AttributeProvider create(AttributeProviderContext arg0) {
            return new AnchorAttributeProvider();
        }
    }).attributeProviderFactory(new AttributeProviderFactory() {

        @Override
        public AttributeProvider create(AttributeProviderContext arg0) {
            return new ImageAttributeProvider();
        }
    }).build();
    final String html = renderer.render(document);
    // strip white-spaces around generated HTML added by renderer
    return html.trim();
}
Also used : Node(org.commonmark.node.Node) HtmlNodeRendererFactory(org.commonmark.renderer.html.HtmlNodeRendererFactory) Parser(org.commonmark.parser.Parser) HeadingAnchorExtension(org.commonmark.ext.heading.anchor.HeadingAnchorExtension) Extension(org.commonmark.Extension) AutolinkExtension(org.commonmark.ext.autolink.AutolinkExtension) TablesExtension(org.commonmark.ext.gfm.tables.TablesExtension) AttributeProviderFactory(org.commonmark.renderer.html.AttributeProviderFactory) AttributeProviderContext(org.commonmark.renderer.html.AttributeProviderContext) HtmlRenderer(org.commonmark.renderer.html.HtmlRenderer) HtmlNodeRendererContext(org.commonmark.renderer.html.HtmlNodeRendererContext) AttributeProvider(org.commonmark.renderer.html.AttributeProvider)

Aggregations

Node (org.commonmark.node.Node)2 Parser (org.commonmark.parser.Parser)2 AttributeProviderContext (org.commonmark.renderer.html.AttributeProviderContext)2 AttributeProviderFactory (org.commonmark.renderer.html.AttributeProviderFactory)2 HtmlRenderer (org.commonmark.renderer.html.HtmlRenderer)2 Extension (org.commonmark.Extension)1 AutolinkExtension (org.commonmark.ext.autolink.AutolinkExtension)1 TablesExtension (org.commonmark.ext.gfm.tables.TablesExtension)1 HeadingAnchorExtension (org.commonmark.ext.heading.anchor.HeadingAnchorExtension)1 AttributeProvider (org.commonmark.renderer.html.AttributeProvider)1 HtmlNodeRendererContext (org.commonmark.renderer.html.HtmlNodeRendererContext)1 HtmlNodeRendererFactory (org.commonmark.renderer.html.HtmlNodeRendererFactory)1