moved from commonmark to inhouse MutableMarkdown for verification

This commit is contained in:
Mike Yaacoub
2022-12-19 13:24:40 -05:00
parent 7ba2a3de38
commit c149684d09
3 changed files with 31 additions and 5 deletions

View File

@@ -51,6 +51,11 @@ public class MutableMarkdown {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public MutableMarkdown(String rawMarkdown) {
this.path = null;
this.rawMarkdown = rawMarkdown;
parseStructure(rawMarkdown);
}
private void parseStructure(String rawMarkdown) { private void parseStructure(String rawMarkdown) {
AbstractYamlFrontMatterVisitor v = new AbstractYamlFrontMatterVisitor(); AbstractYamlFrontMatterVisitor v = new AbstractYamlFrontMatterVisitor();
@@ -69,13 +74,19 @@ public class MutableMarkdown {
} else if (node instanceof WhiteSpace) { } else if (node instanceof WhiteSpace) {
} else if (node instanceof YamlFrontMatterBlock) { } else if (node instanceof YamlFrontMatterBlock) {
} else { } else {
if(this.path != null)
throw new RuntimeException("The markdown file at '" + this.path.toString() + "' must have an initial heading as a title, before any other element, but found:" + node.getClass().getSimpleName()); throw new RuntimeException("The markdown file at '" + this.path.toString() + "' must have an initial heading as a title, before any other element, but found:" + node.getClass().getSimpleName());
else
throw new RuntimeException("The markdown string provided must have an initial heading as a title, before any other element, but found: "+ node.getClass().getSimpleName());
} }
node=node.getNext(); node=node.getNext();
} }
} }
if (frontMatter.getTitle()==null || frontMatter.getTitle().isEmpty()) { if (frontMatter.getTitle()==null || frontMatter.getTitle().isEmpty()) {
if(this.path != null)
throw new RuntimeException("The markdown file at '" + this.path.toString() + "' has no heading to use as a title."); throw new RuntimeException("The markdown file at '" + this.path.toString() + "' has no heading to use as a title.");
else
throw new RuntimeException("The markdown string provided has no heading to use as a title.");
} }
} }
@@ -90,7 +101,10 @@ public class MutableMarkdown {
if (end>=0) { if (end>=0) {
return rawMarkdown.substring(end+4); return rawMarkdown.substring(end+4);
} else { } else {
if(path != null)
throw new RuntimeException("Unable to find matching boundaries in " + path.toString() + ": " + boundary); throw new RuntimeException("Unable to find matching boundaries in " + path.toString() + ": " + boundary);
else
throw new RuntimeException("Unable to find matching boundaries in provided markdown: " + boundary);
} }
} }
} }

View File

@@ -72,6 +72,12 @@
<artifactId>adapter-diag</artifactId> <artifactId>adapter-diag</artifactId>
<version>4.17.31-SNAPSHOT</version> <version>4.17.31-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>org.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.20.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -39,6 +39,8 @@ import java.util.concurrent.Future;
import java.util.function.Function; import java.util.function.Function;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import org.commonmark.parser.Parser;
import org.commonmark.node.Node;
public class BundledMarkdownZipExporter { public class BundledMarkdownZipExporter {
@@ -74,10 +76,14 @@ public class BundledMarkdownZipExporter {
{ {
String filename = entry.getKey(); String filename = entry.getKey();
StringBuilder fileStringBuilder = entry.getValue(); StringBuilder fileStringBuilder = entry.getValue();
MutableMarkdown parsed = new MutableMarkdown(fileStringBuilder.toString());
for (BundledMarkdownProcessor filter : this.filters) {
parsed = filter.apply(parsed);
}
ZipEntry zipEntry = new ZipEntry(bindingsPrefix + filename); ZipEntry zipEntry = new ZipEntry(bindingsPrefix + filename);
zipEntry.setTime(new Date().getTime()); zipEntry.setTime(new Date().getTime());
zipstream.putNextEntry(zipEntry); zipstream.putNextEntry(zipEntry);
zipstream.write(fileStringBuilder.toString().getBytes()); zipstream.write(parsed.getComposedMarkdown().getBytes(StandardCharsets.UTF_8));
zipstream.closeEntry(); zipstream.closeEntry();
} }
zipstream.finish(); zipstream.finish();