1.解析markdown里的所有代码块.
通过创建visitor,收集markdown里的代码片段并分类.
public static void main(String[] args) throws IOException {
Parser parser = Parser.builder().build();
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(SRC_MD), StandardCharsets.UTF_8)) {
Node document = parser.parseReader(reader);
MyVisitor visitor = new MyVisitor();
document.accept(visitor);
// HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build();
// System.out.println(renderer.render(document));
System.out.println(visitor.codeType2Codes);
}
}
static class MyVisitor extends AbstractVisitor {
public Map<String, List<String>> codeType2Codes = new HashMap<>();
public MyVisitor() {
}
/**
* 代码块
*
* @param fencedCodeBlock
*/
@Override
public void visit(FencedCodeBlock fencedCodeBlock) {
codeType2Codes.putIfAbsent(fencedCodeBlock.getInfo(), new ArrayList<>());
codeType2Codes.get(fencedCodeBlock.getInfo()).add(fencedCodeBlock.getLiteral());
visitChildren(fencedCodeBlock);
}
}