0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

🐛 Fixed markdown text expansion behaviour when similar unexpanded text is present

closes https://github.com/TryGhost/Ghost/issues/10770

- ensure text expansions only occur on the last match before the cursor
- fix subscript text expansion
This commit is contained in:
Kevin Ansfield 2019-06-03 21:43:04 +01:00
parent f36bd730e8
commit 83400f6f47

View file

@ -168,14 +168,14 @@ function registerInlineMarkdownTextExpansions(editor) {
} }
function matchStrongStar(editor, text) { function matchStrongStar(editor, text) {
let matches = text.match(/(?:^|\s)\*\*([^\s*]+|[^\s*][^*]*[^\s])\*\*/); let matches = text.match(/(?:^|\s)\*\*([^\s*]+|[^\s*][^*]*[^\s])\*\*$/);
if (matches) { if (matches) {
_addMarkdownMarkup(this, editor, matches, 'strong'); _addMarkdownMarkup(this, editor, matches, 'strong');
} }
} }
function matchStrongUnderscore(editor, text) { function matchStrongUnderscore(editor, text) {
let matches = text.match(/(?:^|\s)__([^\s_]+|[^\s_][^_]*[^\s])__/); let matches = text.match(/(?:^|\s)__([^\s_]+|[^\s_][^_]*[^\s])__$/);
if (matches) { if (matches) {
_addMarkdownMarkup(this, editor, matches, 'strong'); _addMarkdownMarkup(this, editor, matches, 'strong');
} }
@ -196,42 +196,51 @@ function registerInlineMarkdownTextExpansions(editor) {
// input = " *foo*" // input = " *foo*"
// matches[0] = " *foo*" // matches[0] = " *foo*"
// matches[1] = "foo" // matches[1] = "foo"
let matches = text.match(/(?:^|\s)\*([^\s*]+|[^\s*][^*]*[^\s])\*/); let matches = text.match(/(?:^|\s)\*([^\s*]+|[^\s*][^*]*[^\s])\*$/);
if (matches) { if (matches) {
_addMarkdownMarkup(this, editor, matches, 'em'); _addMarkdownMarkup(this, editor, matches, 'em');
} }
} }
function matchEmUnderscore(editor, text) { function matchEmUnderscore(editor, text) {
let matches = text.match(/(?:^|\s)_([^\s_]+|[^\s_][^_]*[^\s])_/); let matches = text.match(/(?:^|\s)_([^\s_]+|[^\s_][^_]*[^\s])_$/);
if (matches) { if (matches) {
_addMarkdownMarkup(this, editor, matches, 'em'); _addMarkdownMarkup(this, editor, matches, 'em');
} }
} }
function matchSub(editor, text) { function matchSub(editor, text) {
let matches = text.match(/(?:^|[^~])~([^\s~]+|[^\s~][^~]*[^\s])~/); let matches = text.match(/(^|[^~])~([^\s~]+|[^\s~][^~]*[^\s~])~$/);
if (matches) { if (matches) {
_addMarkdownMarkup(this, editor, matches, 'sub'); // re-adjust the matches to remove the first matched char if it
// exists, otherwise our length calculations are off. This is
// different to other matchers because we match any char at the
// beginning rather than a blank space and need to allow ~~ for
// the strikethrough expansion
let newMatches = [
matches[1] ? matches[0].replace(matches[1], '').trim() : matches[0],
matches[2]
];
_addMarkdownMarkup(this, editor, newMatches, 'sub');
} }
} }
function matchStrikethrough(editor, text) { function matchStrikethrough(editor, text) {
let matches = text.match(/(?:^|\s)~~([^\s~]+|[^\s~][^~]*[^\s])~~/); let matches = text.match(/(?:^|\s)~~([^\s~]+|[^\s~][^~]*[^\s])~~$/);
if (matches) { if (matches) {
_addMarkdownMarkup(this, editor, matches, 's'); _addMarkdownMarkup(this, editor, matches, 's');
} }
} }
function matchCode(editor, text) { function matchCode(editor, text) {
let matches = text.match(/(?:^|\s)`([^\s`]+|[^\s`][^`]*[^\s`])`/); let matches = text.match(/(?:^|\s)`([^\s`]+|[^\s`][^`]*[^\s`])`$/);
if (matches) { if (matches) {
_addMarkdownMarkup(this, editor, matches, 'code'); _addMarkdownMarkup(this, editor, matches, 'code');
} }
} }
function matchSup(editor, text) { function matchSup(editor, text) {
let matches = text.match(/\^([^\s^]+|[^\s^][^^]*[^\s^])\^/); let matches = text.match(/\^([^\s^]+|[^\s^][^^]*[^\s^])\^$/);
if (matches) { if (matches) {
_addMarkdownMarkup(this, editor, matches, 'sup'); _addMarkdownMarkup(this, editor, matches, 'sup');
} }