diff --git a/keywords.yml.example b/keywords.yml.example index feeceef..1c552e2 100644 --- a/keywords.yml.example +++ b/keywords.yml.example @@ -5,6 +5,11 @@ keywords: # will match plain regex - name: woof pattern: 'w[oa]+f' + # you can also specify flags for your regex + flags: + global: true + multi_line: false + insensitive: true # will match regex wraped with word borders # will match 'hi, woof :3', 'woof!', 'i heard a woof' but not 'i like subwoofers' diff --git a/package.json b/package.json index c611771..0bba223 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "mtproto_exporter", "type": "module", - "version": "1.4.2", + "version": "1.5.0", "packageManager": "pnpm@10.6.5", "license": "MIT", "scripts": { diff --git a/src/config.ts b/src/config.ts index e8ab7e6..b76c3d9 100644 --- a/src/config.ts +++ b/src/config.ts @@ -94,11 +94,32 @@ export async function readKeywords(filePath: string): Promise keywords.push(item); } else if (typeof item === "object" && typeof item.name === "string") { if (typeof item.pattern === "string") { - keywords.push({ + let result = { name: item.name, pattern: item.pattern, word: Boolean(item.word ?? false), - }); + flags: { + global: true, + multi_line: false, + insensitive: true, + } + }; + + if (typeof item.flags === "object") { + if (typeof item.flags.global === "boolean") { + result.flags.global = item.flags.global; + } + + if (typeof item.flags.multi_line === "boolean") { + result.flags.multi_line = item.flags.multi_line; + } + + if (typeof item.flags.insensitive === "boolean") { + result.flags.insensitive = item.flags.insensitive; + } + } + + keywords.push(result); } } } diff --git a/src/metrics/keywords.ts b/src/metrics/keywords.ts index ea45354..6691633 100644 --- a/src/metrics/keywords.ts +++ b/src/metrics/keywords.ts @@ -11,6 +11,11 @@ export interface RawKeywordPattern { name: string; pattern: string; word: boolean; + flags: { + global: boolean; + multi_line: boolean; + insensitive: boolean; + } } export type RawKeywordLike = string | RawKeywordPattern; @@ -26,6 +31,7 @@ export function rawToPatterns(raw: RawKeywordLike[]): KeywordPattern[] { let pattern; let name; let addBorders = false; + let flags = "giu"; if (typeof keyword === "string") { pattern = escapeRegex(keyword); @@ -35,15 +41,28 @@ export function rawToPatterns(raw: RawKeywordLike[]): KeywordPattern[] { pattern = keyword.pattern; name = keyword.name; addBorders = keyword.word; + + flags = "u"; + if (keyword.flags.global) { + flags += "g"; + } + + if (keyword.flags.insensitive) { + flags += "i"; + } + + if (keyword.flags.multi_line) { + flags += "m"; + } } const wordBorder = escapeRegex("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"); - const borderStart = addBorders ? `(?:[${wordBorder}\\s]|^)` : ""; - const borderEnd = addBorders ? `(?:[${wordBorder}\\s]|$)` : ""; + const borderStart = addBorders ? `(?<=[${wordBorder}\\s]|^)` : ""; + const borderEnd = addBorders ? `(?=[${wordBorder}\\s]|$)` : ""; patterns.push({ name, - pattern: new RegExp(borderStart + pattern + borderEnd), + pattern: new RegExp(`${borderStart}(?:${pattern})${borderEnd}`, flags), }); }