Skip to content

Commit 30af889

Browse files
authored
Merge pull request #4017 from mueller-ma/render-markdown
Render markdown files
2 parents d72bbfa + 128fa5d commit 30af889

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ buildscript {
2121
// Koin
2222
ioInsertKoin = "3.3.3"
2323

24+
// Markwon
25+
markwon = "4.6.2"
26+
2427
// Moshi
2528
comSquareupMoshi = '1.14.0'
2629

owncloudApp/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ dependencies {
5656
implementation "com.google.android.material:material:1.8.0"
5757
implementation "com.jakewharton:disklrucache:2.0.2"
5858

59+
// Markdown Preview
60+
implementation "io.noties.markwon:core:$markwon"
61+
implementation "io.noties.markwon:ext-tables:$markwon"
62+
implementation "io.noties.markwon:ext-strikethrough:$markwon"
63+
implementation "io.noties.markwon:ext-tasklist:$markwon"
64+
implementation "io.noties.markwon:html:$markwon"
65+
5966
// Tests
6067
testImplementation project(":owncloudTestUtil")
6168
testImplementation "androidx.arch.core:core-testing:$androidxArchCore"

owncloudApp/src/main/java/com/owncloud/android/ui/preview/PreviewTextFragment.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.owncloud.android.ui.preview;
2222

2323
import android.accounts.Account;
24+
import android.content.Context;
2425
import android.os.AsyncTask;
2526
import android.os.Bundle;
2627
import android.view.LayoutInflater;
@@ -48,6 +49,11 @@
4849
import com.owncloud.android.ui.dialog.LoadingDialog;
4950
import com.owncloud.android.ui.fragment.FileFragment;
5051
import com.owncloud.android.utils.PreferenceUtils;
52+
import io.noties.markwon.Markwon;
53+
import io.noties.markwon.ext.strikethrough.StrikethroughPlugin;
54+
import io.noties.markwon.ext.tables.TablePlugin;
55+
import io.noties.markwon.ext.tasklist.TaskListPlugin;
56+
import io.noties.markwon.html.HtmlPlugin;
5157
import timber.log.Timber;
5258

5359
import java.io.BufferedWriter;
@@ -180,7 +186,7 @@ public void onStart() {
180186

181187
private void loadAndShowTextPreview() {
182188
mTextLoadTask = new TextLoadAsyncTask(new WeakReference<>(mTextPreview));
183-
mTextLoadTask.execute(getFile().getStoragePath());
189+
mTextLoadTask.execute(getFile());
184190
}
185191

186192
/**
@@ -189,6 +195,7 @@ private void loadAndShowTextPreview() {
189195
private class TextLoadAsyncTask extends AsyncTask<Object, Void, StringWriter> {
190196
private final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
191197
private final WeakReference<TextView> mTextViewReference;
198+
private String mimeType;
192199

193200
private TextLoadAsyncTask(WeakReference<TextView> textView) {
194201
mTextViewReference = textView;
@@ -205,7 +212,9 @@ protected StringWriter doInBackground(java.lang.Object... params) {
205212
throw new IllegalArgumentException("The parameter to " + TextLoadAsyncTask.class.getName() + " must " +
206213
"be (1) the file location");
207214
}
208-
final String location = (String) params[0];
215+
final OCFile file = (OCFile) params[0];
216+
final String location = file.getStoragePath();
217+
mimeType = file.getMimeType();
209218

210219
FileInputStream inputStream = null;
211220
Scanner sc = null;
@@ -247,7 +256,20 @@ protected void onPostExecute(final StringWriter stringWriter) {
247256
final TextView textView = mTextViewReference.get();
248257

249258
if (textView != null) {
250-
textView.setText(new String(stringWriter.getBuffer()));
259+
String text = new String(stringWriter.getBuffer());
260+
if (mimeType.equals("text/markdown")) {
261+
Context context = textView.getContext();
262+
Markwon markwon = Markwon
263+
.builder(context)
264+
.usePlugin(TablePlugin.create(context))
265+
.usePlugin(StrikethroughPlugin.create())
266+
.usePlugin(TaskListPlugin.create(context))
267+
.usePlugin(HtmlPlugin.create())
268+
.build();
269+
markwon.setMarkdown(textView, text);
270+
} else {
271+
textView.setText(text);
272+
}
251273
textView.setVisibility(View.VISIBLE);
252274
}
253275

0 commit comments

Comments
 (0)