recital / core / src / pipeline / 01-strings / plugins / comments.ts
comments.ts
Raw
/**
 * Copyright (c) 2022 Amorphous
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import moo from 'moo'
import eol from 'eol'
import { StringParserPlugin } from '../../../types'

export const parseComments: StringParserPlugin = (raw: string): string => {
	const lexer = moo.compile({
		comment: { match: /^=.*\n?/, lineBreaks: true },
		comment2: /^=.*?$/,
		text: { match: /[^]+?/, lineBreaks: true },
	})

	lexer.reset(eol.lf(raw))

	let finalString = ''

	for (let token of Array.from(lexer) as any[]) {
		if (token.type === 'text') {
			finalString += token.value
		}
	}

	return finalString
}