quoteのネスト制限を追加 (#91)

* refactor quote

* add nesting limit test for quote

* nesting limit for quote
This commit is contained in:
marihachi 2022-01-10 13:40:37 +09:00 committed by GitHub
parent 8cc0ca9400
commit d9b8b8db5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 4 deletions

View file

@ -77,7 +77,7 @@
// nesting control
const nestLimit = options.nestLimit || 20;
const nestLimit = (options.nestLimit != null ? options.nestLimit : 20);
let depth = 0;
function enterNest() {
if (depth + 1 > nestLimit) {
@ -166,25 +166,32 @@ plain
// block: quote
quote
= &(BEGIN ">") q:quoteInner LF? { return q; }
= &(BEGIN ">") &{ return (depth + 1 <= nestLimit); } @quoteInner LF?
quoteInner
= head:(quoteLine / quoteEmptyLine) tails:(quoteLine / quoteEmptyLine)+
{
depth++;
const children = applyParser([head, ...tails].join('\n'), 'fullParser');
depth--;
return QUOTE(children);
}
/ line:quoteLine
{
depth++;
const children = applyParser(line, 'fullParser');
depth--;
return QUOTE(children);
}
quoteLine
= BEGIN ">" _? text:$CHAR+ END { return text; }
= BEGIN ">" _? @$CHAR+ END
quoteEmptyLine
= BEGIN ">" _? END { return ''; }
= BEGIN ">" _? END
{
return '';
}
// block: search

View file

@ -1128,6 +1128,36 @@ hoge`;
});
describe('nesting limit', () => {
describe('quote', () => {
it('basic', () => {
const input = '>>> abc';
const output = [
QUOTE([
QUOTE([
TEXT('> abc'),
]),
]),
];
assert.deepStrictEqual(mfm.parse(input, { nestLimit: 2 }), output);
});
it('basic 2', () => {
const input = '>> **abc**';
const output = [
QUOTE([
QUOTE([
TEXT('*'),
ITALIC([
TEXT('abc'),
]),
TEXT('*'),
]),
]),
];
assert.deepStrictEqual(mfm.parse(input, { nestLimit: 2 }), output);
});
});
it('big', () => {
const input = '<b><b>***abc***</b></b>';
const output = [