diff options
Diffstat (limited to 'app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils')
3 files changed, 50 insertions, 7 deletions
diff --git a/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/dependency_linker_util.js b/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/dependency_linker_util.js index 56ad55ef553..dbe6812cf16 100644 --- a/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/dependency_linker_util.js +++ b/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/dependency_linker_util.js @@ -7,9 +7,10 @@ export const createLink = (href, innerText) => { const link = document.createElement('a'); setAttributes(link, { href: escape(href), rel }); - link.innerText = escape(innerText); + link.textContent = innerText; return link.outerHTML; }; -export const generateHLJSOpenTag = (type) => `<span class="hljs-${escape(type)}">"`; +export const generateHLJSOpenTag = (type, delimiter = '"') => + `<span class="hljs-${escape(type)}">${delimiter}`; diff --git a/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/gemspec_linker.js b/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/gemspec_linker.js new file mode 100644 index 00000000000..35de8fd13d6 --- /dev/null +++ b/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/gemspec_linker.js @@ -0,0 +1,39 @@ +import { joinPaths } from '~/lib/utils/url_utility'; +import { GEM_URL } from '../../constants'; +import { createLink, generateHLJSOpenTag } from './dependency_linker_util'; + +const methodRegex = '.*add_dependency.*|.*add_runtime_dependency.*|.*add_development_dependency.*'; +const openTagRegex = generateHLJSOpenTag('string', '(&.*;)'); +const closeTagRegex = '&.*</span>'; + +const DEPENDENCY_REGEX = new RegExp( + /* + * Detects gemspec dependencies inside of content that is highlighted by Highlight.js + * Example: s.add_dependency(<span class="hljs-string">'rugged'</span>, <span class="hljs-string">'~> 0.24.0'</span>) + * + * Group 1 (method) : s.add_dependency( + * Group 2 (delimiter) : ' + * Group 3 (packageName): rugged + * Group 4 (closeTag) : '</span> + * Group 5 (rest) : , <span class="hljs-string">'~> 0.24.0'</span>) + */ + `(${methodRegex})${openTagRegex}(.*)(${closeTagRegex})(.*${closeTagRegex})`, + 'gm', +); + +const handleReplace = (method, delimiter, packageName, closeTag, rest) => { + // eslint-disable-next-line @gitlab/require-i18n-strings + const openTag = generateHLJSOpenTag('string linked', delimiter); + const href = joinPaths(GEM_URL, packageName); + const packageLink = createLink(href, packageName); + + return `${method}${openTag}${packageLink}${closeTag}${rest}`; +}; + +export default (result) => { + return result.value.replace( + DEPENDENCY_REGEX, + (_, method, delimiter, packageName, closeTag, rest) => + handleReplace(method, delimiter, packageName, closeTag, rest), + ); +}; diff --git a/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/package_json_linker.js b/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/package_json_linker.js index d013d077ba3..3c6fc23c138 100644 --- a/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/package_json_linker.js +++ b/app/assets/javascripts/vue_shared/components/source_viewer/plugins/utils/package_json_linker.js @@ -1,3 +1,4 @@ +import { unescape } from 'lodash'; import { joinPaths } from '~/lib/utils/url_utility'; import { NPM_URL } from '../../constants'; import { createLink, generateHLJSOpenTag } from './dependency_linker_util'; @@ -17,13 +18,15 @@ const DEPENDENCY_REGEX = new RegExp( ); const handleReplace = (original, packageName, version, dependenciesToLink) => { - const href = joinPaths(NPM_URL, packageName); - const packageLink = createLink(href, packageName); - const versionLink = createLink(href, version); + const unescapedPackageName = unescape(packageName); + const unescapedVersion = unescape(version); + const href = joinPaths(NPM_URL, unescapedPackageName); + const packageLink = createLink(href, unescapedPackageName); + const versionLink = createLink(href, unescapedVersion); const closeAndOpenTag = `${closeTag}: ${attrOpenTag}`; - const dependencyToLink = dependenciesToLink[packageName]; + const dependencyToLink = dependenciesToLink[unescapedPackageName]; - if (dependencyToLink && dependencyToLink === version) { + if (dependencyToLink && dependencyToLink === unescapedVersion) { return `${attrOpenTag}${packageLink}${closeAndOpenTag}${versionLink}${closeTag}`; } |