2222import dev .dediamondpro .minemark .elements .creators .TextElementCreator ;
2323import dev .dediamondpro .minemark .elements .formatting .FormattingElement ;
2424import dev .dediamondpro .minemark .style .Style ;
25+ import dev .dediamondpro .minemark .utils .PrefixedReader ;
2526import org .commonmark .Extension ;
2627import org .commonmark .node .Node ;
2728import org .commonmark .parser .Parser ;
3738import java .nio .charset .StandardCharsets ;
3839import java .util .List ;
3940import java .util .concurrent .locks .ReentrantLock ;
41+ import java .util .regex .Pattern ;
4042
4143/**
4244 * Class responsible for integrating parsing, layout and rendering
4547 * @param <R> The class passed to the rendering implementation at render time
4648 */
4749public class MineMarkCore <S extends Style , R > {
50+ private static final Pattern ACTIVATION_PATTERN = Pattern .compile ("<minemark-activator>.*?</minemark-activator>" , Pattern .DOTALL );
4851 private final Parser markdownParser ;
4952 private final HtmlRenderer htmlRenderer ;
5053 private final MineMarkHtmlParser <S , R > htmlParser ;
@@ -81,6 +84,9 @@ protected MineMarkCore(TextElementCreator<S, R> textElement, List<ElementCreator
8184 * @throws IOException An IOException during parsing
8285 */
8386 public MineMarkElement <S , R > parse (@ NotNull S style , @ NotNull String markdown , @ NotNull Charset charSet ) throws SAXException , IOException {
87+ // Trick the markdown renderer to activate early,
88+ // this makes it so some problematic whitespaces are handled for us
89+ markdown = "<minemark-activator>\n \n **MineMark-activation**\n \n </minemark-activator>" + markdown ;
8490 Node document = markdownParser .parse (markdown );
8591 return parseDocument (style , document , charSet );
8692 }
@@ -109,6 +115,9 @@ public MineMarkElement<S, R> parse(@NotNull S style, @NotNull String markdown) t
109115 * @throws IOException An IOException during parsing
110116 */
111117 public MineMarkElement <S , R > parse (@ NotNull S style , @ NotNull Reader markdown , @ NotNull Charset charSet ) throws SAXException , IOException {
118+ // Trick the markdown renderer to activate early,
119+ // this makes it so some problematic whitespaces are handled for us
120+ markdown = new PrefixedReader ("<minemark-activator>\n \n **MineMark-activation**\n \n </minemark-activator>" , markdown );
112121 Node document = markdownParser .parseReader (markdown );
113122 return parseDocument (style , document , charSet );
114123 }
@@ -127,7 +136,12 @@ public MineMarkElement<S, R> parse(@NotNull S style, @NotNull Reader markdown) t
127136 }
128137
129138 private MineMarkElement <S , R > parseDocument (@ NotNull S style , Node document , @ NotNull Charset charSet ) throws SAXException , IOException {
130- String html = "<minemark>\n " + htmlRenderer .render (document ) + "</minemark>" ;
139+ // Render the document to HTML
140+ String html = htmlRenderer .render (document );
141+ // Remove the markdown activation part
142+ html = ACTIVATION_PATTERN .matcher (html ).replaceFirst ("" );
143+ // Prepare the HTML for parsing
144+ html = "<minemark>\n " + html + "</minemark>" ;
131145 // Acquire the lock to make sure this thread is the only one using the parser
132146 parsingLock .lock ();
133147 try (InputStream stream = new ByteArrayInputStream (html .getBytes (charSet ))) {
0 commit comments