GP-0: Styling HTML code blocks in converted Markdown files

This commit is contained in:
Ryan Kurtz
2025-04-07 11:26:44 -04:00
parent c91b82f9f0
commit fc3a242a5d

View File

@@ -23,8 +23,7 @@ import org.commonmark.Extension;
import org.commonmark.ext.footnotes.FootnotesExtension;
import org.commonmark.ext.gfm.tables.*;
import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
import org.commonmark.node.Link;
import org.commonmark.node.Node;
import org.commonmark.node.*;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.*;
@@ -58,6 +57,7 @@ public class MarkdownToHtml {
HtmlRenderer renderer = HtmlRenderer.builder()
.extensions(extensions)
.attributeProviderFactory(new TableAttributeProvider())
.attributeProviderFactory(new CodeAttributeProvider())
.attributeProviderFactory(new LinkAttributeProvider())
.build();
@@ -94,6 +94,26 @@ public class MarkdownToHtml {
}
}
/**
* Class to add custom style to code tags and code blocks
*/
private static class CodeAttributeProvider
implements AttributeProvider, AttributeProviderFactory {
@Override
public AttributeProvider create(AttributeProviderContext attributeProviderContext) {
return new CodeAttributeProvider();
}
@Override
public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
if (node instanceof Code || node instanceof IndentedCodeBlock ||
node instanceof FencedCodeBlock) {
attributes.put("style", "background-color: #eef;");
}
}
}
/**
* Class to help adjust links to Markdown files to instead become links to HTML files
*/