From 7a577df3e283ca1dae5e3e1f83d746937650ab6f Mon Sep 17 00:00:00 2001 From: Grzegorz Orczykowski Date: Sat, 13 Apr 2024 01:07:52 +0200 Subject: [PATCH] Improved handling markdown --- .../java/org/odk/collect/android/utilities/HtmlUtils.java | 5 +---- .../org/odk/collect/android/utilities/HtmlUtilsTest.java | 6 ++++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/collect_app/src/main/java/org/odk/collect/android/utilities/HtmlUtils.java b/collect_app/src/main/java/org/odk/collect/android/utilities/HtmlUtils.java index d911b601670..90d6ccfe053 100644 --- a/collect_app/src/main/java/org/odk/collect/android/utilities/HtmlUtils.java +++ b/collect_app/src/main/java/org/odk/collect/android/utilities/HtmlUtils.java @@ -87,10 +87,7 @@ static String markdownToHtml(String text) { text = text.replaceAll("(?s)\\*\\*(.*?)\\*\\*", "$1"); // emphasis using underscore - text = text.replaceAll("\\s_([^\\s][^_\n]*)_\\s", " $1 "); - text = text.replaceAll("^_([^\\s][^_\n]*)_$", "$1"); - text = text.replaceAll("^_([^\\s][^_\n]*)_\\s", "$1 "); - text = text.replaceAll("\\s_([^\\s][^_\n]*)_$", " $1"); + text = text.replaceAll("(^|[\\s]|[^\\w])_([^\\s][^_\n]*)_($|[\\s]|[^\\w])", "$1$2$3"); // emphasis using asterisk text = text.replaceAll("\\*([^\\s][^\\*\n]*)\\*", "$1"); diff --git a/collect_app/src/test/java/org/odk/collect/android/utilities/HtmlUtilsTest.java b/collect_app/src/test/java/org/odk/collect/android/utilities/HtmlUtilsTest.java index 2c75128f42f..066c048c18b 100644 --- a/collect_app/src/test/java/org/odk/collect/android/utilities/HtmlUtilsTest.java +++ b/collect_app/src/test/java/org/odk/collect/android/utilities/HtmlUtilsTest.java @@ -35,7 +35,13 @@ public void markDownToHtmlEscapesBackslash() { {"A _B_ C", "A B C"}, {"A_B_ C", "A_B_ C"}, {"A _B_C", "A _B_C"}, + {"_A_5", "_A_5"}, {"_A_", "A"}, + {"(_A_)", "(A)"}, + {"_A_?", "A?"}, + {"*_A_", "*A"}, + {"blah _A_!", "blah A!"}, + {" _A_! blah", " A! blah"}, {"\\_ _AB\\__", "_ AB_"}, {"\\\\ _AB\\_\\\\_", "\\ AB_\\"}, {"A\\*B\\*C", "A*B*C"},