Skip to content

Commit

Permalink
add docs for workers
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Mar 1, 2019
1 parent ae9484d commit 3ab43e4
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
80 changes: 74 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');
var 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,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 = function (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 = function (html) => {
clearTimeout(markedTimeout);
console.log(html);
});
markedWorker.postMessage(markdownString);
</script>
```
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

0 comments on commit 3ab43e4

Please sign in to comment.