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

go_sum_linker.js « utils « plugins « source_viewer « components « vue_shared « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b290dfa78b9d1620afea3c0f3ab890f0521ef8dd (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
import { createLink } from './dependency_linker_util';

const openTag = '<span class="">';
const closeTag = '</span>';
const TAG_URL = 'https://sum.golang.org/lookup/';
const GO_PACKAGE_URL = 'https://pkg.go.dev/';

const DEPENDENCY_REGEX = new RegExp(
  /*
   * Detects dependencies inside of content that is highlighted by Highlight.js
   * Example: '<span class="">cloud.google.com/Go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o=</span>'
   * Group 1 (packagePath):  'cloud.google.com/Go/bigquery'
   * Group 2 (version): 'v1.0.1/go.mod'
   * Group 3 (base64url): 'i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o='
   */
  `${openTag}(.*) (v.*) h1:(.*)${closeTag}`,
  'gm',
);

const handleReplace = (packagePath, version, tag) => {
  const lowercasePath = packagePath.toLowerCase();
  const packageHref = `${GO_PACKAGE_URL}${lowercasePath}`;
  const packageLink = createLink(packageHref, packagePath);
  const tagHref = `${TAG_URL}${lowercasePath}@${version.split('/go.mod')[0]}`;
  const tagLink = createLink(tagHref, tag);

  return `${openTag}${packageLink} ${version} h1:${tagLink}${closeTag}`;
};

export default (result) => {
  return result.value.replace(DEPENDENCY_REGEX, (_, packagePath, version, tag) =>
    handleReplace(packagePath, version, tag),
  );
};