Repository navigation
Add text styling to the todo widget when using Markdown #2696
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
Open
ssebs
wants to merge
15
commits into
gsantner:master
Choose a base branch
from
ssebs:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ab5d750
Add some text styling to the todo widget when using Markdown.
ssebs a5ef0ab
Add some text styling to the todo widget when using Markdown.
ssebs 59a5142
Cleaner rendering
ssebs b5c7f01
Cleanup
ssebs ae568a5
Only render Markdown if the file extension is .md
ssebs 0f4b577
Cleanup
ssebs c1b17e1
Merge branch 'master' into master
gsantner 0cb6d6d
reformat code
gsantner 6bc7fb4
Merge branch 'master' into master
ssebs 344af84
change way to confirm if MD file
ssebs 3de2ac9
Merge branch 'master' into master
gsantner ad81531
Merge branch 'master' into master
gsantner 62cb840
Merge branch 'master' into master
gsantner 7de5c0c
Merge branch 'master' into master
gsantner ffb4654
Merge branch 'master' into master
gsantner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
app/src/main/java/net/gsantner/markor/widget/MarkdownWidgetUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package net.gsantner.markor.widget; | ||
|
|
||
| import android.os.Build; | ||
| import android.text.Html; | ||
| import android.text.SpannableStringBuilder; | ||
| import android.text.Spanned; | ||
|
|
||
| import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension; | ||
| import com.vladsch.flexmark.html.HtmlRenderer; | ||
| import com.vladsch.flexmark.parser.Parser; | ||
|
|
||
| import java.util.Collections; | ||
|
|
||
| /** | ||
| * A utility class for converting Markdown to a {@link android.text.Spannable} for use in Android widgets. | ||
| * <p> | ||
| * This class provides methods to render a limited subset of Markdown, making it suitable for display | ||
| * within {@link android.widget.RemoteViews}. | ||
| */ | ||
| public final class MarkdownWidgetUtils { | ||
|
|
||
| /** | ||
| * Converts markdown → HTML using flexmark → Spanned → SpannableStringBuilder. | ||
| * Safe for RemoteViews (bold, italic, lists, headings, etc.). | ||
| */ | ||
| public static SpannableStringBuilder fromFlexmark(String markdown) { | ||
| if (markdown == null || markdown.isEmpty()) { | ||
| return new SpannableStringBuilder(""); | ||
| } | ||
|
|
||
| Parser parser = Parser.builder() | ||
| .extensions(Collections.singletonList(TaskListExtension.create())) | ||
| .build(); | ||
| HtmlRenderer renderer = HtmlRenderer.builder() | ||
| .extensions(Collections.singletonList(TaskListExtension.create())) | ||
| .build(); | ||
|
|
||
| String html = renderer.render(parser.parse(markdown)); | ||
|
|
||
| Spanned spanned; | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
| spanned = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY); | ||
| } else { | ||
| spanned = Html.fromHtml(html); | ||
| } | ||
|
|
||
| // Flexmark produces <p>…</p> blocks, so Html.fromHtml adds newlines. | ||
| // Trim trailing newlines to match old behavior. | ||
| SpannableStringBuilder out = new SpannableStringBuilder(spanned); | ||
| trimTrailingNewlines(out); | ||
|
|
||
| return out; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Helper to remove excessive newlines from Html.fromHtml output | ||
| */ | ||
| private static void trimTrailingNewlines(SpannableStringBuilder sb) { | ||
| while (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') { | ||
| sb.delete(sb.length() - 1, sb.length()); | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,24 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:id="@+id/todo_widget_container" | ||
| android:orientation="vertical" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| android:background="@color/background"> | ||
| android:background="@color/background" | ||
| android:orientation="vertical" | ||
| android:padding="8dp"> | ||
|
|
||
| <ListView | ||
| android:id="@+id/todo_widget_list_view" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| android:divider="@color/grey" | ||
| android:dividerHeight="0.5dp" /> | ||
| android:divider="@color/background" | ||
| android:dividerHeight="0dp" /> | ||
|
|
||
| <TextView | ||
| android:id="@+id/todo_widget_empty_view" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| android:text="Empty" | ||
| android:gravity="center" | ||
| android:text="Empty" | ||
| android:visibility="gone" /> | ||
| </LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.