Commit 37cabebe authored by Douwe Maan's avatar Douwe Maan

Address style feedback

parent dbfa58e2
/* eslint-disable class-methods-use-this */ /* eslint-disable class-methods-use-this */
/*jshint esversion: 6 */
(() => { (() => {
const gfmRules = { const gfmRules = {
// Should have an entry for every filter in lib/banzai/pipeline/gfm_pipeline.rb, // Should have an entry for every filter in lib/banzai/pipeline/gfm_pipeline.rb,
// in reverse order. // in reverse order.
// Should have test coverage in spec/features/copy_as_gfm_spec.rb. // Should have test coverage in spec/features/copy_as_gfm_spec.rb.
"InlineDiffFilter": { InlineDiffFilter: {
"span.idiff.addition": function(el, text) { 'span.idiff.addition'(el, text) {
return "{+" + text + "+}"; return `{+${text}+}`;
}, },
"span.idiff.deletion": function(el, text) { 'span.idiff.deletion'(el, text) {
return "{-" + text + "-}"; return `{-${text}-}`;
}, },
}, },
"TaskListFilter": { TaskListFilter: {
"input[type=checkbox].task-list-item-checkbox": function(el, text) { 'input[type=checkbox].task-list-item-checkbox'(el, text) {
return '[' + (el.checked ? 'x' : ' ') + ']'; return `[${el.checked ? 'x' : ' '}]`;
} }
}, },
"ReferenceFilter": { ReferenceFilter: {
"a.gfm:not([data-link=true])": function(el, text) { 'a.gfm:not([data-link=true])'(el, text) {
return el.getAttribute('data-original') || text; return el.dataset.original || text;
}, },
}, },
"AutolinkFilter": { AutolinkFilter: {
"a": function(el, text) { 'a'(el, text) {
if (text != el.getAttribute("href")) { // Fallback on the regular MarkdownFilter's `a` handler.
// Fall back to handler for MarkdownFilter if (text !== el.getAttribute('href')) return false;
return false;
}
return text; return text;
}, },
}, },
"TableOfContentsFilter": { TableOfContentsFilter: {
"ul.section-nav": function(el, text) { 'ul.section-nav'(el, text) {
return "[[_TOC_]]"; return '[[_TOC_]]';
}, },
}, },
"EmojiFilter": { EmojiFilter: {
"img.emoji": function(el, text) { 'img.emoji'(el, text) {
return el.getAttribute("alt"); return el.getAttribute('alt');
}, },
}, },
"ImageLinkFilter": { ImageLinkFilter: {
"a.no-attachment-icon": function(el, text) { 'a.no-attachment-icon'(el, text) {
return text; return text;
}, },
}, },
"VideoLinkFilter": { VideoLinkFilter: {
".video-container": function(el, text) { '.video-container'(el, text) {
var videoEl = el.querySelector('video'); let videoEl = el.querySelector('video');
if (!videoEl) { if (!videoEl) return false;
return false;
}
return CopyAsGFM.nodeToGFM(videoEl); return CopyAsGFM.nodeToGFM(videoEl);
}, },
"video": function(el, text) { 'video'(el, text) {
return "![" + el.getAttribute('data-title') + "](" + el.getAttribute("src") + ")"; return `![${el.dataset.title}](${el.getAttribute('src')})`;
}, },
}, },
"MathFilter": { MathFilter: {
"pre.code.math[data-math-style='display']": function(el, text) { 'pre.code.math[data-math-style=display]'(el, text) {
return "```math\n" + text.trim() + "\n```"; return '```math\n' + text.trim() + '\n```';
}, },
"code.code.math[data-math-style='inline']": function(el, text) { 'code.code.math[data-math-style=inline]'(el, text) {
return "$`" + text + "`$"; return '$`' + text + '`$';
}, },
"span.katex-display span.katex-mathml": function(el, text) { 'span.katex-display span.katex-mathml'(el, text) {
var mathAnnotation = el.querySelector('annotation[encoding="application/x-tex"]'); let mathAnnotation = el.querySelector('annotation[encoding="application/x-tex"]');
if (!mathAnnotation) { if (!mathAnnotation) return false;
return false;
}
return "```math\n" + CopyAsGFM.nodeToGFM(mathAnnotation) + "\n```"; return '```math\n' + CopyAsGFM.nodeToGFM(mathAnnotation) + '\n```';
}, },
"span.katex-mathml": function(el, text) { 'span.katex-mathml'(el, text) {
var mathAnnotation = el.querySelector('annotation[encoding="application/x-tex"]'); let mathAnnotation = el.querySelector('annotation[encoding="application/x-tex"]');
if (!mathAnnotation) { if (!mathAnnotation) return false;
return false;
}
return "$`" + CopyAsGFM.nodeToGFM(mathAnnotation) + "`$"; return '$`' + CopyAsGFM.nodeToGFM(mathAnnotation) + '`$';
}, },
"span.katex-html": function(el, text) { 'span.katex-html'(el, text) {
return ""; // 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(); return text.trim();
} }
}, },
"SyntaxHighlightFilter": { SyntaxHighlightFilter: {
"pre.code.highlight": function(el, text) { 'pre.code.highlight'(el, text) {
var lang = el.getAttribute("lang"); let lang = el.getAttribute('lang');
if (lang == "text") { if (lang === 'text') {
lang = ""; 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 `` // Don't wrap code blocks in ``
return text; return text;
}, },
}, },
"MarkdownFilter": { MarkdownFilter: {
"code": function(el, text) { 'code'(el, text) {
var backtickCount = 1; let backtickCount = 1;
var backtickMatch = text.match(/`+/); let backtickMatch = text.match(/`+/);
if (backtickMatch) { if (backtickMatch) {
backtickCount = backtickMatch[0].length + 1; backtickCount = backtickMatch[0].length + 1;
} }
var backticks = new Array(backtickCount + 1).join('`'); let backticks = new Array(backtickCount + 1).join('`');
var spaceOrNoSpace = backtickCount > 1 ? " " : ""; let spaceOrNoSpace = backtickCount > 1 ? ' ' : '';
return backticks + spaceOrNoSpace + text + spaceOrNoSpace + backticks; return backticks + spaceOrNoSpace + text + spaceOrNoSpace + backticks;
}, },
"blockquote": function(el, text) { 'blockquote'(el, text) {
return text.trim().split('\n').map(function(s) { return ('> ' + s).trim(); }).join('\n'); return text.trim().split('\n').map((s) => (`> ${s}`).trim()).join('\n');
}, },
"img": function(el, text) { 'img'(el, text) {
return "![" + el.getAttribute("alt") + "](" + el.getAttribute("src") + ")"; 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; return text;
}, },
"a": function(el, text) { 'a'(el, text) {
return "[" + text + "](" + el.getAttribute("href") + ")"; return `[${text}](${el.getAttribute('href')})`;
}, },
"li": function(el, text) { 'li'(el, text) {
var lines = text.trim().split('\n'); let lines = text.trim().split('\n');
var firstLine = '- ' + lines.shift(); let firstLine = '- ' + lines.shift();
var nextLines = lines.map(function(s) { return (' ' + s).replace(/\s+$/, ''); }); // 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; 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. '); return text.replace(/^- /mg, '1. ');
}, },
"h1": function(el, text) { 'h1'(el, text) {
return '# ' + text.trim(); return `# ${text.trim()}`;
}, },
"h2": function(el, text) { 'h2'(el, text) {
return '## ' + text.trim(); return `## ${text.trim()}`;
}, },
"h3": function(el, text) { 'h3'(el, text) {
return '### ' + text.trim(); return `### ${text.trim()}`;
}, },
"h4": function(el, text) { 'h4'(el, text) {
return '#### ' + text.trim(); return `#### ${text.trim()}`;
}, },
"h5": function(el, text) { 'h5'(el, text) {
return '##### ' + text.trim(); return `##### ${text.trim()}`;
}, },
"h6": function(el, text) { 'h6'(el, text) {
return '###### ' + text.trim(); return `###### ${text.trim()}`;
}, },
"strong": function(el, text) { 'strong'(el, text) {
return '**' + text + '**'; return `**${text}**`;
}, },
"em": function(el, text) { 'em'(el, text) {
return '_' + text + '_'; return `_${text}_`;
}, },
"del": function(el, text) { 'del'(el, text) {
return '~~' + text + '~~'; return `~~${text}~~`;
}, },
"sup": function(el, text) { 'sup'(el, text) {
return '^' + text; return `^${text}`;
}, },
"hr": function(el, text) { 'hr'(el, text) {
return '-----'; return '-----';
}, },
"table": function(el, text) { 'table'(el, text) {
var theadText = CopyAsGFM.nodeToGFM(el.querySelector('thead')); let theadText = CopyAsGFM.nodeToGFM(el.querySelector('thead'));
var tbodyText = CopyAsGFM.nodeToGFM(el.querySelector('tbody')); let tbodyText = CopyAsGFM.nodeToGFM(el.querySelector('tbody'));
return theadText + tbodyText; return theadText + tbodyText;
}, },
"thead": function(el, text) { 'thead'(el, text) {
var cells = _.map(el.querySelectorAll('th'), function(cell) { let cells = _.map(el.querySelectorAll('th'), function(cell) {
var chars = CopyAsGFM.nodeToGFM(cell).trim().length; let chars = CopyAsGFM.nodeToGFM(cell).trim().length;
var before = ''; let before = '';
var after = ''; let after = '';
switch (cell.style.textAlign) { switch (cell.style.textAlign) {
case 'center': case 'center':
before = ':'; before = ':';
...@@ -201,17 +204,18 @@ ...@@ -201,17 +204,18 @@
chars = Math.max(chars, 0); chars = Math.max(chars, 0);
var middle = new Array(chars + 1).join('-'); let middle = new Array(chars + 1).join('-');
return before + middle + after; return before + middle + after;
}); });
return text + '| ' + cells.join(' | ') + ' |';
return text + `| ${cells.join(' | ')} |`;
}, },
"tr": function(el, text) { 'tr'(el, text) {
var cells = _.map(el.querySelectorAll('td, th'), function(cell) { let cells = _.map(el.querySelectorAll('td, th'), function(cell) {
return CopyAsGFM.nodeToGFM(cell).trim(); return CopyAsGFM.nodeToGFM(cell).trim();
}); });
return '| ' + cells.join(' | ') + ' |'; return `| ${cells.join(' | ')} |`;
}, },
} }
}; };
...@@ -223,29 +227,29 @@ ...@@ -223,29 +227,29 @@
} }
handleCopy(e) { handleCopy(e) {
var clipboardData = e.originalEvent.clipboardData; let clipboardData = e.originalEvent.clipboardData;
if (!clipboardData) return; if (!clipboardData) return;
if (!window.getSelection) return; if (!window.getSelection) return;
var selection = window.getSelection(); let selection = window.getSelection();
if (!selection.rangeCount || selection.rangeCount === 0) return; if (!selection.rangeCount || selection.rangeCount === 0) return;
var selectedDocument = selection.getRangeAt(0).cloneContents(); let selectedDocument = selection.getRangeAt(0).cloneContents();
if (!selectedDocument) return; if (!selectedDocument) return;
e.preventDefault(); e.preventDefault();
clipboardData.setData('text/plain', selectedDocument.textContent); clipboardData.setData('text/plain', selectedDocument.textContent);
var gfm = CopyAsGFM.nodeToGFM(selectedDocument); let gfm = CopyAsGFM.nodeToGFM(selectedDocument);
clipboardData.setData('text/x-gfm', gfm); clipboardData.setData('text/x-gfm', gfm);
} }
handlePaste(e) { handlePaste(e) {
var clipboardData = e.originalEvent.clipboardData; let clipboardData = e.originalEvent.clipboardData;
if (!clipboardData) return; if (!clipboardData) return;
var gfm = clipboardData.getData('text/x-gfm'); let gfm = clipboardData.getData('text/x-gfm');
if (!gfm) return; if (!gfm) return;
e.preventDefault(); e.preventDefault();
...@@ -255,36 +259,39 @@ ...@@ -255,36 +259,39 @@
insertText(target, text) { insertText(target, text) {
// Firefox doesn't support `document.execCommand('insertText', false, text);` on textareas // Firefox doesn't support `document.execCommand('insertText', false, text);` on textareas
var selectionStart = target.selectionStart;
var selectionEnd = target.selectionEnd; let selectionStart = target.selectionStart;
var value = target.value; let selectionEnd = target.selectionEnd;
var textBefore = value.substring(0, selectionStart); let value = target.value;
var textAfter = value.substring(selectionEnd, value.length);
var newText = textBefore + text + textAfter; let textBefore = value.substring(0, selectionStart);
let textAfter = value.substring(selectionEnd, value.length);
let newText = textBefore + text + textAfter;
target.value = newText; target.value = newText;
target.selectionStart = target.selectionEnd = selectionStart + text.length; target.selectionStart = target.selectionEnd = selectionStart + text.length;
} }
static nodeToGFM(node) { static nodeToGFM(node) {
if (node.nodeType == Node.TEXT_NODE) { if (node.nodeType === Node.TEXT_NODE) {
return node.textContent; 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; return text;
} }
for (var filter in gfmRules) { for (let filter in gfmRules) {
var rules = gfmRules[filter]; let rules = gfmRules[filter];
for (var selector in rules) { for (let selector in rules) {
var func = rules[selector]; let func = rules[selector];
if (!node.matches(selector)) continue; if (!node.matches(selector)) continue;
var result = func(node, text); let result = func(node, text);
if (result === false) continue; if (result === false) continue;
return result; return result;
...@@ -295,16 +302,16 @@ ...@@ -295,16 +302,16 @@
} }
static innerGFM(parentNode) { static innerGFM(parentNode) {
var nodes = parentNode.childNodes; let nodes = parentNode.childNodes;
var clonedParentNode = parentNode.cloneNode(true); let clonedParentNode = parentNode.cloneNode(true);
var clonedNodes = Array.prototype.slice.call(clonedParentNode.childNodes, 0); let clonedNodes = Array.prototype.slice.call(clonedParentNode.childNodes, 0);
for (var i = 0; i < nodes.length; i++) { for (let i = 0; i < nodes.length; i++) {
var node = nodes[i]; let node = nodes[i];
var clonedNode = clonedNodes[i]; let clonedNode = clonedNodes[i];
var text = this.nodeToGFM(node); let text = this.nodeToGFM(node);
clonedNode.parentNode.replaceChild(document.createTextNode(text), clonedNode); clonedNode.parentNode.replaceChild(document.createTextNode(text), clonedNode);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment