Skip to content

Commit

Permalink
Introduce MarkdownRange class on Android (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw authored Dec 4, 2024
1 parent 6aafc0f commit c0dbced
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.expensify.livemarkdown;

public class MarkdownRange {
private final String mType;
private final int mStart;
private final int mLength;
private final int mDepth;

public MarkdownRange(String type, int start, int length, int depth) {
mType = type;
mStart = start;
mLength = length;
mDepth = depth;
}

public String getType() {
return mType;
}

public int getStart() {
return mStart;
}

public int getLength() {
return mLength;
}

public int getDepth() {
return mDepth;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

public class MarkdownUtils {
Expand Down Expand Up @@ -68,6 +70,7 @@ public void applyMarkdownFormatting(SpannableStringBuilder ssb) {
mPrevParserId = mParserId;
}

List<MarkdownRange> markdownRanges = new LinkedList<>();
try {
JSONArray ranges = new JSONArray(output);
for (int i = 0; i < ranges.length(); i++) {
Expand All @@ -80,14 +83,21 @@ public void applyMarkdownFormatting(SpannableStringBuilder ssb) {
if (length == 0 || end > input.length()) {
continue;
}
applyRange(ssb, type, start, end, depth);
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());
}

for (MarkdownRange markdownRange : markdownRanges) {
applyRange(ssb, markdownRange);
}
}

private void applyRange(SpannableStringBuilder ssb, String type, int start, int end, int depth) {
private void applyRange(SpannableStringBuilder ssb, MarkdownRange markdownRange) {
String type = markdownRange.getType();
int start = markdownRange.getStart();
int end = start + markdownRange.getLength();
switch (type) {
case "bold":
setSpan(ssb, new MarkdownBoldSpan(), start, end);
Expand Down Expand Up @@ -149,7 +159,7 @@ private void applyRange(SpannableStringBuilder ssb, String type, int start, int
mMarkdownStyle.getBlockquoteBorderWidth(),
mMarkdownStyle.getBlockquoteMarginLeft(),
mMarkdownStyle.getBlockquotePaddingLeft(),
depth);
markdownRange.getDepth());
setSpan(ssb, span, start, end);
break;
}
Expand Down

0 comments on commit c0dbced

Please sign in to comment.