-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move parsing logic to separate class on Android (#560)
- Loading branch information
Showing
6 changed files
with
117 additions
and
87 deletions.
There are no files selected for viewing
This file contains 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,35 @@ | ||
#include "MarkdownParser.h" | ||
#include "MarkdownGlobal.h" | ||
|
||
#include <fbjni/fbjni.h> | ||
|
||
using namespace facebook; | ||
|
||
namespace expensify { | ||
namespace livemarkdown { | ||
jni::local_ref<jni::JString> MarkdownParser::nativeParse( | ||
jni::alias_ref<jhybridobject> jThis, | ||
jni::alias_ref<jni::JString> text, | ||
const int parserId) { | ||
static std::mutex workletRuntimeMutex; // this needs to be global since the worklet runtime is also global | ||
const auto lock = std::lock_guard<std::mutex>(workletRuntimeMutex); | ||
|
||
const auto markdownRuntime = expensify::livemarkdown::getMarkdownRuntime(); | ||
jsi::Runtime &rt = markdownRuntime->getJSIRuntime(); | ||
|
||
const auto markdownWorklet = expensify::livemarkdown::getMarkdownWorklet(parserId); | ||
|
||
const auto input = jsi::String::createFromUtf8(rt, text->toStdString()); | ||
const auto output = markdownRuntime->runGuarded(markdownWorklet, input); | ||
|
||
const auto json = rt.global().getPropertyAsObject(rt, "JSON").getPropertyAsFunction(rt, "stringify").call(rt, output).asString(rt).utf8(rt); | ||
return jni::make_jstring(json); | ||
} | ||
|
||
void MarkdownParser::registerNatives() { | ||
registerHybrid({ | ||
makeNativeMethod("nativeParse", MarkdownParser::nativeParse)}); | ||
} | ||
|
||
} // namespace livemarkdown | ||
} // namespace expensify |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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
71 changes: 71 additions & 0 deletions
71
android/src/main/java/com/expensify/livemarkdown/MarkdownParser.java
This file contains 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,71 @@ | ||
package com.expensify.livemarkdown; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.facebook.react.bridge.ReactContext; | ||
import com.facebook.react.util.RNLog; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class MarkdownParser { | ||
private final @NonNull ReactContext mReactContext; | ||
private String mPrevText; | ||
private int mPrevParserId; | ||
private List<MarkdownRange> mPrevMarkdownRanges; | ||
|
||
public MarkdownParser(@NonNull ReactContext reactContext) { | ||
mReactContext = reactContext; | ||
} | ||
|
||
private native String nativeParse(String text, int parserId); | ||
|
||
public synchronized List<MarkdownRange> parse(String text, int parserId) { | ||
if (text.equals(mPrevText) && parserId == mPrevParserId) { | ||
return mPrevMarkdownRanges; | ||
} | ||
|
||
String json; | ||
try { | ||
json = nativeParse(text, parserId); | ||
} catch (Exception e) { | ||
// Skip formatting, runGuarded will show the error in LogBox | ||
mPrevText = text; | ||
mPrevParserId = parserId; | ||
mPrevMarkdownRanges = Collections.emptyList(); | ||
return mPrevMarkdownRanges; | ||
} | ||
|
||
List<MarkdownRange> markdownRanges = new LinkedList<>(); | ||
try { | ||
JSONArray ranges = new JSONArray(json); | ||
for (int i = 0; i < ranges.length(); i++) { | ||
JSONObject range = ranges.getJSONObject(i); | ||
String type = range.getString("type"); | ||
int start = range.getInt("start"); | ||
int length = range.getInt("length"); | ||
int depth = range.optInt("depth", 1); | ||
if (length == 0 || start + length > text.length()) { | ||
continue; | ||
} | ||
markdownRanges.add(new MarkdownRange(type, start, length, depth)); | ||
} | ||
} catch (JSONException e) { | ||
RNLog.w(mReactContext, "[react-native-live-markdown] Incorrect schema of worklet parser output: " + e.getMessage()); | ||
mPrevText = text; | ||
mPrevParserId = parserId; | ||
mPrevMarkdownRanges = Collections.emptyList(); | ||
return mPrevMarkdownRanges; | ||
} | ||
|
||
mPrevText = text; | ||
mPrevParserId = parserId; | ||
mPrevMarkdownRanges = markdownRanges; | ||
return mPrevMarkdownRanges; | ||
} | ||
} |
This file contains 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