From 83400f6f477c3802e4fae68cc0f790e4ae2cb3d7 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Mon, 3 Jun 2019 21:43:04 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20markdown=20text=20expans?= =?UTF-8?q?ion=20behaviour=20when=20similar=20unexpanded=20text=20is=20pre?= =?UTF-8?q?sent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes https://github.com/TryGhost/Ghost/issues/10770 - ensure text expansions only occur on the last match before the cursor - fix subscript text expansion --- .../addon/options/text-expansions.js | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/ghost/admin/lib/koenig-editor/addon/options/text-expansions.js b/ghost/admin/lib/koenig-editor/addon/options/text-expansions.js index d3367f2481..f42de30588 100644 --- a/ghost/admin/lib/koenig-editor/addon/options/text-expansions.js +++ b/ghost/admin/lib/koenig-editor/addon/options/text-expansions.js @@ -168,14 +168,14 @@ function registerInlineMarkdownTextExpansions(editor) { } function matchStrongStar(editor, text) { - let matches = text.match(/(?:^|\s)\*\*([^\s*]+|[^\s*][^*]*[^\s])\*\*/); + let matches = text.match(/(?:^|\s)\*\*([^\s*]+|[^\s*][^*]*[^\s])\*\*$/); if (matches) { _addMarkdownMarkup(this, editor, matches, 'strong'); } } function matchStrongUnderscore(editor, text) { - let matches = text.match(/(?:^|\s)__([^\s_]+|[^\s_][^_]*[^\s])__/); + let matches = text.match(/(?:^|\s)__([^\s_]+|[^\s_][^_]*[^\s])__$/); if (matches) { _addMarkdownMarkup(this, editor, matches, 'strong'); } @@ -196,42 +196,51 @@ function registerInlineMarkdownTextExpansions(editor) { // input = " *foo*" // matches[0] = " *foo*" // matches[1] = "foo" - let matches = text.match(/(?:^|\s)\*([^\s*]+|[^\s*][^*]*[^\s])\*/); + let matches = text.match(/(?:^|\s)\*([^\s*]+|[^\s*][^*]*[^\s])\*$/); if (matches) { _addMarkdownMarkup(this, editor, matches, 'em'); } } function matchEmUnderscore(editor, text) { - let matches = text.match(/(?:^|\s)_([^\s_]+|[^\s_][^_]*[^\s])_/); + let matches = text.match(/(?:^|\s)_([^\s_]+|[^\s_][^_]*[^\s])_$/); if (matches) { _addMarkdownMarkup(this, editor, matches, 'em'); } } function matchSub(editor, text) { - let matches = text.match(/(?:^|[^~])~([^\s~]+|[^\s~][^~]*[^\s])~/); + let matches = text.match(/(^|[^~])~([^\s~]+|[^\s~][^~]*[^\s~])~$/); 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) { - let matches = text.match(/(?:^|\s)~~([^\s~]+|[^\s~][^~]*[^\s])~~/); + let matches = text.match(/(?:^|\s)~~([^\s~]+|[^\s~][^~]*[^\s])~~$/); if (matches) { _addMarkdownMarkup(this, editor, matches, 's'); } } function matchCode(editor, text) { - let matches = text.match(/(?:^|\s)`([^\s`]+|[^\s`][^`]*[^\s`])`/); + let matches = text.match(/(?:^|\s)`([^\s`]+|[^\s`][^`]*[^\s`])`$/); if (matches) { _addMarkdownMarkup(this, editor, matches, 'code'); } } function matchSup(editor, text) { - let matches = text.match(/\^([^\s^]+|[^\s^][^^]*[^\s^])\^/); + let matches = text.match(/\^([^\s^]+|[^\s^][^^]*[^\s^])\^$/); if (matches) { _addMarkdownMarkup(this, editor, matches, 'sup'); }