Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add links to npm packages in package.json file view #29344

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
6 changes: 6 additions & 0 deletions web_src/css/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,12 @@ a.ui.active.label:hover {
width: 100%;
}

/* don't change link color in highlighted code */
.code-view .lines-code a,
.code-view .lines-code a:hover {
color: inherit;
}

silverwind marked this conversation as resolved.
Show resolved Hide resolved
.ui.primary.label,
.ui.primary.labels .label,
.ui.ui.ui.primary.label {
Expand Down
4 changes: 2 additions & 2 deletions web_src/js/features/copycontent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {clippie} from 'clippie';
import {showTemporaryTooltip} from '../modules/tippy.js';
import {convertImage} from '../utils.js';
import {getFileViewContent} from '../utils/misc.js';
import {GET} from '../modules/fetch.js';

const {i18n} = window.config;
Expand Down Expand Up @@ -36,8 +37,7 @@ export function initCopyContent() {
btn.classList.remove('is-loading', 'small-loading-icon');
}
} else { // text, read from DOM
const lineEls = document.querySelectorAll('.file-view .lines-code');
content = Array.from(lineEls, (el) => el.textContent).join('');
content = getFileViewContent();
}

// try copy original first, if that fails and it's an image, convert it to png
Expand Down
45 changes: 44 additions & 1 deletion web_src/js/features/repo-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {svg} from '../svg.js';
import {invertFileFolding} from './file-fold.js';
import {createTippy} from '../modules/tippy.js';
import {clippie} from 'clippie';
import {toAbsoluteUrl} from '../utils.js';
import {toAbsoluteUrl, createExternalLink} from '../utils.js';
import {getFileViewContent, getFileViewFileName} from '../utils/misc.js';

export const singleAnchorRegex = /^#(L|n)([1-9][0-9]*)$/;
export const rangeAnchorRegex = /^#(L[1-9][0-9]*)-(L[1-9][0-9]*)$/;
Expand Down Expand Up @@ -120,6 +121,47 @@ function showLineButton() {
});
}

function postProcessPackageJson() {
silverwind marked this conversation as resolved.
Show resolved Hide resolved
let data;
try {
data = JSON.parse(getFileViewContent());
} catch {
return;
}

const packages = new Set();
for (const key of [
'dependencies',
'devDependencies',
'optionalDependencies',
'peerDependencies',
]) {
for (const packageName of Object.keys(data?.[key] || {})) {
packages.add(packageName);
}
}

// match chroma NameTag token to detect JSON object keys
for (const el of document.querySelectorAll('.code-inner .nt')) {
silverwind marked this conversation as resolved.
Show resolved Hide resolved
const jsonKey = el.textContent.replace(/^"(.*)"$/, '$1');
if (packages.has(jsonKey)) {
const link = createExternalLink({
textContent: jsonKey,
href: `https://www.npmjs.com/package/${jsonKey}`,
});
el.textContent = '';
el.append('"', link, '"');
}
}
}

function postProcessFile() {
const fileName = getFileViewFileName();
if (fileName === 'package.json') {
postProcessPackageJson();
}
}

export function initRepoCodeView() {
if ($('.code-view .lines-num').length > 0) {
$(document).on('click', '.lines-num span', function (e) {
Expand Down Expand Up @@ -197,4 +239,5 @@ export function initRepoCodeView() {
$(document).on('click', '.copy-line-permalink', async ({currentTarget}) => {
await clippie(toAbsoluteUrl(currentTarget.getAttribute('data-url')));
});
postProcessFile();
}
10 changes: 10 additions & 0 deletions web_src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,13 @@ export function parseDom(text, contentType) {
export function serializeXml(node) {
return xmlSerializer.serializeToString(node);
}

export function createExternalLink(props = {}) {
const a = document.createElement('a');
a.target = '_blank';
a.rel = 'noopener noreferrer nofollow';
for (const [key, value] of Object.entries(props)) {
a[key] = value;
}
return a;
}
12 changes: 9 additions & 3 deletions web_src/js/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
basename, extname, isObject, stripTags, parseIssueHref,
parseUrl, translateMonth, translateDay, blobToDataURI,
toAbsoluteUrl, encodeURLEncodedBase64, decodeURLEncodedBase64,
basename, extname, isObject, stripTags, parseIssueHref, parseUrl, translateMonth, translateDay,
blobToDataURI, toAbsoluteUrl, encodeURLEncodedBase64, decodeURLEncodedBase64, createExternalLink,
} from './utils.js';

test('basename', () => {
Expand Down Expand Up @@ -112,3 +111,10 @@ test('encodeURLEncodedBase64, decodeURLEncodedBase64', () => {
expect(Array.from(decodeURLEncodedBase64('YQ'))).toEqual(Array.from(uint8array('a')));
expect(Array.from(decodeURLEncodedBase64('YQ=='))).toEqual(Array.from(uint8array('a')));
});

test('createExternalLink', () => {
const link = createExternalLink({href: 'https://example.com', textContent: 'example'});
expect(link.tagName).toEqual('A');
expect(link.href).toEqual('https://example.com/');
expect(link.textContent).toEqual('example');
});
8 changes: 8 additions & 0 deletions web_src/js/utils/misc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function getFileViewFileName() {
return document.querySelector('.repo-path .active')?.textContent?.trim();
}

export function getFileViewContent() {
const lineEls = document.querySelectorAll('.file-view .lines-code');
return Array.from(lineEls, (el) => el.textContent).join('');
}
Loading