summaryrefslogtreecommitdiff
path: root/src/text/parse/elements/code.ts
blob: f48e9450487dfabcec0a248e94e53b2002701a2a (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
/**
 * Code (block)
 */

import genHtml from '../core/syntax-highlighter';

export type TextElementCode = {
	type: 'code'
	content: string
	code: string
	html: string
};

export default function(text: string) {
	const match = text.match(/^```([\s\S]+?)```/);
	if (!match) return null;
	const code = match[0];
	return {
		type: 'code',
		content: code,
		code: code.substr(3, code.length - 6).trim(),
		html: genHtml(code.substr(3, code.length - 6).trim())
	} as TextElementCode;
}