diff --git a/src/mfm/parse/elements/quote.ts b/src/mfm/parse/elements/quote.ts index aa932cbf8..994ce98ca 100644 --- a/src/mfm/parse/elements/quote.ts +++ b/src/mfm/parse/elements/quote.ts @@ -8,13 +8,20 @@ export type TextElementQuote = { quote: string }; -export default function(text: string) { - const match = text.match(/^"([\s\S]+?)\n"/) || text.match(/^>([\s\S]+?)\n\n/) || text.match(/^\n>([\s\S]+?)\n\n/) || text.match(/^>([\s\S]+?)$/); +export default function(text: string, index: number) { + const match = text.match(/^"([\s\S]+?)\n"/) || text.match(/^\n>([\s\S]+?)(\n\n|$)/) || + (index == 0 ? text.match(/^>([\s\S]+?)(\n\n|$)/) : null); + if (!match) return null; - const quote = match[0]; + + const quote = match[1] + .split('\n') + .map(line => line.replace(/^>+/g, '').trim()) + .join('\n'); + return { type: 'quote', - content: quote, - quote: match[1].trim(), + content: match[0], + quote: quote, } as TextElementQuote; } diff --git a/test/mfm.ts b/test/mfm.ts index 25a7c290e..b0c793e44 100644 --- a/test/mfm.ts +++ b/test/mfm.ts @@ -88,17 +88,27 @@ describe('Text', () => { }); it('quote', () => { - const tokens1 = analyze('> foo\nbar\baz'); + const tokens1 = analyze('> foo\nbar\nbaz'); assert.deepEqual([ - { type: 'quote', content: '> foo\nbar\baz', quote: 'foo\nbar\baz' } + { type: 'quote', content: '> foo\nbar\nbaz', quote: 'foo\nbar\nbaz' } ], tokens1); - const tokens2 = analyze('before\n> foo\nbar\baz\n\nafter'); + const tokens2 = analyze('before\n> foo\nbar\nbaz\n\nafter'); assert.deepEqual([ { type: 'text', content: 'before' }, - { type: 'quote', content: '\n> foo\nbar\baz\n\n', quote: 'foo\nbar\baz' }, + { type: 'quote', content: '\n> foo\nbar\nbaz\n\n', quote: 'foo\nbar\nbaz' }, { type: 'text', content: 'after' } ], tokens2); + + const tokens3 = analyze('piyo> foo\nbar\nbaz'); + assert.deepEqual([ + { type: 'text', content: 'piyo> foo\nbar\nbaz' } + ], tokens3); + + const tokens4 = analyze('> foo\n> bar\n> baz'); + assert.deepEqual([ + { type: 'quote', content: '> foo\n> bar\n> baz', quote: 'foo\nbar\nbaz' } + ], tokens4); }); it('url', () => {