parseHtmlMarks.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import type {Fuse} from 'sentry/utils/fuzzySearch';
  2. type Options = {
  3. htmlString: string;
  4. key: string;
  5. markTags: {
  6. highlightPostTag: string;
  7. highlightPreTag: string;
  8. };
  9. };
  10. /**
  11. * Parses the "marked" html strings into a {key, value, indices} (mimincing the
  12. * FuseResultMatch type) object, where the indices are a set of zero indexed
  13. * [start, end] indices for what should be highlighted.
  14. *
  15. * @param key The key of the field, this mimics the Fuse match object
  16. * @param htmlString The html string to parse
  17. * @param markTags.highlightPreTag The left tag
  18. * @param markTags.highlightPostTag The right tag
  19. */
  20. export default function parseHtmlMarks({key, htmlString, markTags}: Options) {
  21. const {highlightPreTag, highlightPostTag} = markTags;
  22. const indices: [number, number][] = [];
  23. let value = htmlString;
  24. // eslint-disable-next-line no-constant-condition
  25. while (true) {
  26. const openIndex = value.indexOf(highlightPreTag);
  27. const openIndexEnd = openIndex + highlightPreTag.length;
  28. if (openIndex === -1 || value.indexOf(highlightPostTag) === -1) {
  29. break;
  30. }
  31. value = value.slice(0, openIndex) + value.slice(openIndexEnd);
  32. const closeIndex = value.indexOf(highlightPostTag);
  33. const closeIndexEnd = closeIndex + highlightPostTag.length;
  34. value = value.slice(0, closeIndex) + value.slice(closeIndexEnd);
  35. indices.push([openIndex, closeIndex - 1]);
  36. }
  37. return {key, value, indices} as Fuse.FuseResultMatch;
  38. }