Skip to content

Commit 4a57485

Browse files
authored
fix: Maximum call stack size exceeded (#315)
* fix: Maximum call stack size exceeded * test: increase timeout * refactor: simplify test assertions by using expected variables
1 parent 9d42f9e commit 4a57485

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

lib/renderer.js

+6
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ module.exports = function(data, options) {
225225
const { prependRoot, postAsset, dompurify } = markedCfg;
226226
const { path, text } = data;
227227

228+
marked.defaults.extensions = null;
229+
marked.defaults.tokenizer = null;
230+
marked.defaults.renderer = null;
231+
marked.defaults.hooks = null;
232+
marked.defaults.walkTokens = null;
233+
228234
// exec filter to extend marked
229235
this.execFilterSync('marked:use', marked.use, { context: this });
230236

test/index.js

+42-12
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,21 @@ describe('Marked renderer', () => {
331331
].join('\n');
332332

333333
it('autolink enabled', () => {
334-
const result = r({text: body});
335-
336-
result.should.eql([
334+
let result = r({text: body});
335+
const expected = [
337336
'<p>Great website <a href="http://hexo.io/">http://hexo.io</a></p>',
338337
'<p>A webpage <a href="http://www.example.com/">www.example.com</a></p>',
339338
'<p><a href="http://hexo.io/">Hexo</a></p>',
340339
'<p><a href="http://lorem.com/foo/">http://lorem.com/foo/</a></p>',
341340
'<p><a href="http://dolor.com/">http://dolor.com</a></p>'
342-
].join('\n') + '\n');
341+
].join('\n') + '\n';
342+
343+
result.should.eql(expected);
344+
345+
// try again
346+
result = r({text: body});
347+
348+
result.should.eql(expected);
343349
});
344350

345351
it('autolink disabled', () => {
@@ -354,6 +360,17 @@ describe('Marked renderer', () => {
354360
'<p><a href="http://dolor.com/">http://dolor.com</a></p>'
355361
].join('\n') + '\n');
356362
});
363+
364+
it('should not stack overflow', function() {
365+
this.timeout(5000);
366+
const body = 'Great website http://hexo.io';
367+
368+
(() => {
369+
for (let i = 0; i < 100000; i++) {
370+
r({text: body});
371+
}
372+
}).should.not.throw();
373+
});
357374
});
358375

359376
describe('mangle', () => {
@@ -924,12 +941,17 @@ describe('Marked renderer', () => {
924941

925942
const r = require('../lib/renderer').bind(hexo);
926943

927-
const result = r({text: body});
928-
929-
result.should.eql([
944+
let result = r({text: body});
945+
const expected = [
930946
`<p><img data-src="${encodeURL(urlA)}">`,
931947
`<img data-src="${encodeURL(urlB)}"></p>\n`
932-
].join('\n'));
948+
].join('\n');
949+
950+
result.should.eql(expected);
951+
// try again
952+
result = r({text: body});
953+
954+
result.should.eql(expected);
933955
});
934956

935957
it('should execute filter registered to marked:tokenizer', () => {
@@ -951,8 +973,12 @@ describe('Marked renderer', () => {
951973

952974
const r = require('../lib/renderer').bind(hexo);
953975

954-
const result = r({text: body});
955-
result.should.eql(`<p>${escapeHTML(smartypants(body))}</p>\n`);
976+
let result = r({text: body});
977+
const expected = `<p>${escapeHTML(smartypants(body))}</p>\n`;
978+
result.should.eql(expected);
979+
// try again
980+
result = r({text: body});
981+
result.should.eql(expected);
956982
});
957983

958984
it('should execute filter registered to marked:extensions', () => {
@@ -983,8 +1009,12 @@ describe('Marked renderer', () => {
9831009

9841010
const r = require('../lib/renderer').bind(hexo);
9851011

986-
const result = r({text: body});
987-
result.should.eql(`<p class="math block">${escapeHTML('E=mc^2')}</p>\n`);
1012+
let result = r({text: body});
1013+
const expected = `<p class="math block">${escapeHTML('E=mc^2')}</p>\n`;
1014+
result.should.eql(expected);
1015+
// try again
1016+
result = r({text: body});
1017+
result.should.eql(expected);
9881018
});
9891019
});
9901020

0 commit comments

Comments
 (0)