Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add docs for workers #1432

Merged
merged 12 commits into from May 3, 2019
80 changes: 74 additions & 6 deletions docs/USING_ADVANCED.md
Expand Up @@ -14,12 +14,12 @@ marked(markdownString [,options] [,callback])

```js
// Create reference instance
var myMarked = require('marked');
var marked = require('marked');
styfle marked this conversation as resolved.
Show resolved Hide resolved

// 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,83 @@ 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 shut it down 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

```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.postMessage(markdownString);
```

### Web Worker

```js
// markedWorker.js

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

onmessage = (markdownString) => {
postMessage(marked(markdownString));
};
```

```html
<script>
var markedWorker = new Worker('markedWorker.js');

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

markedWorker.onmessage = (html) => {
clearTimeout(markedTimeout);
console.log(html);
};

markedWorker.postMessage(markdownString);
</script>
```
1 change: 1 addition & 0 deletions docs/index.html
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