diff options
Diffstat (limited to 'src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/search-highlight.pipe.ts')
-rw-r--r-- | src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/search-highlight.pipe.ts | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/search-highlight.pipe.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/search-highlight.pipe.ts new file mode 100644 index 000000000..c00cc46c6 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/pipes/search-highlight.pipe.ts @@ -0,0 +1,26 @@ +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'searchHighlight' +}) +export class SearchHighlightPipe implements PipeTransform { + transform(value: string, args: string): string { + if (!args) { + return value; + } + args = this.escapeRegExp(args); + const regex = new RegExp(args, 'gi'); + const match = value.match(regex); + + if (!match) { + return value; + } + + return value.replace(regex, '<mark>$&</mark>'); + } + + private escapeRegExp(str: string) { + // $& means the whole matched string + return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); + } +} |