summaryrefslogtreecommitdiff
path: root/packages/astro-parser/src/parse/state/text.ts
blob: 2bd87e37cca7c794971e3f1a57a34ae872c936a5 (plain) (blame)
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
31
32
// @ts-nocheck

import { decode_character_references } from '../utils/html.js';
import { Parser } from '../index.js';

export default function text(parser: Parser) {
	const start = parser.index;

	let data = '';

	const shouldContinue = () => {
		// Special case 'code' content to avoid tripping up on user code
		if (parser.current().name === 'code') {
			return !parser.match('<') && !parser.match('{');
		}
		return !parser.match('<') && !parser.match('{') && !parser.match('`');
	};

	while (parser.index < parser.template.length && shouldContinue()) {
		data += parser.template[parser.index++];
	}

	const node = {
		start,
		end: parser.index,
		type: 'Text',
		raw: data,
		data: decode_character_references(data),
	};

	parser.current().children.push(node);
}