Use Base64 to encode html when rendering Markdown files

This commit is contained in:
Javi Rueda 2020-12-29 11:30:05 +01:00
parent fb560f8f01
commit 2e0331305a

View file

@ -15,6 +15,7 @@ import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.util.Base64;
import android.view.ContextMenu; import android.view.ContextMenu;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
@ -92,8 +93,6 @@ import java.util.Map;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.lbry.browser.MainActivity; import io.lbry.browser.MainActivity;
import io.lbry.browser.R; import io.lbry.browser.R;
@ -2140,19 +2139,9 @@ public class FileViewFragment extends BaseFragment implements
ReadTextFileTask task = new ReadTextFileTask(filePath, new ReadTextFileTask.ReadTextFileHandler() { ReadTextFileTask task = new ReadTextFileTask(filePath, new ReadTextFileTask.ReadTextFileHandler() {
@Override @Override
public void onSuccess(String text) { public void onSuccess(String text) {
String html = buildMarkdownHtml(text);
if (webView != null) { if (webView != null) {
// Due to a change to Chrome, WebView only displays '#' -and everything after it- String html = buildMarkdownHtml(text);
// if it is '%23' instead. Problem appears in text like '#2' or #hashtags. webView.loadData(Base64.encodeToString(html.getBytes(), Base64.NO_PADDING), "text/html", "base64");
Pattern pattern = Pattern.compile("#(\\S+)");
Matcher matcher = pattern.matcher(html);
if (matcher.find()) {
html = html.replaceAll(pattern.toString(), "&%2335;" + matcher.group(1));
}
webView.loadData(html, "text/html", "utf-8");
} }
} }