diff --git a/src/main/java/dev/dediamondpro/minemark/MineMarkCore.java b/src/main/java/dev/dediamondpro/minemark/MineMarkCore.java index 524c9d1..ed1c600 100644 --- a/src/main/java/dev/dediamondpro/minemark/MineMarkCore.java +++ b/src/main/java/dev/dediamondpro/minemark/MineMarkCore.java @@ -140,12 +140,15 @@ private MineMarkElement parseDocument(@NotNull S style, Node document, @No String html = htmlRenderer.render(document); // Remove the markdown activation part html = ACTIVATION_PATTERN.matcher(html).replaceFirst(""); + // Get the wrapper to wrap the content with, make sure the html does not include it + String wrapper = getMineMarkWrapper(html); // Prepare the HTML for parsing - html = "\n" + html + ""; + html = "<" + wrapper + ">" + html + ""; // Acquire the lock to make sure this thread is the only one using the parser parsingLock.lock(); try (InputStream stream = new ByteArrayInputStream(html.getBytes(charSet))) { htmlParser.setStyle(style, new LayoutStyle(style)); + htmlParser.setWrapper(wrapper); InputSource source = new InputSource(stream); source.setEncoding(charSet.name()); xmlParser.parse(source); @@ -156,6 +159,16 @@ private MineMarkElement parseDocument(@NotNull S style, Node document, @No } } + private String getMineMarkWrapper(String html) { + String wrapper = "minemark"; + int num = 0; + while (html.contains(wrapper)) { + wrapper = "minemark-" + num; + num++; + } + return wrapper; + } + /** * @param The {@link Style} this core and all associated elements will use * @param The class passed to the rendering implementation at render time diff --git a/src/main/java/dev/dediamondpro/minemark/MineMarkHtmlParser.java b/src/main/java/dev/dediamondpro/minemark/MineMarkHtmlParser.java index ee483d3..1d616d6 100644 --- a/src/main/java/dev/dediamondpro/minemark/MineMarkHtmlParser.java +++ b/src/main/java/dev/dediamondpro/minemark/MineMarkHtmlParser.java @@ -37,6 +37,7 @@ public class MineMarkHtmlParser extends DefaultHandler { private MineMarkElement markDown; private Element currentElement; private LayoutStyle layoutStyle; + private String wrapper = "minemark"; private S style; private StringBuilder textBuilder = new StringBuilder(); private boolean isPreFormatted = false; @@ -50,9 +51,6 @@ protected MineMarkHtmlParser(TextElementCreator textElementCreator, List(style, layoutStyle, attributes); - return; case "br": textBuilder.append("\n"); return; @@ -60,6 +58,10 @@ public void startElement(String uri, String localName, String qName, Attributes isPreFormatted = true; break; } + if (qName.equals(wrapper)) { + currentElement = markDown = new MineMarkElement<>(style, layoutStyle, attributes); + return; + } addText(); LayoutStyle originalLayoutStyle = currentElement.getLayoutStyle(); @@ -89,7 +91,6 @@ public void startElement(String uri, String localName, String qName, Attributes @Override public void endElement(String uri, String localName, String qName) { switch (qName) { - case "minemark": case "br": return; case "pre": @@ -98,6 +99,9 @@ public void endElement(String uri, String localName, String qName) { } addText(); currentElement.complete(); + if (qName.equals(wrapper)) { + return; // We are done parsing + } currentElement = currentElement.getParent(); } @@ -162,6 +166,10 @@ protected void setStyle(S style, LayoutStyle layoutStyle) { this.layoutStyle = layoutStyle; } + protected void setWrapper(String wrapper) { + this.wrapper = wrapper; + } + protected MineMarkElement getParsedResult() { return markDown; }