Sharkey/src/mfm/parse/elements/quote.ts

29 lines
542 B
TypeScript
Raw Normal View History

2018-02-27 12:32:01 +09:00
/**
* Quoted text
*/
2018-06-17 19:55:39 +09:00
export type TextElementQuote = {
2018-06-18 14:28:43 +09:00
type: 'quote'
2018-06-17 19:55:39 +09:00
content: string
quote: string
};
2018-09-21 08:33:24 +09:00
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);
2018-02-27 12:32:01 +09:00
if (!match) return null;
2018-09-21 08:33:24 +09:00
const quote = match[1]
.split('\n')
.map(line => line.replace(/^>+/g, '').trim())
2018-10-29 19:09:24 +09:00
.join('\n')
.trim();
2018-09-21 08:33:24 +09:00
2018-02-27 12:32:01 +09:00
return {
type: 'quote',
2018-09-21 08:33:24 +09:00
content: match[0],
quote: quote,
2018-06-17 19:55:39 +09:00
} as TextElementQuote;
}