Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent italic or strikethrough emojis on Android #534

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.expensify.livemarkdown;

import org.json.JSONException;
import org.json.JSONObject;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import org.json.JSONException;
import org.json.JSONObject;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI, I also needed class MarkdownRange so I added it in #559. I hope you can reuse mine in this PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 692de7c

public class MarkdownRange {
public final String type;
public final int start;
public final int end;
public final int length;
public final int depth;

public MarkdownRange(String type, int start, int length, int depth) {
this.type = type;
this.start = start;
this.length = length;
this.end = start + length;
this.depth = depth;
}
}

81 changes: 65 additions & 16 deletions android/src/main/java/com/expensify/livemarkdown/MarkdownUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.TreeMap;

public class MarkdownUtils {
static {
Expand Down Expand Up @@ -66,37 +69,83 @@ public void setMarkdownStyle(@NonNull MarkdownStyle markdownStyle) {
mMarkdownStyle = markdownStyle;
}

public void applyMarkdownFormatting(SpannableStringBuilder ssb) {
Objects.requireNonNull(mMarkdownStyle, "mMarkdownStyle is null");
private void splitRangesOnEmojis(List<MarkdownRange> markdownRanges, String type) {
List<MarkdownRange> emojiRanges = new ArrayList<>();
for (MarkdownRange range : markdownRanges) {
if (range.type.equals("emoji")) {
emojiRanges.add(range);
}
}

removeSpans(ssb);
int i = 0;
int j = 0;
while (i < markdownRanges.size() && j < emojiRanges.size()) {
MarkdownRange currentRange = markdownRanges.get(i);
MarkdownRange emojiRange = emojiRanges.get(j);

String input = ssb.toString();
String output;
if (input.equals(mPrevInput)) {
output = mPrevOutput;
} else {
output = parseMarkdown(input);
mPrevInput = input;
mPrevOutput = output;
if (!currentRange.type.equals(type) || currentRange.end < emojiRange.start) {
i += 1;
Skalakid marked this conversation as resolved.
Show resolved Hide resolved
continue;
} else if (emojiRange.start >= currentRange.start && emojiRange.end <= currentRange.end) {
// Split range
MarkdownRange startRange = new MarkdownRange(currentRange.type, currentRange.start, emojiRange.start - currentRange.start, currentRange.depth);
MarkdownRange endRange = new MarkdownRange(currentRange.type, emojiRange.end, currentRange.end - emojiRange.end, currentRange.depth);

markdownRanges.add(i + 1, startRange);
markdownRanges.add(i + 2, endRange);
markdownRanges.remove(i);
i = i + 1;
}
j += 1;
}
}


private List<MarkdownRange> parseRanges(String rangesJSON, String innerText) {
List<MarkdownRange> markdownRanges = new ArrayList<>();
try {
JSONArray ranges = new JSONArray(output);
JSONArray ranges = new JSONArray(rangesJSON);
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);
int end = start + length;
if (length == 0 || end > input.length()) {

MarkdownRange markdownRange = new MarkdownRange(type, start, length, depth);
if (markdownRange.length == 0 || markdownRange.end > innerText.length()) {
continue;
}
applyRange(ssb, type, start, end, depth);
markdownRanges.add(markdownRange);
}
} catch (JSONException e) {
// Do nothing
return new ArrayList<>();
}
splitRangesOnEmojis(markdownRanges, "italic");
splitRangesOnEmojis(markdownRanges, "strikethrough");
return markdownRanges;
}



public void applyMarkdownFormatting(SpannableStringBuilder ssb) {
Objects.requireNonNull(mMarkdownStyle, "mMarkdownStyle is null");

removeSpans(ssb);

String input = ssb.toString();
String output;
if (input.equals(mPrevInput)) {
output = mPrevOutput;
} else {
output = parseMarkdown(input);
mPrevInput = input;
mPrevOutput = output;
}

List<MarkdownRange> ranges = parseRanges(output, input);
for (MarkdownRange range : ranges) {
applyRange(ssb, range.type, range.start, range.end, range.depth);
}
}

Expand Down
2 changes: 2 additions & 0 deletions parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ function getTagPriority(tag: string) {
return 2;
case 'h1':
return 1;
case 'emoji':
return -1;
default:
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion parser/react-native-live-markdown-parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions src/web/utils/blockUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ function addStyleToBlock(targetElement: HTMLElement, type: NodeType, markdownSty
node.style.textDecoration = 'line-through';
break;
case 'emoji':
Object.assign(node.style, {...markdownStyle.emoji, verticalAlign: 'middle'});
Object.assign(node.style, {
...markdownStyle.emoji,
verticalAlign: 'middle',
fontStyle: 'normal',
textDecoration: 'none',
display: 'inline-block',
});
break;
case 'mention-here':
Object.assign(node.style, markdownStyle.mentionHere);
Expand All @@ -49,7 +55,6 @@ function addStyleToBlock(targetElement: HTMLElement, type: NodeType, markdownSty
case 'pre':
Object.assign(node.style, markdownStyle.pre);
break;

case 'blockquote':
Object.assign(node.style, {
...markdownStyle.blockquote,
Expand Down
Loading