Skip to content

Commit

Permalink
conflict fix 2
Browse files Browse the repository at this point in the history
  • Loading branch information
x13machine committed May 21, 2019
2 parents c1c1cfa + 4704e29 commit 281b808
Show file tree
Hide file tree
Showing 188 changed files with 1,088 additions and 4,176 deletions.
16 changes: 8 additions & 8 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
"plugins": [
"standard"
],
"parserOptions": { "ecmaVersion": 5 },
"parserOptions": {
"ecmaVersion": 5,
"sourceType": "script"
},
"rules": {
"semi": ["error", "always"],
"indent": ["warn", 2, {
"VariableDeclarator": { "var": 2 },
"indent": ["error", 2, {
"SwitchCase": 1,
"VariableDeclarator": { "var": 2 },
"outerIIFEBody": 0
}],
"space-before-function-paren": "off",
"object-curly-spacing": "off",
"operator-linebreak": ["error", "before", { "overrides": { "=": "after" } }],
"space-before-function-paren": ["error", "never"],
"no-cond-assign": "off",
"no-useless-escape": "off",
"no-return-assign": "off",
"one-var": "off",
"no-control-regex": "off"
},
"env": {
"node": true,
"browser": true,
"amd": true,
"jasmine": true
"amd": true
}
}
2 changes: 1 addition & 1 deletion bin/marked
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function camelize(text) {

function handleError(err) {
if (err.code === 'ENOENT') {
console.error(`marked: output to ${err.path}: No such directory`);
console.error('marked: output to ' + err.path + ': No such directory');
return process.exit(1);
}
throw err;
Expand Down
90 changes: 84 additions & 6 deletions docs/USING_ADVANCED.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ marked(markdownString [,options] [,callback])

```js
// Create reference instance
var myMarked = require('marked');
const marked = require('marked');

// Set options
// `highlight` example uses `highlight.js`
myMarked.setOptions({
renderer: new myMarked.Renderer(),
marked.setOptions({
renderer: new marked.Renderer(),
highlight: function(code) {
return require('highlight.js').highlightAuto(code).value;
},
Expand All @@ -34,7 +34,7 @@ myMarked.setOptions({
});

// Compile
console.log(myMarked('I am using __markdown__.'));
console.log(marked(markdownString));
```

<h2 id="options">Options</h2>
Expand Down Expand Up @@ -64,15 +64,93 @@ console.log(myMarked('I am using __markdown__.'));
Unlike `highlight.js` the `pygmentize.js` library uses asynchronous highlighting. This example demonstrates that marked is agnostic when it comes to the highlighter you use.

```js
myMarked.setOptions({
marked.setOptions({
highlight: function(code, lang, callback) {
require('pygmentize-bundled') ({ lang: lang, format: 'html' }, code, function (err, result) {
callback(err, result.toString());
});
}
});

console.log(myMarked(markdownString));
console.log(marked(markdownString));
```

In both examples, `code` is a `string` representing the section of code to pass to the highlighter. In this example, `lang` is a `string` informing the highlighter what programming lnaguage to use for the `code` and `callback` is the `function` the asynchronous highlighter will call once complete.

<h2 id="workers">Workers</h2>

To prevent ReDoS attacks you can run marked on a worker and terminate it when parsing takes longer than usual.

Marked can be run in a [worker thread](https://nodejs.org/api/worker_threads.html) on a node server, or a [web worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) in a browser.

### Node Worker Thread

> 🚨 Node Worker Threads are [experimental](https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads) 🚨
>
> This implementation may change.
```js
// markedWorker.js

const marked = require('marked');
const { parentPort } = require('worker_threads');

parentPort.on('message', (markdownString) => {
parentPort.postMessage(marked(markdownString));
});
```

```js
// index.js

const { Worker } = require('worker_threads');
const markedWorker = new Worker('./markedWorker.js');

const markedTimeout = setTimeout(() => {
markedWorker.terminate();
throw new Error('Marked took too long!');
}, timeoutLimit);

markedWorker.on('message', (html) => {
clearTimeout(markedTimeout);
console.log(html);
markedWorker.terminate();
});

markedWorker.postMessage(markdownString);
```

### Web Worker

> **NOTE**: Web Workers send the payload from `postMessage` in an object with the payload in a `.data` property
```js
// markedWorker.js

importScripts('path/to/marked.min.js');

onmessage = (e) => {
const markdownString = e.data
postMessage(marked(markdownString));
};
```

```js
// script.js

const markedWorker = new Worker('./markedWorker.js');

const markedTimeout = setTimeout(() => {
markedWorker.terminate();
throw new Error('Marked took too long!');
}, timeoutLimit);

markedWorker.onmessage = (e) => {
clearTimeout(markedTimeout);
const html = e.data;
console.log(html);
markedWorker.terminate();
};

markedWorker.postMessage(markdownString);
```
63 changes: 39 additions & 24 deletions docs/demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals marked, unfetch, ES6Promise */
/* globals marked, unfetch, ES6Promise, Promise */

if (!window.Promise) {
window.Promise = ES6Promise;
Expand All @@ -7,7 +7,7 @@ if (!window.fetch) {
window.fetch = unfetch;
}

onunhandledrejection = function (e) {
onunhandledrejection = function(e) {
throw e.reason;
};

Expand Down Expand Up @@ -70,7 +70,7 @@ Promise.all([
setInitialText(),
setInitialVersion()
.then(setInitialOptions)
]).then(function () {
]).then(function() {
handleInputChange();
handleOutputChange();
checkForChanges();
Expand All @@ -84,8 +84,8 @@ function setInitialText() {
$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;
}
Expand All @@ -95,18 +95,18 @@ function setInitialText() {

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() {
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';
Expand All @@ -116,9 +116,24 @@ function setInitialVersion() {
$markedVerElem.appendChild(opt);
}
})
.then(function () {
.then(function() {
return fetch('https://github.com/gitapi/repos/markedjs/marked/commits')
.then(function(res) {
return res.json();
})
.then(function(json) {
markedVersions['master'] = 'https://cdn.jsdelivr.net/gh/markedjs/marked@' + json[0].sha + '/lib/marked.js';
})
.catch(function() {
// do nothing
// uses url without commit
});
})
.then(function() {
if (search.version) {
if (!markedVersions[search.version]) {
if (markedVersions[search.version]) {
return search.version;
} else {
var match = search.version.match(/^(\w+):(.+)$/);
if (match) {
switch (match[1]) {
Expand All @@ -127,7 +142,7 @@ function setInitialVersion() {
return search.version;
case 'pr':
return getPrCommit(match[2])
.then(function (commit) {
.then(function(commit) {
if (!commit) {
return 'master';
}
Expand All @@ -141,7 +156,7 @@ function setInitialVersion() {

return 'master';
})
.then(function (version) {
.then(function(version) {
$markedVerElem.value = version;
})
.then(updateVersion);
Expand Down Expand Up @@ -205,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');
Expand Down Expand Up @@ -256,12 +271,12 @@ function addCommitVersion(value, text, commit) {

function getPrCommit(pr) {
return fetch('https://github.com/gitapi/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
});
}
Expand All @@ -281,7 +296,7 @@ function setDefaultOptions() {
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;
}
Expand Down Expand Up @@ -360,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;

Expand Down Expand Up @@ -464,7 +479,7 @@ function messageWorker(message) {
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) {
Expand All @@ -485,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) {
Expand All @@ -511,7 +526,7 @@ function messageWorker(message) {
}

function workerTimeout(seconds) {
markedWorker.timeout = setTimeout(function () {
markedWorker.timeout = setTimeout(function() {
seconds++;
markedWorker.onerror('Marked has taken longer than ' + seconds + ' second' + (seconds > 1 ? 's' : '') + ' to respond...');
workerTimeout(seconds);
Expand Down
14 changes: 7 additions & 7 deletions docs/demo/worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals marked, unfetch, ES6Promise */
/* globals marked, unfetch, ES6Promise, Promise */
if (!self.Promise) {
self.importScripts('https://cdn.jsdelivr.net/npm/es6-promise/dist/es6-promise.js');
self.Promise = ES6Promise;
Expand All @@ -11,15 +11,15 @@ 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);
});
}
Expand Down Expand Up @@ -87,13 +87,13 @@ function loadVersion(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)();
Expand Down
1 change: 1 addition & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ <h1>Marked.js Documentation</h1>
<ul>
<li><a href="#/USING_ADVANCED.md#options">Options</a></li>
<li><a href="#/USING_ADVANCED.md#highlight">Highlighting</a></li>
<li><a href="#/USING_ADVANCED.md#workers">Workers</a></li>
</ul>
</li>
<li>
Expand Down
Loading

0 comments on commit 281b808

Please sign in to comment.