Skip to content

Trying to make a minimal version #152

Discussion options

You must be logged in to vote
// TODO: Rewrite this using the regex sticky flag
function parse(template) {
	template = template
		.replace(/(?<!:)\/\/.*?$/gmu, "") // comments
		.replace(/\\/gu, "\\\\") // backslashes
		.replace(/"/gu, "\\\"") // double quotes
		.replace(/`/gu, "\\`") // backticks
		.replace(/\$\{/gu, "\\${"); // substitutions

	const buffer = [];

	const openTagRegex = /(.*?)<%\s*(=|-)?\s*/gsu;
	const closeTagRegex = /'|"|`|\/\*|(\s*%>)/gu;

	let index = 0;
	let match;

	while (match = openTagRegex.exec(template)) {
		index = match[0].length + match.index;
		const prefix = match[2] ?? "";

		buffer.push(match[1]);

		closeTagRegex.lastIndex = index;

		let closeTag;

		while (closeTag = closeTagRegex.e…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by brianjenkins94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant