diff options
author | Douwe Maan <douwe@selenight.nl> | 2017-05-19 06:17:59 +0300 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2017-05-19 15:26:28 +0300 |
commit | 7e0a4c5e577769dddc4aec1cfec1db3064a3ff42 (patch) | |
tree | 822ac0d55c6eeab802c144dbe86c60da9665c4a0 /app/assets/javascripts/copy_as_gfm.js | |
parent | 36ede00876aacd0005eac8e9b4779e3014519e0c (diff) |
Don't wrap pasted code when it's already inside code tags
Diffstat (limited to 'app/assets/javascripts/copy_as_gfm.js')
-rw-r--r-- | app/assets/javascripts/copy_as_gfm.js | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/app/assets/javascripts/copy_as_gfm.js b/app/assets/javascripts/copy_as_gfm.js index 570799c030e..9d559b15cd4 100644 --- a/app/assets/javascripts/copy_as_gfm.js +++ b/app/assets/javascripts/copy_as_gfm.js @@ -299,12 +299,29 @@ class CopyAsGFM { const clipboardData = e.originalEvent.clipboardData; if (!clipboardData) return; + const text = clipboardData.getData('text/plain'); const gfm = clipboardData.getData('text/x-gfm'); if (!gfm) return; e.preventDefault(); - window.gl.utils.insertText(e.target, gfm); + window.gl.utils.insertText(e.target, (textBefore, textAfter) => { + // If the text before the cursor contains an odd number of backticks, + // we are either inside an inline code span that starts with 1 backtick + // or a code block that starts with 3 backticks. + // This logic still holds when there are one or more _closed_ code spans + // or blocks that will have 2 or 6 backticks. + // This will break down when the actual code block contains an uneven + // number of backticks, but this is a rare edge case. + const backtickMatch = textBefore.match(/`/g); + const insideCodeBlock = backtickMatch && (backtickMatch.length % 2) === 1; + + if (insideCodeBlock) { + return text; + } + + return gfm; + }); } static transformGFMSelection(documentFragment) { |