This commit is contained in:
Cui 2021-05-12 17:11:46 +08:00
parent 3a075c838a
commit 1ccd74e288
2 changed files with 20 additions and 18 deletions

View file

@ -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"));

View file

@ -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;
// 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;
}
}