-
-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to remove all checked items (fix #1521) #1773
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ | |
import static it.niedermann.android.markdown.MarkdownUtil.removeMarkdown; | ||
import static it.niedermann.android.markdown.MarkdownUtil.replaceCheckboxesWithEmojis; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Provides basic functionality for Note operations. | ||
* Created by stefan on 06.10.15. | ||
|
@@ -123,6 +126,29 @@ public static String getLineWithoutMarkdown(@NonNull String content, int lineNum | |
return line; | ||
} | ||
|
||
/** | ||
* Strips all lines beginning with a filled checkbox from the Markdown. | ||
* | ||
* @param content String | ||
* @return newContent String | ||
*/ | ||
@NonNull | ||
public static String getContentWithoutCheckedItems(@NonNull String content) { | ||
final Pattern pattern = Pattern.compile( | ||
"^ *[-+*] \\[[xX]\\].*$", | ||
Pattern.MULTILINE | ||
); | ||
final Matcher matcher = pattern.matcher(content); | ||
|
||
// We can't just replace the matcher with an empty string as this would | ||
// let a blank line. To be able to remove the blank line as well, we set | ||
// a random-ish token that we will then match with the EOL as well and | ||
// remove them all together. | ||
return matcher | ||
.replaceAll("RANDOM-ISH_TEXT_TO_REPLACE") | ||
.replaceAll("RANDOM-ISH_TEXT_TO_REPLACE\n?", ""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have a better idea, but this looks.... odd... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know but I could not find a better way either |
||
} | ||
|
||
@NonNull | ||
public static String extendCategory(@NonNull String category) { | ||
return category.replace("/", " / "); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,4 +99,40 @@ public void testGenerateNoteExcerpt_sdk_30() { | |
// content has markdown while titles markdown is already stripped | ||
assertEquals("Title Bar", NoteUtil.generateNoteExcerpt("# Title\n- Title\n- Bar", "Title")); | ||
} | ||
|
||
@Test | ||
public void testGetContentWithoutCheckedItems() { | ||
String newLine = System.getProperty("line.separator"); | ||
|
||
String multilineWithCheckboxes = "- [ ] Line A" | ||
.concat(newLine) | ||
.concat("- [x] Line B") | ||
.concat(newLine) | ||
.concat("- [X] Line C") | ||
.concat(newLine) | ||
.concat(newLine) | ||
.concat("* [x] Line D") | ||
.concat(newLine) | ||
.concat("* [ ] Line E") | ||
.concat(newLine) | ||
.concat("+ [x] Line F") | ||
.concat(newLine) | ||
.concat("+ [ ] Line G") | ||
.concat(newLine) | ||
.concat(" + [ ] G1") | ||
.concat(newLine) | ||
.concat(" + [x] G2"); | ||
|
||
String expected = "- [ ] Line A" | ||
.concat(newLine) | ||
.concat(newLine) | ||
.concat("* [ ] Line E") | ||
.concat(newLine) | ||
.concat("+ [ ] Line G") | ||
.concat(newLine) | ||
.concat(" + [ ] G1") | ||
.concat(newLine); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As stated above, there are very many cases of the |
||
|
||
assertEquals(expected, NoteUtil.getContentWithoutCheckedItems(multilineWithCheckboxes)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This omits some variants that could lead to undefined behavior, for example checkboxes within code blocks. You can get some inspiration how to handle codeblocks in the
nextcloud-commons
library, but beware: It ain't that easy. Codeblocks can have three or more backticks, for exampleThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I will!