summaryrefslogtreecommitdiff
path: root/src/mfm/parse/elements/quote.ts
blob: 969c1fb4a941319c75df51b06e0f01d2faa9d8ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * Quoted text
 */

export type TextElementQuote = {
	type: 'quote';
	content: string;
	quote: string;
};

export default function(text: string, before: string) {
	const isBegin = before == '';

	const match = text.match(/^"([\s\S]+?)\n"/) || text.match(/^\n>([\s\S]+?)(\n\n|$)/) ||
		(isBegin ? text.match(/^>([\s\S]+?)(\n\n|$)/) : null);

	if (!match) return null;

	const quote = match[1]
		.split('\n')
		.map(line => line.replace(/^>+/g, '').trim())
		.join('\n')
		.trim();

	return {
		type: 'quote',
		content: match[0],
		quote: quote,
	} as TextElementQuote;
}