Skip to content
Open
Changes from all 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
25 changes: 24 additions & 1 deletion packages/react/src/lib/contentEditableComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ const findLink = (text, start) => {
};
};

const isAlphanumeric = (ch) => Boolean(ch && /[a-zA-Z0-9]/.test(ch));
const isWhitespace = (ch) => Boolean(ch && /\s/.test(ch));

const isValidEmphasis = (marker, text, start, end) => {
if (marker === '`' || marker === '~~') {
return true;
}
const prevChar = start > 0 ? text[start - 1] : '';
const firstInside = text[start + marker.length];
const lastInside = text[end - 1];
const nextChar =
end + marker.length < text.length ? text[end + marker.length] : '';

if (isWhitespace(firstInside) || isWhitespace(lastInside)) return false;
if (isAlphanumeric(prevChar)) return false;
if (isAlphanumeric(nextChar)) return false;

return true;
};

const appendMarkdown = (parent, text) => {
const tokens = [
['**', 'strong'],
Expand Down Expand Up @@ -92,7 +112,10 @@ const appendMarkdown = (parent, text) => {
if (token) {
const [marker, tagName] = token;
const end = text.indexOf(marker, index + marker.length);
if (end > index + marker.length) {
if (
end > index + marker.length &&
isValidEmphasis(marker, text, index, end)
) {
flush();
const formatted = document.createElement(tagName);
const content = text.slice(index + marker.length, end);
Expand Down