Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tatuya Kamada
gitlab-ce
Commits
37cabebe
Commit
37cabebe
authored
Jan 16, 2017
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Address style feedback
parent
dbfa58e2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
150 additions
and
143 deletions
+150
-143
app/assets/javascripts/copy_as_gfm.js.es6
app/assets/javascripts/copy_as_gfm.js.es6
+150
-143
No files found.
app/assets/javascripts/copy_as_gfm.js.es6
View file @
37cabebe
/* eslint-disable class-methods-use-this */
/*jshint esversion: 6 */
(() => {
const gfmRules = {
// Should have an entry for every filter in lib/banzai/pipeline/gfm_pipeline.rb,
// in reverse order.
// Should have test coverage in spec/features/copy_as_gfm_spec.rb.
"InlineDiffFilter"
: {
"span.idiff.addition": function
(el, text) {
return
"{+" + text + "+}"
;
InlineDiffFilter
: {
'span.idiff.addition'
(el, text) {
return
`{+${text}+}`
;
},
"span.idiff.deletion": function
(el, text) {
return
"{-" + text + "-}"
;
'span.idiff.deletion'
(el, text) {
return
`{-${text}-}`
;
},
},
"TaskListFilter"
: {
"input[type=checkbox].task-list-item-checkbox": function
(el, text) {
return
'[' + (el.checked ? 'x' : ' ') + ']'
;
TaskListFilter
: {
'input[type=checkbox].task-list-item-checkbox'
(el, text) {
return
`[${el.checked ? 'x' : ' '}]`
;
}
},
"ReferenceFilter"
: {
"a.gfm:not([data-link=true])": function
(el, text) {
return el.
getAttribute('data-original')
|| text;
ReferenceFilter
: {
'a.gfm:not([data-link=true])'
(el, text) {
return el.
dataset.original
|| text;
},
},
"AutolinkFilter": {
"a": function(el, text) {
if (text != el.getAttribute("href")) {
// Fall back to handler for MarkdownFilter
return false;
}
AutolinkFilter: {
'a'(el, text) {
// Fallback on the regular MarkdownFilter's `a` handler.
if (text !== el.getAttribute('href')) return false;
return text;
},
},
"TableOfContentsFilter"
: {
"ul.section-nav": function
(el, text) {
return
"[[_TOC_]]"
;
TableOfContentsFilter
: {
'ul.section-nav'
(el, text) {
return
'[[_TOC_]]'
;
},
},
"EmojiFilter"
: {
"img.emoji": function
(el, text) {
return el.getAttribute(
"alt"
);
EmojiFilter
: {
'img.emoji'
(el, text) {
return el.getAttribute(
'alt'
);
},
},
"ImageLinkFilter"
: {
"a.no-attachment-icon": function
(el, text) {
ImageLinkFilter
: {
'a.no-attachment-icon'
(el, text) {
return text;
},
},
"VideoLinkFilter": {
".video-container": function(el, text) {
var videoEl = el.querySelector('video');
if (!videoEl) {
return false;
}
VideoLinkFilter: {
'.video-container'(el, text) {
let videoEl = el.querySelector('video');
if (!videoEl) return false;
return CopyAsGFM.nodeToGFM(videoEl);
},
"video": function
(el, text) {
return
"![" + el.getAttribute('data-title') + "](" + el.getAttribute("src") + ")"
;
'video'
(el, text) {
return
`![${el.dataset.title}](${el.getAttribute('src')})`
;
},
},
"MathFilter"
: {
"pre.code.math[data-math-style='display']": function
(el, text) {
return
"```math\n" + text.trim() + "\n```"
;
MathFilter
: {
'pre.code.math[data-math-style=display]'
(el, text) {
return
'```math\n' + text.trim() + '\n```'
;
},
"code.code.math[data-math-style='inline']": function
(el, text) {
return
"$`" + text + "`$"
;
'code.code.math[data-math-style=inline]'
(el, text) {
return
'$`' + text + '`$'
;
},
"span.katex-display span.katex-mathml": function(el, text) {
var mathAnnotation = el.querySelector('annotation[encoding="application/x-tex"]');
if (!mathAnnotation) {
return false;
}
'span.katex-display span.katex-mathml'(el, text) {
let mathAnnotation = el.querySelector('annotation[encoding="application/x-tex"]');
if (!mathAnnotation) return false;
return
"```math\n" + CopyAsGFM.nodeToGFM(mathAnnotation) + "\n```"
;
return
'```math\n' + CopyAsGFM.nodeToGFM(mathAnnotation) + '\n```'
;
},
"span.katex-mathml": function(el, text) {
var mathAnnotation = el.querySelector('annotation[encoding="application/x-tex"]');
if (!mathAnnotation) {
return false;
}
'span.katex-mathml'(el, text) {
let mathAnnotation = el.querySelector('annotation[encoding="application/x-tex"]');
if (!mathAnnotation) return false;
return
"$`" + CopyAsGFM.nodeToGFM(mathAnnotation) + "`$"
;
return
'$`' + CopyAsGFM.nodeToGFM(mathAnnotation) + '`$'
;
},
"span.katex-html": function(el, text) {
return "";
'span.katex-html'(el, text) {
// We don't want to include the content of this element in the copied text.
return '';
},
'annotation[encoding="application/x-tex"]'
: function
(el, text) {
'annotation[encoding="application/x-tex"]'(el, text) {
return text.trim();
}
},
"SyntaxHighlightFilter"
: {
"pre.code.highlight": function
(el, text) {
var lang = el.getAttribute("lang"
);
if (lang ==
"text"
) {
lang =
""
;
SyntaxHighlightFilter
: {
'pre.code.highlight'
(el, text) {
let lang = el.getAttribute('lang'
);
if (lang ==
= 'text'
) {
lang =
''
;
}
return
"```" + lang + "\n" + text.trim() + "\n```"
;
return
'```' + lang + '\n' + text.trim() + '\n```'
;
},
"pre > code": function
(el, text) {
'pre > code'
(el, text) {
// Don't wrap code blocks in ``
return text;
},
},
"MarkdownFilter"
: {
"code": function
(el, text) {
var
backtickCount = 1;
var
backtickMatch = text.match(/`+/);
MarkdownFilter
: {
'code'
(el, text) {
let
backtickCount = 1;
let
backtickMatch = text.match(/`+/);
if (backtickMatch) {
backtickCount = backtickMatch[0].length + 1;
}
var
backticks = new Array(backtickCount + 1).join('`');
var spaceOrNoSpace = backtickCount > 1 ? " " : ""
;
let
backticks = new Array(backtickCount + 1).join('`');
let spaceOrNoSpace = backtickCount > 1 ? ' ' : ''
;
return backticks + spaceOrNoSpace + text + spaceOrNoSpace + backticks;
},
"blockquote": function
(el, text) {
return text.trim().split('\n').map(
function(s) { return ('> ' + s).trim(); }
).join('\n');
'blockquote'
(el, text) {
return text.trim().split('\n').map(
(s) => (`> ${s}`).trim()
).join('\n');
},
"img": function
(el, text) {
return
"![" + el.getAttribute("alt") + "](" + el.getAttribute("src") + ")"
;
'img'
(el, text) {
return
`![${el.getAttribute('alt')}](${el.getAttribute('src')})`
;
},
"a.anchor": function(el, text) {
'a.anchor'(el, text) {
// Don't render a Markdown link for the anchor link inside a heading
return text;
},
"a": function(el, text) {
return "[" + text + "](" + el.getAttribute("href") + ")";
},
"li": function(el, text) {
var lines = text.trim().split('\n');
var firstLine = '- ' + lines.shift();
var nextLines = lines.map(function(s) { return (' ' + s).replace(/\s+$/, ''); });
'a'(el, text) {
return `[${text}](${el.getAttribute('href')})`;
},
'li'(el, text) {
let lines = text.trim().split('\n');
let firstLine = '- ' + lines.shift();
// Add two spaces to the front of subsequent list items lines, or leave the line entirely blank.
let nextLines = lines.map(function(s) {
if (s.trim().length === 0) {
return '';
} else {
return ` ${s}`;
}
});
return
firstLine + '\n' + nextLines.join('\n')
;
return
`${firstLine}\n${nextLines.join('\n')}`
;
},
"ul": function
(el, text) {
'ul'
(el, text) {
return text;
},
"ol": function(el, text) {
'ol'(el, text) {
// LIs get a `- ` prefix by default, which we replace by `1. ` for ordered lists.
return text.replace(/^- /mg, '1. ');
},
"h1": function
(el, text) {
return
'# ' + text.trim()
;
'h1'
(el, text) {
return
`# ${text.trim()}`
;
},
"h2": function
(el, text) {
return
'## ' + text.trim()
;
'h2'
(el, text) {
return
`## ${text.trim()}`
;
},
"h3": function
(el, text) {
return
'### ' + text.trim()
;
'h3'
(el, text) {
return
`### ${text.trim()}`
;
},
"h4": function
(el, text) {
return
'#### ' + text.trim()
;
'h4'
(el, text) {
return
`#### ${text.trim()}`
;
},
"h5": function
(el, text) {
return
'##### ' + text.trim()
;
'h5'
(el, text) {
return
`##### ${text.trim()}`
;
},
"h6": function
(el, text) {
return
'###### ' + text.trim()
;
'h6'
(el, text) {
return
`###### ${text.trim()}`
;
},
"strong": function
(el, text) {
return
'**' + text + '**'
;
'strong'
(el, text) {
return
`**${text}**`
;
},
"em": function
(el, text) {
return
'_' + text + '_'
;
'em'
(el, text) {
return
`_${text}_`
;
},
"del": function
(el, text) {
return
'~~' + text + '~~'
;
'del'
(el, text) {
return
`~~${text}~~`
;
},
"sup": function
(el, text) {
return
'^' + text
;
'sup'
(el, text) {
return
`^${text}`
;
},
"hr": function
(el, text) {
'hr'
(el, text) {
return '-----';
},
"table": function
(el, text) {
var
theadText = CopyAsGFM.nodeToGFM(el.querySelector('thead'));
var
tbodyText = CopyAsGFM.nodeToGFM(el.querySelector('tbody'));
'table'
(el, text) {
let
theadText = CopyAsGFM.nodeToGFM(el.querySelector('thead'));
let
tbodyText = CopyAsGFM.nodeToGFM(el.querySelector('tbody'));
return theadText + tbodyText;
},
"thead": function
(el, text) {
var
cells = _.map(el.querySelectorAll('th'), function(cell) {
var
chars = CopyAsGFM.nodeToGFM(cell).trim().length;
'thead'
(el, text) {
let
cells = _.map(el.querySelectorAll('th'), function(cell) {
let
chars = CopyAsGFM.nodeToGFM(cell).trim().length;
var
before = '';
var
after = '';
let
before = '';
let
after = '';
switch (cell.style.textAlign) {
case 'center':
before = ':';
...
...
@@ -201,17 +204,18 @@
chars = Math.max(chars, 0);
var
middle = new Array(chars + 1).join('-');
let
middle = new Array(chars + 1).join('-');
return before + middle + after;
});
return text + '| ' + cells.join(' | ') + ' |';
return text + `| ${cells.join(' | ')} |`;
},
"tr": function
(el, text) {
var
cells = _.map(el.querySelectorAll('td, th'), function(cell) {
'tr'
(el, text) {
let
cells = _.map(el.querySelectorAll('td, th'), function(cell) {
return CopyAsGFM.nodeToGFM(cell).trim();
});
return
'| ' + cells.join(' | ') + ' |'
;
return
`| ${cells.join(' | ')} |`
;
},
}
};
...
...
@@ -223,29 +227,29 @@
}
handleCopy(e) {
var
clipboardData = e.originalEvent.clipboardData;
let
clipboardData = e.originalEvent.clipboardData;
if (!clipboardData) return;
if (!window.getSelection) return;
var
selection = window.getSelection();
let
selection = window.getSelection();
if (!selection.rangeCount || selection.rangeCount === 0) return;
var
selectedDocument = selection.getRangeAt(0).cloneContents();
let
selectedDocument = selection.getRangeAt(0).cloneContents();
if (!selectedDocument) return;
e.preventDefault();
clipboardData.setData('text/plain', selectedDocument.textContent);
var
gfm = CopyAsGFM.nodeToGFM(selectedDocument);
let
gfm = CopyAsGFM.nodeToGFM(selectedDocument);
clipboardData.setData('text/x-gfm', gfm);
}
handlePaste(e) {
var
clipboardData = e.originalEvent.clipboardData;
let
clipboardData = e.originalEvent.clipboardData;
if (!clipboardData) return;
var
gfm = clipboardData.getData('text/x-gfm');
let
gfm = clipboardData.getData('text/x-gfm');
if (!gfm) return;
e.preventDefault();
...
...
@@ -255,36 +259,39 @@
insertText(target, text) {
// Firefox doesn't support `document.execCommand('insertText', false, text);` on textareas
var selectionStart = target.selectionStart;
var selectionEnd = target.selectionEnd;
var value = target.value;
var textBefore = value.substring(0, selectionStart);
var textAfter = value.substring(selectionEnd, value.length);
var newText = textBefore + text + textAfter;
let selectionStart = target.selectionStart;
let selectionEnd = target.selectionEnd;
let value = target.value;
let textBefore = value.substring(0, selectionStart);
let textAfter = value.substring(selectionEnd, value.length);
let newText = textBefore + text + textAfter;
target.value = newText;
target.selectionStart = target.selectionEnd = selectionStart + text.length;
}
static nodeToGFM(node) {
if (node.nodeType == Node.TEXT_NODE) {
if (node.nodeType ==
=
Node.TEXT_NODE) {
return node.textContent;
}
var
text = this.innerGFM(node);
let
text = this.innerGFM(node);
if (node.nodeType == Node.DOCUMENT_FRAGMENT_NODE) {
if (node.nodeType ==
=
Node.DOCUMENT_FRAGMENT_NODE) {
return text;
}
for (
var
filter in gfmRules) {
var
rules = gfmRules[filter];
for (
let
filter in gfmRules) {
let
rules = gfmRules[filter];
for (
var
selector in rules) {
var
func = rules[selector];
for (
let
selector in rules) {
let
func = rules[selector];
if (!node.matches(selector)) continue;
var
result = func(node, text);
let
result = func(node, text);
if (result === false) continue;
return result;
...
...
@@ -295,16 +302,16 @@
}
static innerGFM(parentNode) {
var
nodes = parentNode.childNodes;
let
nodes = parentNode.childNodes;
var
clonedParentNode = parentNode.cloneNode(true);
var
clonedNodes = Array.prototype.slice.call(clonedParentNode.childNodes, 0);
let
clonedParentNode = parentNode.cloneNode(true);
let
clonedNodes = Array.prototype.slice.call(clonedParentNode.childNodes, 0);
for (
var
i = 0; i < nodes.length; i++) {
var
node = nodes[i];
var
clonedNode = clonedNodes[i];
for (
let
i = 0; i < nodes.length; i++) {
let
node = nodes[i];
let
clonedNode = clonedNodes[i];
var
text = this.nodeToGFM(node);
let
text = this.nodeToGFM(node);
clonedNode.parentNode.replaceChild(document.createTextNode(text), clonedNode);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment