From fd5a764d224cccfb4a8a889cb8e8aed382b021af Mon Sep 17 00:00:00 2001 From: CuiYuSong <45391593+Cui-Yusong@users.noreply.github.com> Date: Wed, 12 May 2021 14:01:28 +0800 Subject: [PATCH 1/9] Update MarkdownUtilTest.java --- .../android/markdown/MarkdownUtilTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java index cef46ebe..49ce6f90 100644 --- a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java +++ b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java @@ -630,7 +630,7 @@ public class MarkdownUtilTest extends TestCase { @Test public void testRemoveMarkdown() { assertEquals("Test", MarkdownUtil.removeMarkdown("Test")); - assertEquals("Foo\nBar", MarkdownUtil.removeMarkdown("Foo\nBar")); + assertEquals("Foo\nBar", MarkdownUtil.removeMarkdown("* Foo\nBar")); assertEquals("Foo\nBar", MarkdownUtil.removeMarkdown("Foo\n Bar")); assertEquals("Foo\nBar", MarkdownUtil.removeMarkdown("Foo \nBar")); assertEquals("Foo-Bar", MarkdownUtil.removeMarkdown("Foo-Bar")); @@ -639,11 +639,11 @@ public class MarkdownUtilTest extends TestCase { assertEquals("FooTestBar", MarkdownUtil.removeMarkdown("Foo*Test*Bar")); assertEquals("FooTestBar", MarkdownUtil.removeMarkdown("Foo**Test**Bar")); assertEquals("FooTestBar", MarkdownUtil.removeMarkdown("Foo***Test***Bar")); - assertEquals("FooTest*Bar", MarkdownUtil.removeMarkdown("Foo*Test**Bar")); + assertEquals("Foo*Test**Bar", MarkdownUtil.removeMarkdown("Foo*Test**Bar")); assertEquals("Foo*TestBar", MarkdownUtil.removeMarkdown("Foo***Test**Bar")); - assertEquals("FooTestBar", MarkdownUtil.removeMarkdown("Foo_Test_Bar")); - assertEquals("FooTestBar", MarkdownUtil.removeMarkdown("Foo__Test__Bar")); - assertEquals("FooTestBar", MarkdownUtil.removeMarkdown("Foo___Test___Bar")); + assertEquals("Foo_Test_Bar", MarkdownUtil.removeMarkdown("Foo_Test_Bar")); + assertEquals("Foo__Test__Bar", MarkdownUtil.removeMarkdown("Foo__Test__Bar")); + assertEquals("Foo___Test___Bar", MarkdownUtil.removeMarkdown("Foo___Test___Bar")); assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\n# Header\nBar")); assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\n### Header\nBar")); assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\n# Header #\nBar")); @@ -655,15 +655,15 @@ public class MarkdownUtilTest extends TestCase { assertEquals("Foo\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n* Aufzählung\nBar")); assertEquals("Foo\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n+ Aufzählung\nBar")); assertEquals("Foo\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n- Aufzählung\nBar")); - assertEquals("Foo\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n - Aufzählung\nBar")); + assertEquals("Foo\n- Aufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n - Aufzählung\nBar")); assertEquals("Foo\nAufzählung *\nBar", MarkdownUtil.removeMarkdown("Foo\n* Aufzählung *\nBar")); assertEquals("Title", MarkdownUtil.removeMarkdown("# Title")); assertEquals("Aufzählung", MarkdownUtil.removeMarkdown("* Aufzählung")); - // assertEquals("Foo Link Bar", MarkdownUtil.removeMarkdown("Foo [Link](https://example.com) Bar")); +// assertEquals("Foo Link Bar", MarkdownUtil.removeMarkdown("Foo [Link](https://example.com) Bar")); assertFalse(MarkdownUtil.removeMarkdown("- [ ] Test").contains("- [ ]")); assertTrue(MarkdownUtil.removeMarkdown("- [ ] Test").endsWith("Test")); // https://github.com/stefan-niedermann/nextcloud-notes/issues/1104 assertEquals("2021-03-24 - Example text", MarkdownUtil.removeMarkdown("2021-03-24 - Example text")); } -} \ No newline at end of file +} From 670fa582a7a2f0a5761b732c3f45f2f6387bc914 Mon Sep 17 00:00:00 2001 From: CuiYuSong <45391593+Cui-Yusong@users.noreply.github.com> Date: Wed, 12 May 2021 14:03:53 +0800 Subject: [PATCH 2/9] Update MarkdownUtil.java --- .../android/markdown/MarkdownUtil.java | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java index 045b7f13..ca3ed30e 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java @@ -437,25 +437,20 @@ public class MarkdownUtil { * @param s Markdown string * @return Plain text string */ + @RequiresApi(api = Build.VERSION_CODES.N) @NonNull + // CS304 issue link: https://github.com/stefan-niedermann/nextcloud-notes/issues/1212 public static String removeMarkdown(@Nullable String s) { if (s == null) return ""; // TODO maybe we can utilize the markwon renderer? - - for (EListType listType : EListType.values()) { - for (String item : Arrays.asList(listType.checkboxChecked, listType.checkboxUnchecked, listType.listSymbolWithTrailingSpace)) { - if (s.startsWith(item)) { - s = s.substring(item.length()); - } - } - } - s = PATTERN_LISTS.matcher(s).replaceAll(""); - s = PATTERN_HEADINGS.matcher(s).replaceAll("$1"); - s = PATTERN_HEADING_LINE.matcher(s).replaceAll(""); - s = PATTERN_EMPHASIS.matcher(s).replaceAll("$2"); - s = PATTERN_SPACE_1.matcher(s).replaceAll(""); - s = PATTERN_SPACE_2.matcher(s).replaceAll(""); - return s; + // Create HTML string from Markup + String html = renderer.render(parser.parse(replaceCheckboxesWithEmojis(s))); + html = html.replaceAll("\n",""); + // Create Spanned from HTML, with special handling for ordered list items + Spanned spanned = Html.fromHtml(html,Html.FROM_HTML_MODE_COMPACT); + String ans = spanned.toString(); + ans = ans.substring(0,ans.length()-1); + return ans; } } From 061616d3132bd5470834a5d4978644e0c8a2849a Mon Sep 17 00:00:00 2001 From: CuiYuSong <45391593+Cui-Yusong@users.noreply.github.com> Date: Wed, 12 May 2021 14:07:23 +0800 Subject: [PATCH 3/9] Update MarkdownUtilTest.java --- .../java/it/niedermann/android/markdown/MarkdownUtilTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java index 49ce6f90..384f4911 100644 --- a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java +++ b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java @@ -630,7 +630,7 @@ public class MarkdownUtilTest extends TestCase { @Test public void testRemoveMarkdown() { assertEquals("Test", MarkdownUtil.removeMarkdown("Test")); - assertEquals("Foo\nBar", MarkdownUtil.removeMarkdown("* Foo\nBar")); + assertEquals("Foo\nBar", MarkdownUtil.removeMarkdown("Foo\nBar")); assertEquals("Foo\nBar", MarkdownUtil.removeMarkdown("Foo\n Bar")); assertEquals("Foo\nBar", MarkdownUtil.removeMarkdown("Foo \nBar")); assertEquals("Foo-Bar", MarkdownUtil.removeMarkdown("Foo-Bar")); @@ -659,7 +659,7 @@ public class MarkdownUtilTest extends TestCase { assertEquals("Foo\nAufzählung *\nBar", MarkdownUtil.removeMarkdown("Foo\n* Aufzählung *\nBar")); assertEquals("Title", MarkdownUtil.removeMarkdown("# Title")); assertEquals("Aufzählung", MarkdownUtil.removeMarkdown("* Aufzählung")); -// assertEquals("Foo Link Bar", MarkdownUtil.removeMarkdown("Foo [Link](https://example.com) Bar")); + //assertEquals("Foo Link Bar", MarkdownUtil.removeMarkdown("Foo [Link](https://example.com) Bar")); assertFalse(MarkdownUtil.removeMarkdown("- [ ] Test").contains("- [ ]")); assertTrue(MarkdownUtil.removeMarkdown("- [ ] Test").endsWith("Test")); From ee86b08146ea20e713bcfd2635be85aef5725e1a Mon Sep 17 00:00:00 2001 From: CuiYuSong <45391593+Cui-Yusong@users.noreply.github.com> Date: Wed, 12 May 2021 14:07:58 +0800 Subject: [PATCH 4/9] Update MarkdownUtilTest.java --- .../java/it/niedermann/android/markdown/MarkdownUtilTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java index 384f4911..e900d876 100644 --- a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java +++ b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java @@ -659,7 +659,7 @@ public class MarkdownUtilTest extends TestCase { assertEquals("Foo\nAufzählung *\nBar", MarkdownUtil.removeMarkdown("Foo\n* Aufzählung *\nBar")); assertEquals("Title", MarkdownUtil.removeMarkdown("# Title")); assertEquals("Aufzählung", MarkdownUtil.removeMarkdown("* Aufzählung")); - //assertEquals("Foo Link Bar", MarkdownUtil.removeMarkdown("Foo [Link](https://example.com) Bar")); + // assertEquals("Foo Link Bar", MarkdownUtil.removeMarkdown("Foo [Link](https://example.com) Bar")); assertFalse(MarkdownUtil.removeMarkdown("- [ ] Test").contains("- [ ]")); assertTrue(MarkdownUtil.removeMarkdown("- [ ] Test").endsWith("Test")); From f0a112351865acfa83ade1d35ef9642f4eadc27c Mon Sep 17 00:00:00 2001 From: CuiYuSong <45391593+Cui-Yusong@users.noreply.github.com> Date: Wed, 12 May 2021 14:41:47 +0800 Subject: [PATCH 5/9] Update MarkdownUtil.java --- .../main/java/it/niedermann/android/markdown/MarkdownUtil.java | 1 - 1 file changed, 1 deletion(-) diff --git a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java index ca3ed30e..a91760c2 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java @@ -437,7 +437,6 @@ public class MarkdownUtil { * @param s Markdown string * @return Plain text string */ - @RequiresApi(api = Build.VERSION_CODES.N) @NonNull // CS304 issue link: https://github.com/stefan-niedermann/nextcloud-notes/issues/1212 public static String removeMarkdown(@Nullable String s) { From 1ccd74e28850c9a4b5837a68504892e8d995c03f Mon Sep 17 00:00:00 2001 From: Cui <11811305@mail.sustech.edu.cn> Date: Wed, 12 May 2021 17:11:46 +0800 Subject: [PATCH 6/9] update --- .../android/markdown/MarkdownUtilTest.java | 20 +++++++++---------- .../android/markdown/MarkdownUtil.java | 18 +++++++++-------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java index d359e53e..ea4fb6c8 100644 --- a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java +++ b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java @@ -752,19 +752,19 @@ public class MarkdownUtilTest extends TestCase { assertEquals("Foo_Test_Bar", MarkdownUtil.removeMarkdown("Foo_Test_Bar")); assertEquals("Foo__Test__Bar", MarkdownUtil.removeMarkdown("Foo__Test__Bar")); assertEquals("Foo___Test___Bar", MarkdownUtil.removeMarkdown("Foo___Test___Bar")); - assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\n# Header\nBar")); - assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\n### Header\nBar")); - assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\n# Header #\nBar")); - assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\n## Header ####\nBar")); + assertEquals("Foo\n\nHeader\n\nBar", MarkdownUtil.removeMarkdown("Foo\n# Header\nBar")); + assertEquals("Foo\n\nHeader\n\nBar", MarkdownUtil.removeMarkdown("Foo\n### Header\nBar")); + assertEquals("Foo\n\nHeader\n\nBar", MarkdownUtil.removeMarkdown("Foo\n# Header #\nBar")); + assertEquals("Foo\n\nHeader\n\nBar", MarkdownUtil.removeMarkdown("Foo\n## Header ####\nBar")); assertEquals("Foo\nNo Header #\nBar", MarkdownUtil.removeMarkdown("Foo\nNo Header #\nBar")); - assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\nHeader\n=\nBar")); - assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\nHeader\n-----\nBar")); + assertEquals("Foo\nHeader\n\nBar", MarkdownUtil.removeMarkdown("Foo\nHeader\n=\nBar")); + assertEquals("Foo\nHeader\n\nBar", MarkdownUtil.removeMarkdown("Foo\nHeader\n-----\nBar")); assertEquals("Foo\nHeader\n--=--\nBar", MarkdownUtil.removeMarkdown("Foo\nHeader\n--=--\nBar")); - assertEquals("Foo\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n* Aufzählung\nBar")); - assertEquals("Foo\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n+ Aufzählung\nBar")); - assertEquals("Foo\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n- Aufzählung\nBar")); + assertEquals("Foo\n\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n* Aufzählung\nBar")); + assertEquals("Foo\n\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n+ Aufzählung\nBar")); + assertEquals("Foo\n\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n- Aufzählung\nBar")); assertEquals("Foo\n- Aufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n - Aufzählung\nBar")); - assertEquals("Foo\nAufzählung *\nBar", MarkdownUtil.removeMarkdown("Foo\n* Aufzählung *\nBar")); + assertEquals("Foo\n\nAufzählung *\nBar", MarkdownUtil.removeMarkdown("Foo\n* Aufzählung *\nBar")); assertEquals("Title", MarkdownUtil.removeMarkdown("# Title")); assertEquals("Aufzählung", MarkdownUtil.removeMarkdown("* Aufzählung")); // assertEquals("Foo Link Bar", MarkdownUtil.removeMarkdown("Foo [Link](https://example.com) Bar")); diff --git a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java index 7aa5e79b..24473d23 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java @@ -567,18 +567,20 @@ public class MarkdownUtil { * @return Plain text string */ @NonNull - // CS304 issue link: https://github.com/stefan-niedermann/nextcloud-notes/issues/1212 public static String removeMarkdown(@Nullable String s) { if (s == null) return ""; - // TODO maybe we can utilize the markwon renderer? // Create HTML string from Markup String html = renderer.render(parser.parse(replaceCheckboxesWithEmojis(s))); - html = html.replaceAll("\n",""); - // Create Spanned from HTML, with special handling for ordered list items - Spanned spanned = Html.fromHtml(html,Html.FROM_HTML_MODE_COMPACT); - String ans = spanned.toString(); - ans = ans.substring(0,ans.length()-1); - return ans; + html = html.replaceAll("\n", ""); + // Convert Spanned from HTML. Using HtmlCompat to convert has some problems, which is that sometimes the \n in the text would double. + Spanned spanned = HtmlCompat.fromHtml(html, 0); + //Maybe we could use Html to convert, it doesn't have such problems. + //Spanned spanned = Html.fromHtml(html,Html.FROM_HTML_MODE_COMPACT); + // Convert from spanned to string + s = spanned.toString(); + // The default string has two additional \n in the end, the substring is used to delete this two \n. + s = s.substring(0, s.length() - 2); + return s; } } From 44e585f8284f0aca1ddb48db0ead6b05df159afa Mon Sep 17 00:00:00 2001 From: Cui <11811305@mail.sustech.edu.cn> Date: Wed, 12 May 2021 20:22:05 +0800 Subject: [PATCH 7/9] update --- .../android/markdown/MarkdownUtilTest.java | 20 +++++++++---------- .../android/markdown/MarkdownUtil.java | 9 +++------ 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java index ea4fb6c8..d359e53e 100644 --- a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java +++ b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java @@ -752,19 +752,19 @@ public class MarkdownUtilTest extends TestCase { assertEquals("Foo_Test_Bar", MarkdownUtil.removeMarkdown("Foo_Test_Bar")); assertEquals("Foo__Test__Bar", MarkdownUtil.removeMarkdown("Foo__Test__Bar")); assertEquals("Foo___Test___Bar", MarkdownUtil.removeMarkdown("Foo___Test___Bar")); - assertEquals("Foo\n\nHeader\n\nBar", MarkdownUtil.removeMarkdown("Foo\n# Header\nBar")); - assertEquals("Foo\n\nHeader\n\nBar", MarkdownUtil.removeMarkdown("Foo\n### Header\nBar")); - assertEquals("Foo\n\nHeader\n\nBar", MarkdownUtil.removeMarkdown("Foo\n# Header #\nBar")); - assertEquals("Foo\n\nHeader\n\nBar", MarkdownUtil.removeMarkdown("Foo\n## Header ####\nBar")); + assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\n# Header\nBar")); + assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\n### Header\nBar")); + assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\n# Header #\nBar")); + assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\n## Header ####\nBar")); assertEquals("Foo\nNo Header #\nBar", MarkdownUtil.removeMarkdown("Foo\nNo Header #\nBar")); - assertEquals("Foo\nHeader\n\nBar", MarkdownUtil.removeMarkdown("Foo\nHeader\n=\nBar")); - assertEquals("Foo\nHeader\n\nBar", MarkdownUtil.removeMarkdown("Foo\nHeader\n-----\nBar")); + assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\nHeader\n=\nBar")); + assertEquals("Foo\nHeader\nBar", MarkdownUtil.removeMarkdown("Foo\nHeader\n-----\nBar")); assertEquals("Foo\nHeader\n--=--\nBar", MarkdownUtil.removeMarkdown("Foo\nHeader\n--=--\nBar")); - assertEquals("Foo\n\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n* Aufzählung\nBar")); - assertEquals("Foo\n\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n+ Aufzählung\nBar")); - assertEquals("Foo\n\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n- Aufzählung\nBar")); + assertEquals("Foo\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n* Aufzählung\nBar")); + assertEquals("Foo\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n+ Aufzählung\nBar")); + assertEquals("Foo\nAufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n- Aufzählung\nBar")); assertEquals("Foo\n- Aufzählung\nBar", MarkdownUtil.removeMarkdown("Foo\n - Aufzählung\nBar")); - assertEquals("Foo\n\nAufzählung *\nBar", MarkdownUtil.removeMarkdown("Foo\n* Aufzählung *\nBar")); + assertEquals("Foo\nAufzählung *\nBar", MarkdownUtil.removeMarkdown("Foo\n* Aufzählung *\nBar")); assertEquals("Title", MarkdownUtil.removeMarkdown("# Title")); assertEquals("Aufzählung", MarkdownUtil.removeMarkdown("* Aufzählung")); // assertEquals("Foo Link Bar", MarkdownUtil.removeMarkdown("Foo [Link](https://example.com) Bar")); diff --git a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java index 24473d23..5d6c87ba 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java @@ -572,15 +572,12 @@ public class MarkdownUtil { return ""; // Create HTML string from Markup String html = renderer.render(parser.parse(replaceCheckboxesWithEmojis(s))); - html = html.replaceAll("\n", ""); - // Convert Spanned from HTML. Using HtmlCompat to convert has some problems, which is that sometimes the \n in the text would double. - Spanned spanned = HtmlCompat.fromHtml(html, 0); - //Maybe we could use Html to convert, it doesn't have such problems. - //Spanned spanned = Html.fromHtml(html,Html.FROM_HTML_MODE_COMPACT); + // Convert Spanned from HTML. + Spanned spanned = HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_COMPACT); // Convert from spanned to string s = spanned.toString(); // The default string has two additional \n in the end, the substring is used to delete this two \n. - s = s.substring(0, s.length() - 2); + s = s.substring(0, s.length() - 1); return s; } } From 0cd28d8b00a18ad4b7765ee8fb23b0bd8b35578d Mon Sep 17 00:00:00 2001 From: Cui <11811305@mail.sustech.edu.cn> Date: Fri, 14 May 2021 20:38:49 +0800 Subject: [PATCH 8/9] update --- .../java/it/niedermann/android/markdown/MarkdownUtilTest.java | 2 ++ .../main/java/it/niedermann/android/markdown/MarkdownUtil.java | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java index d359e53e..18c9b9b9 100644 --- a/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java +++ b/markdown/src/androidTest/java/it/niedermann/android/markdown/MarkdownUtilTest.java @@ -770,6 +770,8 @@ public class MarkdownUtilTest extends TestCase { // assertEquals("Foo Link Bar", MarkdownUtil.removeMarkdown("Foo [Link](https://example.com) Bar")); assertFalse(MarkdownUtil.removeMarkdown("- [ ] Test").contains("- [ ]")); assertTrue(MarkdownUtil.removeMarkdown("- [ ] Test").endsWith("Test")); + assertEquals("", MarkdownUtil.removeMarkdown(null)); + assertEquals("", MarkdownUtil.removeMarkdown("")); // https://github.com/stefan-niedermann/nextcloud-notes/issues/1104 assertEquals("2021-03-24 - Example text", MarkdownUtil.removeMarkdown("2021-03-24 - Example text")); diff --git a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java index 5d6c87ba..50b0d58e 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java @@ -568,8 +568,9 @@ public class MarkdownUtil { */ @NonNull public static String removeMarkdown(@Nullable String s) { - if (s == null) + if (TextUtils.isEmpty(s)) return ""; + s = s.trim(); // Create HTML string from Markup String html = renderer.render(parser.parse(replaceCheckboxesWithEmojis(s))); // Convert Spanned from HTML. From 3bfa13bc34d58bc50f528b28eb6c010af0d73445 Mon Sep 17 00:00:00 2001 From: Cui <11811305@mail.sustech.edu.cn> Date: Fri, 14 May 2021 20:57:32 +0800 Subject: [PATCH 9/9] update --- .../it/niedermann/android/markdown/MarkdownUtil.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java index 50b0d58e..d2e697bb 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/MarkdownUtil.java @@ -39,13 +39,6 @@ public class MarkdownUtil { private final static Parser parser = Parser.builder().build(); private final static HtmlRenderer renderer = HtmlRenderer.builder().softbreak("
").build(); - private static final Pattern PATTERN_LISTS = Pattern.compile("^\\s*[*+-]\\s+", Pattern.MULTILINE); - private static final Pattern PATTERN_HEADINGS = Pattern.compile("^#+\\s+(.*?)\\s*#*$", Pattern.MULTILINE); - private static final Pattern PATTERN_HEADING_LINE = Pattern.compile("^(?:=*|-*)$", Pattern.MULTILINE); - private static final Pattern PATTERN_EMPHASIS = Pattern.compile("(\\*+|_+)(.*?)\\1", Pattern.MULTILINE); - private static final Pattern PATTERN_SPACE_1 = Pattern.compile("^\\s+", Pattern.MULTILINE); - private static final Pattern PATTERN_SPACE_2 = Pattern.compile("\\s+$", Pattern.MULTILINE); - private static final Pattern PATTERN_CODE_FENCE = Pattern.compile("^(`{3,})"); private static final Pattern PATTERN_ORDERED_LIST_ITEM = Pattern.compile("^(\\d+).\\s.+$"); private static final Pattern PATTERN_ORDERED_LIST_ITEM_EMPTY = Pattern.compile("^(\\d+).\\s$"); @@ -570,15 +563,14 @@ public class MarkdownUtil { public static String removeMarkdown(@Nullable String s) { if (TextUtils.isEmpty(s)) return ""; - s = s.trim(); // Create HTML string from Markup String html = renderer.render(parser.parse(replaceCheckboxesWithEmojis(s))); // Convert Spanned from HTML. Spanned spanned = HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_COMPACT); // Convert from spanned to string s = spanned.toString(); - // The default string has two additional \n in the end, the substring is used to delete this two \n. - s = s.substring(0, s.length() - 1); + // The default string has two additional \n in the end, the trim is used to delete this two \n. + s = s.trim(); return s; } }