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
29 changes: 28 additions & 1 deletion packages/react/src/lib/contentEditableComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ 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;

// CommonMark: only underscore delimiters cannot be intra-word
if (marker === '_' || marker === '__') {
if (isAlphanumeric(prevChar)) return false;
if (isAlphanumeric(nextChar)) return false;
}

return true;
};

const appendMarkdown = (parent, text) => {
const tokens = [
['**', 'strong'],
Expand Down Expand Up @@ -91,7 +115,10 @@ const appendMarkdown = (parent, text) => {

if (token) {
const [marker, tagName] = token;
const end = text.indexOf(marker, index + marker.length);
let end = text.indexOf(marker, index + marker.length);
while (end !== -1 && !isValidEmphasis(marker, text, index, end)) {
end = text.indexOf(marker, end + marker.length);
}
if (end > index + marker.length) {
flush();
const formatted = document.createElement(tagName);
Expand Down