This commit is contained in:
syuilo 2018-09-21 08:33:24 +09:00
parent ec5b8e1305
commit 3824dccbe3
2 changed files with 26 additions and 9 deletions

View File

@ -8,13 +8,20 @@ export type TextElementQuote = {
quote: string quote: string
}; };
export default function(text: string) { export default function(text: string, index: number) {
const match = text.match(/^"([\s\S]+?)\n"/) || text.match(/^>([\s\S]+?)\n\n/) || text.match(/^\n>([\s\S]+?)\n\n/) || text.match(/^>([\s\S]+?)$/); 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; if (!match) return null;
const quote = match[0];
const quote = match[1]
.split('\n')
.map(line => line.replace(/^>+/g, '').trim())
.join('\n');
return { return {
type: 'quote', type: 'quote',
content: quote, content: match[0],
quote: match[1].trim(), quote: quote,
} as TextElementQuote; } as TextElementQuote;
} }

View File

@ -88,17 +88,27 @@ describe('Text', () => {
}); });
it('quote', () => { it('quote', () => {
const tokens1 = analyze('> foo\nbar\baz'); const tokens1 = analyze('> foo\nbar\nbaz');
assert.deepEqual([ assert.deepEqual([
{ type: 'quote', content: '> foo\nbar\baz', quote: 'foo\nbar\baz' } { type: 'quote', content: '> foo\nbar\nbaz', quote: 'foo\nbar\nbaz' }
], tokens1); ], tokens1);
const tokens2 = analyze('before\n> foo\nbar\baz\n\nafter'); const tokens2 = analyze('before\n> foo\nbar\nbaz\n\nafter');
assert.deepEqual([ assert.deepEqual([
{ type: 'text', content: 'before' }, { 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' } { type: 'text', content: 'after' }
], tokens2); ], 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', () => { it('url', () => {