Welcome to mirror list, hosted at ThFree Co, Russian Federation.

markdown_sourcemap.js « services « content_editor « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fe1b32c5b0a7e37a8032c5e12854289342e84007 (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
31
32
33
34
35
36
37
38
39
40
41
42
import { isString } from 'lodash';

export const getFullSource = (element) => {
  const commentNode = element.ownerDocument.body.lastChild;

  if (commentNode?.nodeName === '#comment' && isString(commentNode.textContent)) {
    return commentNode.textContent.split('\n');
  }

  return [];
};

const getRangeFromSourcePos = (sourcePos) => {
  const [start, end] = sourcePos.split('-');
  const [startRow, startCol] = start.split(':');
  const [endRow, endCol] = end.split(':');

  return {
    start: { row: Number(startRow) - 1, col: Number(startCol) - 1 },
    end: { row: Number(endRow) - 1, col: Number(endCol) - 1 },
  };
};

export const getMarkdownSource = (element) => {
  if (!element.dataset.sourcepos) return undefined;

  const source = getFullSource(element);
  const range = getRangeFromSourcePos(element.dataset.sourcepos);
  let elSource = '';

  for (let i = range.start.row; i <= range.end.row; i += 1) {
    if (i === range.start.row) {
      elSource += source[i].substring(range.start.col);
    } else if (i === range.end.row) {
      elSource += `\n${source[i]?.substring(0, range.start.col)}`;
    } else {
      elSource += `\n${source[i]}` || '';
    }
  }

  return elSource.trim();
};