diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-17 14:33:21 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-17 14:33:21 +0300 |
commit | 7021455bd1ed7b125c55eb1b33c5a01f2bc55ee0 (patch) | |
tree | 5bdc2229f5198d516781f8d24eace62fc7e589e9 /app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/godeps_json_linker.js | |
parent | 185b095e93520f96e9cfc31d9c3e69b498cdab7c (diff) |
Add latest changes from gitlab-org/gitlab@15-6-stable-eev15.6.0-rc42
Diffstat (limited to 'app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/godeps_json_linker.js')
-rw-r--r-- | app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/godeps_json_linker.js | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/godeps_json_linker.js b/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/godeps_json_linker.js new file mode 100644 index 00000000000..bff8e3cf410 --- /dev/null +++ b/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/godeps_json_linker.js @@ -0,0 +1,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)); +}; |