UNPKG

@wordpress/components

Version:
1 lines • 1.9 kB
{"version":3,"file":"utils.cjs","names":["findAll","createElement"],"sources":["../../src/text/utils.ts"],"sourcesContent":["import type { FindAllArgs } from 'highlight-words-core';\nimport { findAll } from 'highlight-words-core';\nimport { createElement } from '@wordpress/element';\n\n/**\n * Source:\n * https://github.com/bvaughn/react-highlight-words/blob/HEAD/src/Highlighter.js\n */\n\ninterface Options {\n\tautoEscape?: boolean;\n\tcaseSensitive?: boolean;\n\tchildren: string;\n\tsanitize?: FindAllArgs[ 'sanitize' ];\n\tsearchWords?: string[];\n}\n\n/**\n * Creates the highlighted text rendered by Text.\n *\n * @param options Highlighter options.\n * @param options.autoEscape Whether to escape regular-expression characters.\n * @param options.caseSensitive Whether matching is case-sensitive.\n * @param options.children Text to highlight.\n * @param options.sanitize Text normalization function used for matching.\n * @param options.searchWords Search terms to highlight.\n */\nexport function createHighlighterText( {\n\tautoEscape,\n\tcaseSensitive = false,\n\tchildren,\n\tsanitize,\n\tsearchWords = [],\n}: Options ) {\n\tif ( ! children ) {\n\t\treturn null;\n\t}\n\n\tconst chunks = findAll( {\n\t\tautoEscape,\n\t\tcaseSensitive,\n\t\tsanitize,\n\t\tsearchWords,\n\t\ttextToHighlight: children,\n\t} );\n\n\treturn chunks.map( ( chunk, index ) =>\n\t\tcreateElement( chunk.highlight ? 'mark' : 'span', {\n\t\t\tchildren: children.slice( chunk.start, chunk.end ),\n\t\t\tkey: index,\n\t\t} )\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA2BA,SAAgB,sBAAuB,EACtC,YACA,gBAAgB,OAChB,UACA,UACA,cAAc,CAAC,KACH;CACZ,IAAK,CAAE,UACN,OAAO;CAWR,QAAA,GAReA,qBAAAA,QAAAA,CAAS;EACvB;EACA;EACA;EACA;EACA,iBAAiB;CAClB,CAEY,CAAC,CAAC,KAAO,OAAO,WAAA,GAC3BC,mBAAAA,cAAAA,CAAe,MAAM,YAAY,SAAS,QAAQ;EACjD,UAAU,SAAS,MAAO,MAAM,OAAO,MAAM,GAAI;EACjD,KAAK;CACN,CAAE,CACH;AACD"}