You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
console.log(myMarked('I am using __markdown__.'));
37
+
console.log(marked(markdownString));
38
38
```
39
39
40
40
<h2id="options">Options</h2>
@@ -64,15 +64,93 @@ console.log(myMarked('I am using __markdown__.'));
64
64
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.
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.
79
+
80
+
<h2id="workers">Workers</h2>
81
+
82
+
To prevent ReDoS attacks you can run marked on a worker and terminate it when parsing takes longer than usual.
83
+
84
+
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.
85
+
86
+
### Node Worker Thread
87
+
88
+
> 🚨 Node Worker Threads are [experimental](https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads) 🚨
89
+
>
90
+
> This implementation may change.
91
+
92
+
```js
93
+
// markedWorker.js
94
+
95
+
constmarked=require('marked');
96
+
const { parentPort } =require('worker_threads');
97
+
98
+
parentPort.on('message', (markdownString) => {
99
+
parentPort.postMessage(marked(markdownString));
100
+
});
101
+
```
102
+
103
+
```js
104
+
// index.js
105
+
106
+
const { Worker } =require('worker_threads');
107
+
constmarkedWorker=newWorker('./markedWorker.js');
108
+
109
+
constmarkedTimeout=setTimeout(() => {
110
+
markedWorker.terminate();
111
+
thrownewError('Marked took too long!');
112
+
}, timeoutLimit);
113
+
114
+
markedWorker.on('message', (html) => {
115
+
clearTimeout(markedTimeout);
116
+
console.log(html);
117
+
markedWorker.terminate();
118
+
});
119
+
120
+
markedWorker.postMessage(markdownString);
121
+
```
122
+
123
+
### Web Worker
124
+
125
+
> **NOTE**: Web Workers send the payload from `postMessage` in an object with the payload in a `.data` property
0 commit comments