diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-03-08 12:17:44 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-03-08 12:17:44 +0300 |
commit | 60028378dd5e5e7844810e4a2aa2934a58f738ca (patch) | |
tree | 61db78141f7684741a0ed22ac5b43361c571f4db /app/assets/javascripts/lib | |
parent | 8986fe34837f7af1df90482c049a1578610cdb94 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/lib')
-rw-r--r-- | app/assets/javascripts/lib/utils/text_markdown.js | 52 |
1 files changed, 46 insertions, 6 deletions
diff --git a/app/assets/javascripts/lib/utils/text_markdown.js b/app/assets/javascripts/lib/utils/text_markdown.js index a4accf0ee8f..ac2eb34260c 100644 --- a/app/assets/javascripts/lib/utils/text_markdown.js +++ b/app/assets/javascripts/lib/utils/text_markdown.js @@ -9,7 +9,7 @@ const LINK_TAG_PATTERN = '[{text}](url)'; // a bullet point character (*+-) and an optional checkbox ([ ] [x]) // OR a number with a . after it and an optional checkbox ([ ] [x]) // followed by one or more whitespace characters -const LIST_LINE_HEAD_PATTERN = /^(?<indent>\s*)(?<leader>((?<isOl>[*+-])|(?<isUl>\d+\.))( \[([x ])\])?\s)(?<content>.)?/; +const LIST_LINE_HEAD_PATTERN = /^(?<indent>\s*)(?<leader>((?<isUl>[*+-])|(?<isOl>\d+\.))( \[([x ])\])?\s)(?<content>.)?/; function selectedText(text, textarea) { return text.substring(textarea.selectionStart, textarea.selectionEnd); @@ -31,8 +31,19 @@ function lineBefore(text, textarea, trimNewlines = true) { return split[split.length - 1]; } -function lineAfter(text, textarea) { - return text.substring(textarea.selectionEnd).trim().split('\n')[0]; +function lineAfter(text, textarea, trimNewlines = true) { + let split = text.substring(textarea.selectionEnd); + + if (trimNewlines) { + split = split.trim(); + } else { + // remove possible leading newline to get at the real line + split = split.replace(/^\n/, ''); + } + + split = split.split('\n'); + + return split[0]; } function convertMonacoSelectionToAceFormat(sel) { @@ -329,6 +340,25 @@ function handleSurroundSelectedText(e, textArea) { } /* eslint-enable @gitlab/require-i18n-strings */ +/** + * Returns the content for a new line following a list item. + * + * @param {Object} result - regex match of the current line + * @param {Object?} nextLineResult - regex match of the next line + * @returns string with the new list item + */ +function continueOlText(result, nextLineResult) { + const { indent, leader } = result.groups; + const { indent: nextIndent, isOl: nextIsOl } = nextLineResult?.groups ?? {}; + + const [numStr, postfix = ''] = leader.split('.'); + + const incrementBy = nextIsOl && nextIndent === indent ? 0 : 1; + const num = parseInt(numStr, 10) + incrementBy; + + return `${indent}${num}.${postfix}`; +} + function handleContinueList(e, textArea) { if (!gon.features?.markdownContinueLists) return; if (!(e.key === 'Enter')) return; @@ -339,7 +369,7 @@ function handleContinueList(e, textArea) { const result = currentLine.match(LIST_LINE_HEAD_PATTERN); if (result) { - const { indent, content, leader } = result.groups; + const { leader, indent, content, isOl } = result.groups; const prevLineEmpty = !content; if (prevLineEmpty) { @@ -349,12 +379,22 @@ function handleContinueList(e, textArea) { return; } - const itemInsert = `${indent}${leader}`; + let itemToInsert; + + if (isOl) { + const nextLine = lineAfter(textArea.value, textArea, false); + const nextLineResult = nextLine.match(LIST_LINE_HEAD_PATTERN); + + itemToInsert = continueOlText(result, nextLineResult); + } else { + // isUl + itemToInsert = `${indent}${leader}`; + } e.preventDefault(); updateText({ - tag: itemInsert, + tag: itemToInsert, textArea, blockTag: '', wrap: false, |