use of org.commonmark.renderer.html.AttributeProviderFactory 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>";
}
use of org.commonmark.renderer.html.AttributeProviderFactory 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();
}
Aggregations