Commit 8aee1efe authored by Ubbo Veentjer's avatar Ubbo Veentjer
Browse files

handle yaml in markdown docs

parent 46c6cc15
Pipeline #139985 passed with stages
in 9 minutes and 28 seconds
......@@ -3,8 +3,10 @@ package info.textgrid.rep.markdown;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.commonmark.Extension;
import org.commonmark.ext.front.matter.YamlFrontMatterExtension;
import org.commonmark.ext.front.matter.YamlFrontMatterVisitor;
......@@ -24,29 +26,51 @@ public class MarkdownRenderService {
// https://github.com/atlassian/commonmark-java#thread-safety
private static Parser parser;
private static HtmlRenderer renderer;
//private static YamlFrontMatterVisitor yamlVisitor;
private static YamlFrontMatterVisitor yamlVisitor;
public MarkdownRenderService() {
List<Extension> extensions = Arrays.asList(
HeadingAnchorExtension.create(),
TablesExtension.create()
// YamlFrontMatterExtension.create()
TablesExtension.create(),
YamlFrontMatterExtension.create()
);
parser = Parser.builder()
.extensions(extensions).build();
renderer = HtmlRenderer.builder()
.extensions(extensions).build();
//yamlVisitor = new YamlFrontMatterVisitor();
yamlVisitor = new YamlFrontMatterVisitor();
}
public String renderHtml(InputStream in) throws IOException {
InputStreamReader reader = new InputStreamReader(in);
Node document = parser.parseReader(reader);
//document.accept(yamlVisitor);
document.accept(yamlVisitor);
return renderer.render(document);
}
public Map<String, List<String>> getYaml(InputStream in) throws IOException {
InputStreamReader reader = new InputStreamReader(in);
Node document = parser.parseReader(reader);
document.accept(yamlVisitor);
//ObjectMapper mapper = new ObjectMapper();
//System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(yamlVisitor.getData()));
return yamlVisitor.getData();
}
public Map<String, List<String>> getYamlFromString(String markdown) throws IOException {
StringReader reader = new StringReader(markdown);
Node document = parser.parseReader(reader);
document.accept(yamlVisitor);
//ObjectMapper mapper = new ObjectMapper();
//System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(yamlVisitor.getData()));
return yamlVisitor.getData();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment