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
|
import { createLink, generateHLJSOpenTag } from './dependency_linker_util';
const PROTOCOL = 'https://';
const GODOCS_DOMAIN = 'godoc.org/';
const REPO_PATH = '/tree/master/';
const GODOCS_REGEX = /golang.org/;
const GITLAB_REPO_PATH = `/_${REPO_PATH}`;
const REPO_REGEX = `[^/'"]+/[^/'"]+`;
const NESTED_REPO_REGEX = '([^/]+/)+[^/]+?';
const GITHUB_REPO_REGEX = new RegExp(`(github.com/${REPO_REGEX})/(.+)`);
const GITLAB_REPO_REGEX = new RegExp(`(gitlab.com/${REPO_REGEX})/(.+)`);
const GITLAB_NESTED_REPO_REGEX = new RegExp(`(gitlab.com/${NESTED_REPO_REGEX}).git/(.+)`);
const attrOpenTag = generateHLJSOpenTag('attr');
const stringOpenTag = generateHLJSOpenTag('string');
const closeTag = '"</span>';
const importPathString =
'ImportPath"</span><span class="hljs-punctuation">:</span><span class=""> </span>';
const DEPENDENCY_REGEX = new RegExp(
/*
* Detects dependencies inside of content that is highlighted by Highlight.js
* Example: <span class="hljs-attr">"ImportPath"</span><span class="hljs-punctuation">:</span><span class=""> </span><span class="hljs-string">"github.com/ayufan/golang-kardianos-service"</span>
* Group 1: github.com/ayufan/golang-kardianos-service
*/
`${importPathString}${stringOpenTag}(.*)${closeTag}`,
'gm',
);
const replaceRepoPath = (dependency, regex, repoPath) =>
dependency.replace(regex, (_, repo, path) => `${PROTOCOL}${repo}${repoPath}${path}`);
const regexConfigs = [
{
matcher: GITHUB_REPO_REGEX,
resolver: (dep) => replaceRepoPath(dep, GITHUB_REPO_REGEX, REPO_PATH),
},
{
matcher: GITLAB_REPO_REGEX,
resolver: (dep) => replaceRepoPath(dep, GITLAB_REPO_REGEX, GITLAB_REPO_PATH),
},
{
matcher: GITLAB_NESTED_REPO_REGEX,
resolver: (dep) => replaceRepoPath(dep, GITLAB_NESTED_REPO_REGEX, GITLAB_REPO_PATH),
},
{
matcher: GODOCS_REGEX,
resolver: (dep) => `${PROTOCOL}${GODOCS_DOMAIN}${dep}`,
},
];
const getLinkHref = (dependency) => {
const regexConfig = regexConfigs.find((config) => dependency.match(config.matcher));
return regexConfig ? regexConfig.resolver(dependency) : `${PROTOCOL}${dependency}`;
};
const handleReplace = (dependency) => {
const linkHref = getLinkHref(dependency);
const link = createLink(linkHref, dependency);
return `${importPathString}${attrOpenTag}${link}${closeTag}`;
};
export default (result) => {
return result.value.replace(DEPENDENCY_REGEX, (_, dependency) => handleReplace(dependency));
};
|