Search in sources :

Example 1 with FencedCodeBlock

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&quot;\">content\n</code></pre>\n", rendered2);
}
Also used : Node(com.vladsch.flexmark.ast.Node) Attributes(com.vladsch.flexmark.util.html.Attributes) FencedCodeBlock(com.vladsch.flexmark.ast.FencedCodeBlock) Test(org.junit.Test)

Aggregations

FencedCodeBlock (com.vladsch.flexmark.ast.FencedCodeBlock)1 Node (com.vladsch.flexmark.ast.Node)1 Attributes (com.vladsch.flexmark.util.html.Attributes)1 Test (org.junit.Test)1