Added debug option to Markdown

master
M66B 3 months ago
parent 5141e82871
commit a80771db51

@ -126,7 +126,7 @@ public class AI {
} else
throw new IllegalArgumentException("No AI available");
String html = Markdown.toHtml(sb.toString());
String html = Markdown.toHtml(sb.toString(), context);
Document d = HtmlHelper.sanitizeCompose(context, html, false);
return HtmlHelper.fromDocument(context, d, null, null);
}
@ -239,7 +239,7 @@ public class AI {
} else
throw new IllegalArgumentException("No AI available");
String html = Markdown.toHtml(sb.toString());
String html = Markdown.toHtml(sb.toString(), context);
Document doc = HtmlHelper.sanitizeCompose(context, html, false);
return HtmlHelper.fromDocument(context, doc, null, null);
}

@ -7287,13 +7287,13 @@ public class FragmentCompose extends FragmentBase {
if (markdown) {
String html = (convertMarkdown
? HtmlHelper.toHtml(spanned, context)
: Markdown.toHtml(spanned.toString()));
: Markdown.toHtml(spanned.toString(), context));
Document doc = JsoupEx.parse(html);
doc.body().attr("markdown", Boolean.toString(markdown));
body = doc.html();
} else
body = (convertMarkdown
? Markdown.toHtml(spanned.toString())
? Markdown.toHtml(spanned.toString(), context)
: HtmlHelper.toHtml(spanned, context));
if (convertMarkdown)
dirty = true;
@ -8279,7 +8279,7 @@ public class FragmentCompose extends FragmentBase {
Spanned spannedBody;
if (markdown) {
String md = Markdown.fromHtml(doc.body().html());
String md = Markdown.fromHtml(doc.body().html(), context);
spannedBody = new SpannableStringBuilder(md);
} else {
HtmlHelper.clearAnnotations(doc); // Legacy left-overs

@ -19,6 +19,11 @@ package eu.faircode.email;
Copyright 2018-2026 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.data.MutableDataSet;
@ -41,7 +46,7 @@ import java.util.List;
import java.util.Map;
public class Markdown {
static String toHtml(String markdown) {
static String toHtml(String markdown, Context context) {
// https://github.com/commonmark/commonmark-java#usage
// https://github.com/commonmark/commonmark-java/issues/294
markdown = markdown.replace('\u00a0', ' ');
@ -59,14 +64,18 @@ public class Markdown {
.extensions(extensions)
.build();
String html = r.render(d);
if (BuildConfig.DEBUG) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean debug = prefs.getBoolean("debug", false);
if (BuildConfig.DEBUG || debug) {
Log.i("Markdown md=" + markdown.replace('\n', '|'));
Log.i("Markdown html=" + html.replace('\n', '|'));
}
return html;
}
static String fromHtml(String html) {
static String fromHtml(String html, Context context) {
// https://github.com/vsch/flexmark-java/wiki/Extensions#html-to-markdown
Map<String, String> specialCharsMap = new HashMap<>();
//specialCharsMap.put("“", "\"");
@ -123,7 +132,9 @@ public class Markdown {
.build()
.convert(doc.html());
if (BuildConfig.DEBUG) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean debug = prefs.getBoolean("debug", false);
if (BuildConfig.DEBUG || debug) {
Log.i("Markdown html=" + html.replace('\n', '|'));
Log.i("Markdown md=" + markdown.replace('\n', '|'));
}

@ -4279,7 +4279,7 @@ public class MessageHelper {
StandardCharsets.US_ASCII.equals(cs) ||
StandardCharsets.ISO_8859_1.equals(cs))
result = new String(result.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
result = (first ? "" : "<br><hr>") + Markdown.toHtml(result);
result = (first ? "" : "<br><hr>") + Markdown.toHtml(result, context);
} catch (Throwable ex) {
Log.e(ex);
result = HtmlHelper.formatPlainText(result);

Loading…
Cancel
Save