summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/source_viewer/plugins/wrap_bidi_chars.js
blob: 3b6cd96ef78253d93788f442e6be3a66df26b692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import {
  BIDI_CHARS,
  BIDI_CHARS_CLASS_LIST,
  BIDI_CHAR_TOOLTIP,
} from '~/vue_shared/components/source_viewer/constants';

/**
 * Highlight.js plugin for wrapping BIDI chars.
 * This ensures potentially dangerous BIDI characters are highlighted.
 *
 * Plugin API: https://github.com/highlightjs/highlight.js/blob/main/docs/plugin-api.rst
 *
 * @param {Object} Result - an object that represents the highlighted result from Highlight.js
 */

function wrapBidiChar(bidiChar) {
  return `<span class="${BIDI_CHARS_CLASS_LIST}" title="${BIDI_CHAR_TOOLTIP}">${bidiChar}</span>`;
}

export default (result) => {
  let { value } = result;
  BIDI_CHARS.forEach((bidiChar) => {
    if (value.includes(bidiChar)) {
      value = value.replace(bidiChar, wrapBidiChar(bidiChar));
    }
  });

  // eslint-disable-next-line no-param-reassign
  result.value = value; // Highlight.js expects the result param to be mutated for plugins to work
};