Convert to/from markdown

This commit is contained in:
M66B 2024-02-20 10:56:33 +01:00
parent cc576a4c71
commit 95da049eb2
2 changed files with 15 additions and 11 deletions

View File

@ -6715,18 +6715,21 @@ public class FragmentCompose extends FragmentBase {
boolean dirty = false; boolean dirty = false;
String body; String body;
if (markdown ^ extras.getBoolean("markdown")) { boolean convertMarkdown = extras.getBoolean("markdown");
String text = spanned.toString().replace('\u00a0', ' '); if (markdown) {
String html = Markdown.toHtml(text); String html = (convertMarkdown
? HtmlHelper.toHtml(spanned, context)
: Markdown.toHtml(spanned.toString()));
Document doc = JsoupEx.parse(html); Document doc = JsoupEx.parse(html);
doc.body().attr("markdown", Boolean.toString(markdown)); doc.body().attr("markdown", Boolean.toString(markdown));
body = doc.html(); body = doc.html();
} else {
if (markdown != extras.getBoolean("markdown")) body = (convertMarkdown
dirty = true; ? Markdown.toHtml(spanned.toString())
} else : HtmlHelper.toHtml(spanned, context));
body = HtmlHelper.toHtml(spanned, context); }
if (markdown ^ convertMarkdown)
dirty = true;
EntityMessage draft; EntityMessage draft;
@ -7689,7 +7692,7 @@ public class FragmentCompose extends FragmentBase {
Spanned spannedBody; Spanned spannedBody;
if (markdown) { if (markdown) {
String md = Markdown.fromHtml(doc); String md = Markdown.fromHtml(doc.body().html());
spannedBody = new SpannableStringBuilder(md); spannedBody = new SpannableStringBuilder(md);
} else { } else {
HtmlHelper.clearAnnotations(doc); // Legacy left-overs HtmlHelper.clearAnnotations(doc); // Legacy left-overs

View File

@ -32,6 +32,7 @@ import java.util.List;
public class Markdown { public class Markdown {
static String toHtml(String markdown) { static String toHtml(String markdown) {
markdown = markdown.replace('\u00a0', ' ');
List<Extension> extensions = Arrays.asList( List<Extension> extensions = Arrays.asList(
TablesExtension.create(), TablesExtension.create(),
StrikethroughExtension.create()); StrikethroughExtension.create());
@ -45,7 +46,7 @@ public class Markdown {
return r.render(d); return r.render(d);
} }
static String fromHtml(Document d) { static String fromHtml(String html) {
// TODO: HTML to Markdown // TODO: HTML to Markdown
throw new IllegalArgumentException("Not implemented"); throw new IllegalArgumentException("Not implemented");
} }