mirror of
https://github.com/philips/supernote-obsidian-plugin.git
synced 2026-07-22 09:50:25 +00:00
Faster matching
This commit is contained in:
parent
097bec282f
commit
dae3556ccd
1 changed files with 11 additions and 4 deletions
|
|
@ -177,10 +177,17 @@ export function createCustomDictionarySettingsUI(containerEl: HTMLElement, plugi
|
|||
createDictionaryTableUI(customDictionaryContainer, plugin);
|
||||
}
|
||||
|
||||
// Escape special characters in a string to be used in a regular expression
|
||||
function escapeRegExp(string: string): string {
|
||||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
|
||||
// Replace text with custom dictionary entries
|
||||
export function replaceTextWithCustomDictionary(text: string, customDictionary: CustomDictionarySettings['customDictionary']): string {
|
||||
for (const entry of customDictionary) {
|
||||
text = text.replace(new RegExp(entry.source, 'g'), entry.replace);
|
||||
}
|
||||
return text;
|
||||
customDictionary.map(entry => escapeRegExp(entry.source)).join('|');
|
||||
|
||||
return text.replace(new RegExp(customDictionary.map(entry => escapeRegExp(entry.source)).join('|'), 'g'), (match) => {
|
||||
const entry = customDictionary.find(entry => new RegExp(escapeRegExp(entry.source)).test(match));
|
||||
return entry ? entry.replace : match;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue