use of com.vladsch.flexmark.ast.FencedCodeBlock in project flexmark-java by vsch.
the class HtmlRendererTest method attributeProviderForCodeBlock.
@Test
public void attributeProviderForCodeBlock() {
AttributeProviderFactory factory = new IndependentAttributeProviderFactory() {
@Override
public AttributeProvider create(LinkResolverContext context) {
// noinspection ReturnOfInnerClass
return new AttributeProvider() {
@Override
public void setAttributes(Node node, AttributablePart part, Attributes attributes) {
if (node instanceof FencedCodeBlock && part == CoreNodeRenderer.CODE_CONTENT) {
FencedCodeBlock fencedCodeBlock = (FencedCodeBlock) node;
// Remove the default attribute for info
attributes.remove("class");
// Put info in custom attribute instead
attributes.replaceValue("data-custom", fencedCodeBlock.getInfo().toString());
}
}
};
}
};
HtmlRenderer renderer = HtmlRenderer.builder().attributeProviderFactory(factory).build();
String rendered = renderer.render(parse("```info\ncontent\n```"));
assertEquals("<pre><code data-custom=\"info\">content\n</code></pre>\n", rendered);
String rendered2 = renderer.render(parse("```evil\"\ncontent\n```"));
assertEquals("<pre><code data-custom=\"evil"\">content\n</code></pre>\n", rendered2);
}
Aggregations