summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/source_viewer/utils.js
blob: e64e564bf613bc0120b0c8d635828916becbfa52 (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
export const wrapLines = (content) => {
  return (
    content &&
    content
      .split('\n')
      .map((line, i) => {
        let formattedLine;
        const idAttribute = `id="LC${i + 1}"`;

        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)/, `${idAttribute} `);
        } else {
          formattedLine = `<span ${idAttribute} class="line">${line}</span>`;
        }

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