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

fix: Maximum call stack size exceeded #315

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ module.exports = function(data, options) {
const { prependRoot, postAsset, dompurify } = markedCfg;
const { path, text } = data;

marked.defaults.extensions = null;
marked.defaults.tokenizer = null;
marked.defaults.renderer = null;
marked.defaults.hooks = null;
marked.defaults.walkTokens = null;

// exec filter to extend marked
this.execFilterSync('marked:use', marked.use, { context: this });

Expand Down
54 changes: 42 additions & 12 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,21 @@ describe('Marked renderer', () => {
].join('\n');

it('autolink enabled', () => {
const result = r({text: body});

result.should.eql([
let result = r({text: body});
const expected = [
'<p>Great website <a href="http://hexo.io/">http://hexo.io</a></p>',
'<p>A webpage <a href="http://www.example.com/">www.example.com</a></p>',
'<p><a href="http://hexo.io/">Hexo</a></p>',
'<p><a href="http://lorem.com/foo/">http://lorem.com/foo/</a></p>',
'<p><a href="http://dolor.com/">http://dolor.com</a></p>'
].join('\n') + '\n');
].join('\n') + '\n';

result.should.eql(expected);

// try again
result = r({text: body});

result.should.eql(expected);
});

it('autolink disabled', () => {
Expand All @@ -354,6 +360,17 @@ describe('Marked renderer', () => {
'<p><a href="http://dolor.com/">http://dolor.com</a></p>'
].join('\n') + '\n');
});

it('should not stack overflow', function() {
this.timeout(5000);
const body = 'Great website http://hexo.io';

(() => {
for (let i = 0; i < 100000; i++) {
r({text: body});
}
}).should.not.throw();
});
});

describe('mangle', () => {
Expand Down Expand Up @@ -924,12 +941,17 @@ describe('Marked renderer', () => {

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

const result = r({text: body});

result.should.eql([
let result = r({text: body});
const expected = [
`<p><img data-src="${encodeURL(urlA)}">`,
`<img data-src="${encodeURL(urlB)}"></p>\n`
].join('\n'));
].join('\n');

result.should.eql(expected);
// try again
result = r({text: body});

result.should.eql(expected);
});

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

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

const result = r({text: body});
result.should.eql(`<p>${escapeHTML(smartypants(body))}</p>\n`);
let result = r({text: body});
const expected = `<p>${escapeHTML(smartypants(body))}</p>\n`;
result.should.eql(expected);
// try again
result = r({text: body});
result.should.eql(expected);
});

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

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

const result = r({text: body});
result.should.eql(`<p class="math block">${escapeHTML('E=mc^2')}</p>\n`);
let result = r({text: body});
const expected = `<p class="math block">${escapeHTML('E=mc^2')}</p>\n`;
result.should.eql(expected);
// try again
result = r({text: body});
result.should.eql(expected);
});
});

Expand Down
Loading