diff --git a/.eslintrc.json b/.eslintrc.json index 220dfc0ac7..142e68f6a9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -23,6 +23,7 @@ "ignoreComments": false }], "operator-linebreak": ["error", "before", { "overrides": { "=": "after" } }], + "space-before-function-paren": ["error", "never"], "no-cond-assign": "off", "no-useless-escape": "off", "one-var": "off", @@ -150,7 +151,6 @@ "rest-spread-spacing": ["error", "never"], "semi-spacing": ["error", { "before": false, "after": true }], "space-before-blocks": ["error", "always"], - "space-before-function-paren": ["error", "always"], "space-in-parens": ["error", "never"], "space-infix-ops": "error", "space-unary-ops": ["error", { "words": true, "nonwords": false }], diff --git a/bin/marked b/bin/marked index c7676d7d2d..307db173b0 100755 --- a/bin/marked +++ b/bin/marked @@ -13,7 +13,7 @@ var fs = require('fs'), * Man Page */ -function help () { +function help() { var spawn = require('child_process').spawn; var options = { @@ -24,15 +24,15 @@ function help () { }; spawn('man', [path.resolve(__dirname, '/../man/marked.1')], options) - .on('error', function () { - fs.readFile(path.resolve(__dirname, '/../man/marked.1.txt'), 'utf8', function (err, data) { + .on('error', function() { + fs.readFile(path.resolve(__dirname, '/../man/marked.1.txt'), 'utf8', function(err, data) { if (err) throw err; console.log(data); }); }); } -function version () { +function version() { var pkg = require('../package.json'); console.log(pkg.version); } @@ -41,7 +41,7 @@ function version () { * Main */ -function main (argv, callback) { +function main(argv, callback) { var files = [], options = {}, input, @@ -51,7 +51,7 @@ function main (argv, callback) { tokens, opt; - function getarg () { + function getarg() { var arg = argv.shift(); if (arg.indexOf('--') === 0) { @@ -65,7 +65,7 @@ function main (argv, callback) { } else if (arg[0] === '-') { if (arg.length > 2) { // e.g. -abc - argv = arg.substring(1).split('').map(function (ch) { + argv = arg.substring(1).split('').map(function(ch) { return '-' + ch; }).concat(argv); arg = argv.shift(); @@ -128,7 +128,7 @@ function main (argv, callback) { } } - function getData (callback) { + function getData(callback) { if (!input) { if (files.length <= 2) { if (string) { @@ -141,7 +141,7 @@ function main (argv, callback) { return fs.readFile(input, 'utf8', callback); } - return getData(function (err, data) { + return getData(function(err, data) { if (err) return callback(err); data = tokens @@ -161,21 +161,21 @@ function main (argv, callback) { * Helpers */ -function getStdin (callback) { +function getStdin(callback) { var stdin = process.stdin, buff = ''; stdin.setEncoding('utf8'); - stdin.on('data', function (data) { + stdin.on('data', function(data) { buff += data; }); - stdin.on('error', function (err) { + stdin.on('error', function(err) { return callback(err); }); - stdin.on('end', function () { + stdin.on('end', function() { return callback(null, buff); }); @@ -186,13 +186,13 @@ function getStdin (callback) { } } -function camelize (text) { - return text.replace(/(\w)-(\w)/g, function (_, a, b) { +function camelize(text) { + return text.replace(/(\w)-(\w)/g, function(_, a, b) { return a + b.toUpperCase(); }); } -function handleError (err) { +function handleError(err) { if (err.code === 'ENOENT') { console.error('marked: output to ' + err.path + ': No such directory'); return process.exit(1); @@ -206,7 +206,7 @@ function handleError (err) { if (!module.parent) { process.title = 'marked'; - main(process.argv.slice(), function (err, code) { + main(process.argv.slice(), function(err, code) { if (err) return handleError(err); return process.exit(code || 0); }); diff --git a/docs/demo/demo.js b/docs/demo/demo.js index eb452d0a41..c54a3a244a 100644 --- a/docs/demo/demo.js +++ b/docs/demo/demo.js @@ -7,7 +7,7 @@ if (!window.fetch) { window.fetch = unfetch; } -onunhandledrejection = function (e) { +onunhandledrejection = function(e) { throw e.reason; }; @@ -70,7 +70,7 @@ Promise.all([ setInitialText(), setInitialVersion() .then(setInitialOptions) -]).then(function () { +]).then(function() { handleInputChange(); handleOutputChange(); checkForChanges(); @@ -79,13 +79,13 @@ Promise.all([ $mainElem.style.display = 'block'; }); -function setInitialText () { +function setInitialText() { if ('text' in search) { $markdownElem.value = search.text; } else { return fetch('./initial.md') - .then(function (res) { return res.text(); }) - .then(function (text) { + .then(function(res) { return res.text(); }) + .then(function(text) { if ($markdownElem.value === '') { $markdownElem.value = text; } @@ -93,20 +93,20 @@ function setInitialText () { } } -function setInitialQuickref () { +function setInitialQuickref() { return fetch('./quickref.md') - .then(function (res) { return res.text(); }) - .then(function (text) { + .then(function(res) { return res.text(); }) + .then(function(text) { document.querySelector('#quickref').value = text; }); } -function setInitialVersion () { +function setInitialVersion() { return fetch('https://data.jsdelivr.com/v1/package/npm/marked') - .then(function (res) { + .then(function(res) { return res.json(); }) - .then(function (json) { + .then(function(json) { for (var i = 0; i < json.versions.length; i++) { var ver = json.versions[i]; markedVersions[ver] = 'https://cdn.jsdelivr.net/npm/marked@' + ver + '/lib/marked.js'; @@ -116,20 +116,20 @@ function setInitialVersion () { $markedVerElem.appendChild(opt); } }) - .then(function () { + .then(function() { return fetch('https://api.github.com/repos/markedjs/marked/commits') - .then(function (res) { + .then(function(res) { return res.json(); }) - .then(function (json) { + .then(function(json) { markedVersions['master'] = 'https://cdn.jsdelivr.net/gh/markedjs/marked@' + json[0].sha + '/lib/marked.js'; }) - .catch(function () { + .catch(function() { // do nothing // uses url without commit }); }) - .then(function () { + .then(function() { if (search.version) { if (markedVersions[search.version]) { return search.version; @@ -142,7 +142,7 @@ function setInitialVersion () { return search.version; case 'pr': return getPrCommit(match[2]) - .then(function (commit) { + .then(function(commit) { if (!commit) { return 'master'; } @@ -156,13 +156,13 @@ function setInitialVersion () { return 'master'; }) - .then(function (version) { + .then(function(version) { $markedVerElem.value = version; }) .then(updateVersion); } -function setInitialOptions () { +function setInitialOptions() { if ('options' in search) { $optionsElem.value = search.options; } else { @@ -170,22 +170,22 @@ function setInitialOptions () { } } -function setInitialOutputType () { +function setInitialOutputType() { if (search.outputType) { $outputTypeElem.value = search.outputType; } } -function handleIframeLoad () { +function handleIframeLoad() { lastInput = ''; inputDirty = true; } -function handleInput () { +function handleInput() { inputDirty = true; }; -function handleVersionChange () { +function handleVersionChange() { if ($markedVerElem.value === 'commit' || $markedVerElem.value === 'pr') { $commitVerElem.style.display = ''; } else { @@ -194,14 +194,14 @@ function handleVersionChange () { } } -function handleClearClick () { +function handleClearClick() { $markdownElem.value = ''; $markedVerElem.value = 'master'; $commitVerElem.style.display = 'none'; updateVersion().then(setDefaultOptions); } -function handleAddVersion (e) { +function handleAddVersion(e) { if (e.which === 13) { switch ($markedVerElem.value) { case 'commit': @@ -220,7 +220,7 @@ function handleAddVersion (e) { $commitVerElem.disabled = true; var pr = $commitVerElem.value.replace(/\D/g, ''); getPrCommit(pr) - .then(function (commit) { + .then(function(commit) { $commitVerElem.disabled = false; if (!commit) { alert('That is not a valid PR'); @@ -236,16 +236,16 @@ function handleAddVersion (e) { } } -function handleInputChange () { +function handleInputChange() { handleChange($inputPanes, $inputTypeElem.value); } -function handleOutputChange () { +function handleOutputChange() { $activeOutputElem = handleChange($panes, $outputTypeElem.value); updateLink(); } -function handleChange (panes, visiblePane) { +function handleChange(panes, visiblePane) { var active = null; for (var i = 0; i < panes.length; i++) { if (panes[i].id === visiblePane) { @@ -258,7 +258,7 @@ function handleChange (panes, visiblePane) { return active; }; -function addCommitVersion (value, text, commit) { +function addCommitVersion(value, text, commit) { if (markedVersions[value]) { return; } @@ -269,19 +269,19 @@ function addCommitVersion (value, text, commit) { $markedVerElem.insertBefore(opt, $markedVerElem.firstChild); } -function getPrCommit (pr) { +function getPrCommit(pr) { return fetch('https://api.github.com/repos/markedjs/marked/pulls/' + pr + '/commits') - .then(function (res) { + .then(function(res) { return res.json(); }) - .then(function (json) { + .then(function(json) { return json[json.length - 1].sha; - }).catch(function () { + }).catch(function() { // return undefined }); } -function setDefaultOptions () { +function setDefaultOptions() { if (window.Worker) { messageWorker({ task: 'defaults', @@ -293,10 +293,10 @@ function setDefaultOptions () { } } -function setOptions (opts) { +function setOptions(opts) { $optionsElem.value = JSON.stringify( opts, - function (key, value) { + function(key, value) { if (value && typeof value === 'object' && Object.getPrototypeOf(value) !== Object.prototype) { return undefined; } @@ -304,7 +304,7 @@ function setOptions (opts) { }, ' '); } -function searchToObject () { +function searchToObject() { // modified from https://stackoverflow.com/a/7090123/806777 var pairs = location.search.slice(1).split('&'); var obj = {}; @@ -322,7 +322,7 @@ function searchToObject () { return obj; } -function jsonString (input) { +function jsonString(input) { var output = (input + '') .replace(/\n/g, '\\n') .replace(/\r/g, '\\r') @@ -333,13 +333,13 @@ function jsonString (input) { return '"' + output + '"'; }; -function getScrollSize () { +function getScrollSize() { var e = $activeOutputElem; return e.scrollHeight - e.clientHeight; }; -function getScrollPercent () { +function getScrollPercent() { var size = getScrollSize(); if (size <= 0) { @@ -349,11 +349,11 @@ function getScrollPercent () { return $activeOutputElem.scrollTop / size; }; -function setScrollPercent (percent) { +function setScrollPercent(percent) { $activeOutputElem.scrollTop = percent * getScrollSize(); }; -function updateLink () { +function updateLink() { var outputType = ''; if ($outputTypeElem.value !== 'preview') { outputType = 'outputType=' + $outputTypeElem.value + '&'; @@ -365,7 +365,7 @@ function updateLink () { history.replaceState('', document.title, $permalinkElem.href); } -function updateVersion () { +function updateVersion() { if (window.Worker) { handleInput(); return Promise.resolve(); @@ -375,13 +375,13 @@ function updateVersion () { promise = Promise.resolve(markedVersionCache[$markedVerElem.value]); } else { promise = fetch(markedVersions[$markedVerElem.value]) - .then(function (res) { return res.text(); }) - .then(function (text) { + .then(function(res) { return res.text(); }) + .then(function(text) { markedVersionCache[$markedVerElem.value] = text; return text; }); } - return promise.then(function (text) { + return promise.then(function(text) { var script = document.createElement('script'); script.textContent = text; @@ -390,7 +390,7 @@ function updateVersion () { }).then(handleInput); } -function checkForChanges () { +function checkForChanges() { if (inputDirty && $markedVerElem.value !== 'commit' && $markedVerElem.value !== 'pr' && (typeof marked !== 'undefined' || window.Worker)) { inputDirty = false; @@ -448,7 +448,7 @@ function checkForChanges () { checkChangeTimeout = window.setTimeout(checkForChanges, delayTime); }; -function setResponseTime (ms) { +function setResponseTime(ms) { var amount = ms; var suffix = 'ms'; if (ms > 1000 * 60 * 60) { @@ -464,7 +464,7 @@ function setResponseTime (ms) { $responseTimeElem.textContent = amount + suffix; } -function setParsed (parsed, lexed) { +function setParsed(parsed, lexed) { try { $previewIframe.contentDocument.body.innerHTML = parsed; } catch (ex) {} @@ -472,14 +472,14 @@ function setParsed (parsed, lexed) { $lexerElem.value = lexed; } -function messageWorker (message) { +function messageWorker(message) { if (!markedWorker || markedWorker.working) { if (markedWorker) { clearTimeout(markedWorker.timeout); markedWorker.terminate(); } markedWorker = new Worker('worker.js'); - markedWorker.onmessage = function (e) { + markedWorker.onmessage = function(e) { clearTimeout(markedWorker.timeout); markedWorker.working = false; switch (e.data.task) { @@ -500,7 +500,7 @@ function messageWorker (message) { delayTime = 10; checkForChanges(); }; - markedWorker.onerror = markedWorker.onmessageerror = function (err) { + markedWorker.onerror = markedWorker.onmessageerror = function(err) { clearTimeout(markedWorker.timeout); var error = 'There was an error in the Worker'; if (err) { @@ -525,8 +525,8 @@ function messageWorker (message) { markedWorker.postMessage(message); } -function workerTimeout (seconds) { - markedWorker.timeout = setTimeout(function () { +function workerTimeout(seconds) { + markedWorker.timeout = setTimeout(function() { seconds++; markedWorker.onerror('Marked has taken longer than ' + seconds + ' second' + (seconds > 1 ? 's' : '') + ' to respond...'); workerTimeout(seconds); diff --git a/docs/demo/worker.js b/docs/demo/worker.js index 34020ae875..01b6b7530c 100644 --- a/docs/demo/worker.js +++ b/docs/demo/worker.js @@ -11,21 +11,21 @@ if (!self.fetch) { var versionCache = {}; var currentVersion; -onunhandledrejection = function (e) { +onunhandledrejection = function(e) { throw e.reason; }; -onmessage = function (e) { +onmessage = function(e) { if (e.data.version === currentVersion) { parse(e); } else { - loadVersion(e.data.version).then(function () { + loadVersion(e.data.version).then(function() { parse(e); }); } }; -function parse (e) { +function parse(e) { switch (e.data.task) { case 'defaults': @@ -70,7 +70,7 @@ function parse (e) { } } -function jsonString (input) { +function jsonString(input) { var output = (input + '') .replace(/\n/g, '\\n') .replace(/\r/g, '\\r') @@ -81,19 +81,19 @@ function jsonString (input) { return '"' + output + '"'; }; -function loadVersion (ver) { +function loadVersion(ver) { var promise; if (versionCache[ver]) { promise = Promise.resolve(versionCache[ver]); } else { promise = fetch(ver) - .then(function (res) { return res.text(); }) - .then(function (text) { + .then(function(res) { return res.text(); }) + .then(function(text) { versionCache[ver] = text; return text; }); } - return promise.then(function (text) { + return promise.then(function(text) { try { // eslint-disable-next-line no-new-func Function(text)(); diff --git a/lib/marked.js b/lib/marked.js index a643f060d4..d9e3bafeb1 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -4,7 +4,7 @@ * https://github.com/markedjs/marked */ -;(function (root) { +;(function(root) { 'use strict'; /** @@ -133,7 +133,7 @@ block.pedantic = merge({}, block.normal, { * Block Lexer */ -function Lexer (options) { +function Lexer(options) { this.tokens = []; this.tokens.links = Object.create(null); this.options = options || marked.defaults; @@ -160,7 +160,7 @@ Lexer.rules = block; * Static Lex Method */ -Lexer.lex = function (src, options) { +Lexer.lex = function(src, options) { var lexer = new Lexer(options); return lexer.lex(src); }; @@ -169,7 +169,7 @@ Lexer.lex = function (src, options) { * Preprocessing */ -Lexer.prototype.lex = function (src) { +Lexer.prototype.lex = function(src) { src = src .replace(/\r\n|\r/g, '\n') .replace(/\t/g, ' ') @@ -183,7 +183,7 @@ Lexer.prototype.lex = function (src) { * Lexing */ -Lexer.prototype.token = function (src, top) { +Lexer.prototype.token = function(src, top) { src = src.replace(/^ +$/gm, ''); var next, loose, @@ -653,7 +653,7 @@ inline.breaks = merge({}, inline.gfm, { * Inline Lexer & Compiler */ -function InlineLexer (links, options) { +function InlineLexer(links, options) { this.options = options || marked.defaults; this.links = links; this.rules = inline.normal; @@ -685,7 +685,7 @@ InlineLexer.rules = inline; * Static Lexing/Compiling Method */ -InlineLexer.output = function (src, links, options) { +InlineLexer.output = function(src, links, options) { var inline = new InlineLexer(links, options); return inline.output(src); }; @@ -694,7 +694,7 @@ InlineLexer.output = function (src, links, options) { * Lexing/Compiling */ -InlineLexer.prototype.output = function (src) { +InlineLexer.prototype.output = function(src) { var out = '', link, text, @@ -874,7 +874,7 @@ InlineLexer.prototype.output = function (src) { return out; }; -InlineLexer.escapes = function (text) { +InlineLexer.escapes = function(text) { return text ? text.replace(InlineLexer.rules._escapes, '$1') : text; }; @@ -882,7 +882,7 @@ InlineLexer.escapes = function (text) { * Compile Link */ -InlineLexer.prototype.outputLink = function (cap, link) { +InlineLexer.prototype.outputLink = function(cap, link) { var href = link.href, title = link.title ? escape(link.title) : null; @@ -895,7 +895,7 @@ InlineLexer.prototype.outputLink = function (cap, link) { * Smartypants Transformations */ -InlineLexer.prototype.smartypants = function (text) { +InlineLexer.prototype.smartypants = function(text) { if (!this.options.smartypants) return text; return text // em-dashes @@ -918,7 +918,7 @@ InlineLexer.prototype.smartypants = function (text) { * Mangle Links */ -InlineLexer.prototype.mangle = function (text) { +InlineLexer.prototype.mangle = function(text) { if (!this.options.mangle) return text; var out = '', l = text.length, @@ -940,11 +940,11 @@ InlineLexer.prototype.mangle = function (text) { * Renderer */ -function Renderer (options) { +function Renderer(options) { this.options = options || marked.defaults; } -Renderer.prototype.code = function (code, infostring, escaped) { +Renderer.prototype.code = function(code, infostring, escaped) { var lang = (infostring || '').match(/\S*/)[0]; if (this.options.highlight) { var out = this.options.highlight(code, lang); @@ -968,15 +968,15 @@ Renderer.prototype.code = function (code, infostring, escaped) { + '\n'; }; -Renderer.prototype.blockquote = function (quote) { +Renderer.prototype.blockquote = function(quote) { return '
\n' + quote + '\n'; }; -Renderer.prototype.html = function (html) { +Renderer.prototype.html = function(html) { return html; }; -Renderer.prototype.heading = function (text, level, raw, slugger) { +Renderer.prototype.heading = function(text, level, raw, slugger) { if (this.options.headerIds) { return '
' + text + '
\n'; }; -Renderer.prototype.table = function (header, body) { +Renderer.prototype.table = function(header, body) { if (body) body = '' + body + ''; return '' + text + '
';
};
-Renderer.prototype.br = function () {
+Renderer.prototype.br = function() {
return this.options.xhtml ? '