Skip to content

Commit 888e9af

Browse files
committed
Add links to npm in package.json file view
1 parent 3ef6252 commit 888e9af

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

web_src/css/base.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,12 @@ a.ui.active.label:hover {
16201620
width: 100%;
16211621
}
16221622

1623+
/* don't change link color in highlighted code */
1624+
.code-view .lines-code a,
1625+
.code-view .lines-code a:hover {
1626+
color: inherit;
1627+
}
1628+
16231629
.ui.primary.label,
16241630
.ui.primary.labels .label,
16251631
.ui.ui.ui.primary.label {

web_src/js/features/copycontent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {clippie} from 'clippie';
22
import {showTemporaryTooltip} from '../modules/tippy.js';
33
import {convertImage} from '../utils.js';
4+
import {getFileViewContent} from '../utils/misc.js';
45
import {GET} from '../modules/fetch.js';
56

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

4343
// try copy original first, if that fails and it's an image, convert it to png

web_src/js/features/repo-code.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {invertFileFolding} from './file-fold.js';
44
import {createTippy} from '../modules/tippy.js';
55
import {clippie} from 'clippie';
66
import {toAbsoluteUrl} from '../utils.js';
7+
import {getFileViewContent, getFileViewFileName} from '../utils/misc.js';
78

89
export const singleAnchorRegex = /^#(L|n)([1-9][0-9]*)$/;
910
export const rangeAnchorRegex = /^#(L[1-9][0-9]*)-(L[1-9][0-9]*)$/;
@@ -120,6 +121,44 @@ function showLineButton() {
120121
});
121122
}
122123

124+
function initFilePostProcess() {
125+
const fileName = getFileViewFileName();
126+
if (fileName === 'package.json') {
127+
let data;
128+
try {
129+
data = JSON.parse(getFileViewContent());
130+
} catch {
131+
return;
132+
}
133+
134+
const packages = new Set();
135+
for (const key of [
136+
'dependencies',
137+
'devDependencies',
138+
'optionalDependencies',
139+
'peerDependencies',
140+
]) {
141+
for (const packageName of Object.keys(data?.[key] || {})) {
142+
packages.add(packageName);
143+
}
144+
}
145+
146+
// match chroma NameTag token to detect JSON object keys
147+
for (const el of document.querySelectorAll('.code-inner .nt')) {
148+
const jsonKey = el.textContent.replace(/^"(.*)"$/, '$1');
149+
if (packages.has(jsonKey)) {
150+
const a = document.createElement('a');
151+
a.textContent = jsonKey;
152+
a.href = `https://www.npmjs.com/package/${jsonKey}`;
153+
a.target = '_blank';
154+
a.rel = 'noopener noreferrer nofollow';
155+
el.textContent = '';
156+
el.append('"', a, '"');
157+
}
158+
}
159+
}
160+
}
161+
123162
export function initRepoCodeView() {
124163
if ($('.code-view .lines-num').length > 0) {
125164
$(document).on('click', '.lines-num span', function (e) {
@@ -197,4 +236,5 @@ export function initRepoCodeView() {
197236
$(document).on('click', '.copy-line-permalink', async ({currentTarget}) => {
198237
await clippie(toAbsoluteUrl(currentTarget.getAttribute('data-url')));
199238
});
239+
initFilePostProcess()
200240
}

web_src/js/utils/misc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function getFileViewFileName() {
2+
return document.querySelector('.repo-path .active')?.textContent?.trim();
3+
}
4+
5+
export function getFileViewContent() {
6+
const lineEls = document.querySelectorAll('.file-view .lines-code');
7+
return Array.from(lineEls, (el) => el.textContent).join('');
8+
}

0 commit comments

Comments
 (0)