aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/documentation/utils.ts
blob: b11b50beeec75fdc7343456dfaf8be3a018e4586 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * SonarQube
 * Copyright (C) 2009-2020 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { sortBy } from 'lodash';

export type DocumentationEntryScope = 'sonarqube' | 'sonarcloud' | 'static';

export interface DocumentationEntry {
  content: string;
  relativeName: string;
  navTitle: string | undefined;
  text: string;
  title: string;
  url: string;
}

export function getNodeFromUrl(pages: DocumentationEntry[], url: string) {
  return pages.find(p => p.url === url);
}

const WORDS = 6;

function cutLeadingWords(str: string) {
  let words = 0;
  for (let i = str.length - 1; i >= 0; i--) {
    if (/\s/.test(str[i])) {
      words++;
    }
    if (words === WORDS) {
      return i > 0 ? `...${str.substring(i + 1)}` : str;
    }
  }
  return str;
}

function cutTrailingWords(str: string) {
  let words = 0;
  for (let i = 0; i < str.length; i++) {
    if (/\s/.test(str[i])) {
      words++;
    }
    if (words === WORDS) {
      return i < str.length - 1 ? `${str.substring(0, i)}...` : str;
    }
  }
  return str;
}

export function cutWords(tokens: Array<{ text: string; marked: boolean }>) {
  const result: Array<{ text: string; marked: boolean }> = [];
  let length = 0;

  const highlightPos = tokens.findIndex(token => token.marked);
  if (highlightPos > 0) {
    const text = cutLeadingWords(tokens[highlightPos - 1].text);
    result.push({ text, marked: false });
    length += text.length;
  }

  result.push(tokens[highlightPos]);
  length += tokens[highlightPos].text.length;

  for (let i = highlightPos + 1; i < tokens.length; i++) {
    if (length + tokens[i].text.length > 100) {
      const text = cutTrailingWords(tokens[i].text);
      result.push({ text, marked: false });
      return result;
    } else {
      result.push(tokens[i]);
      length += tokens[i].text.length;
    }
  }

  return result;
}

export function highlightMarks(str: string, marks: Array<{ from: number; to: number }>) {
  const sortedMarks = sortBy(
    [
      ...marks.map(mark => ({ pos: mark.from, start: true })),
      ...marks.map(mark => ({ pos: mark.to, start: false }))
    ],
    mark => mark.pos,
    mark => Number(!mark.start)
  );

  const cuts: Array<{ text: string; marked: boolean }> = [];
  let start = 0;
  let balance = 0;

  for (const mark of sortedMarks) {
    if (mark.start) {
      if (balance === 0 && start !== mark.pos) {
        cuts.push({ text: str.substring(start, mark.pos), marked: false });
        start = mark.pos;
      }
      balance++;
    } else {
      balance--;
      if (balance === 0 && start !== mark.pos) {
        cuts.push({ text: str.substring(start, mark.pos), marked: true });
        start = mark.pos;
      }
    }
  }

  if (start < str.length - 1) {
    cuts.push({ text: str.substr(start), marked: false });
  }

  return cuts;
}