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] 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; } }