Use flexmark to convert to Markdown

This commit is contained in:
M66B
2024-02-20 10:59:17 +01:00
parent 95da049eb2
commit 273501116e
2 changed files with 10 additions and 3 deletions

View File

@@ -777,6 +777,9 @@ dependencies {
implementation "org.commonmark:commonmark-ext-gfm-tables:$commonmark_version";
implementation "org.commonmark:commonmark-ext-gfm-strikethrough:$commonmark_version";
// https://github.com/vsch/flexmark-java
implementation "com.vladsch.flexmark:flexmark-html2md-converter:0.64.8"
// // https://github.com/QuadFlask/colorpicker
//implementation "com.github.QuadFlask:colorpicker:$colorpicker_version"
implementation project(':colorpicker')

View File

@@ -19,13 +19,14 @@ package eu.faircode.email;
Copyright 2018-2024 by Marcel Bokhorst (M66B)
*/
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
import org.commonmark.Extension;
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.jsoup.nodes.Document;
import java.util.Arrays;
import java.util.List;
@@ -47,7 +48,10 @@ public class Markdown {
}
static String fromHtml(String html) {
// TODO: HTML to Markdown
throw new IllegalArgumentException("Not implemented");
// https://github.com/vsch/flexmark-java/wiki/Extensions#html-to-markdown
String markdown = FlexmarkHtmlConverter.builder()
.build()
.convert(html);
return markdown.replaceAll("\n\n\\s+<!-- -->\n", "");
}
}