summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/source_viewer/utils.js
blob: d726a8a55ffdb637f95813daed97c9f7fc579ae5 (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
export const wrapLines = (content, language) => {
  const isValidLanguage = /^[a-z\d\-_]+$/.test(language); // To prevent the possibility of a vulnerability we only allow languages that contain alphanumeric characters ([a-z\d), dashes (-) or underscores (_).

  return (
    content &&
    content
      .split('\n')
      .map((line, i) => {
        let formattedLine;
        const attributes = `id="LC${i + 1}" lang="${isValidLanguage ? language : ''}"`;

        if (line.includes('<span class="hljs') && !line.includes('</span>')) {
          /**
           * In some cases highlight.js will wrap multiple lines in a span, in these cases we want to append the line number to the existing span
           *
           * example (before):  <span class="hljs-code">```bash
           * example (after):   <span id="LC67" class="hljs-code">```bash
           */
          formattedLine = line.replace(/(?=class="hljs)/, `${attributes} `);
        } else {
          formattedLine = `<span ${attributes} class="line">${line}</span>`;
        }

        return formattedLine;
      })
      .join('\n')
  );
};