Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ Where:
* **[Ayowel](https://github.com/ayowel)**<br>~° Mermaid update
* **[Matthew White](https://github.com/mehw)**<br>~° Zim-Wiki link/attachment conformance
* **[Markus Paintner](https://github.com/goli4thus)**<br/>~° Added duplicate lines action
* **[Sebastian Safari](https://ssebs.com/)**<br/>~° Add text styling to the todo widget when using Markdown
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());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class TodoWidgetRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {

Expand Down Expand Up @@ -56,7 +57,19 @@ public int getCount() {
@Override
public RemoteViews getViewAt(int position) {
RemoteViews views = new RemoteViews(_context.getPackageName(), R.layout.todo_widget_list_item);
views.setTextViewText(R.id.todo_widget_item_text, _tasks.get(position).getDescription());

String itemText = _tasks.get(position).getDescription();

String filename = _document.path.toLowerCase(Locale.ROOT);
boolean isMarkdown = filename.endsWith(".md") || filename.endsWith(".markdown");
Comment thread
ssebs marked this conversation as resolved.
Resolved
if (isMarkdown) {
CharSequence styled = MarkdownWidgetUtils.fromFlexmark(itemText);
views.setTextViewText(R.id.todo_widget_item_text, styled);
} else {
views.setTextViewText(R.id.todo_widget_item_text, itemText);
}


views.setInt(R.id.todo_widget_item_text, "setTextColor", _appSettings.getEditorForegroundColor());

final Intent fillInIntent = new Intent()
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/res/layout/todo_widget_layout.xml
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>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/todo_widget_list_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
android:id="@+id/todo_widget_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:padding="2dp"
android:textSize="15sp"
android:textColor="@color/primary_text" />