You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
test-assignment-overleaf/src/build-scripts/viteThesaurusRegexPlugin.ts

37 lines
1.2 KiB

import type { PathLike } from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import { Plugin } from 'vite';
import { extractWordsFromFile } from './oooThesaurusParser.js';
type ViteThesaurusRegexPluginOptions = {
sourceUrl: URL;
thesaurusPath: string;
};
const WORD_CHARACTER_REGEX_FRAGMENT = "[a-zA-Z']";
export const getRegexStringForThesaurusPath = async (
inputOooThesaurusPath: PathLike,
) => {
const words = await extractWordsFromFile(inputOooThesaurusPath);
return `(?<!${WORD_CHARACTER_REGEX_FRAGMENT})${WORD_CHARACTER_REGEX_FRAGMENT}+(?!${WORD_CHARACTER_REGEX_FRAGMENT})(?<!(?<!${WORD_CHARACTER_REGEX_FRAGMENT})(${words.join(
'|',
)}))`;
};
export const viteThesaurusRegexPlugin = ({
thesaurusPath,
sourceUrl,
}: ViteThesaurusRegexPluginOptions): Plugin => ({
name: 'vite-thesaurus-regex-plugin',
async writeBundle(options) {
if (!options.dir) {
throw new Error('dir is empty');
}
const regexString = await getRegexStringForThesaurusPath(
new URL(thesaurusPath, sourceUrl),
);
await fs.writeFile(path.join(options.dir, thesaurusPath), regexString);
},
});