-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
silverwind
wants to merge
17
commits into
go-gitea:main
Choose a base branch
from
silverwind:npmlink
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d617918
Add links to npm in package.json file view
silverwind 405e737
factor out createExternalLink
silverwind fdb81a7
tweak test
silverwind f57ecc7
rename
silverwind 1a12f18
extract another function
silverwind c472295
add new suppressed link helper and use it
silverwind d7b4a34
add comment
silverwind 62b58d5
Merge remote-tracking branch 'origin/main' into npmlink
silverwind 33b66c7
restructure and improve
silverwind 9b2f650
revert change
silverwind 8aa5c5e
add more keys
silverwind c2217a4
fix lint
silverwind 1f3a151
fix lint
silverwind c28fe8b
remove unneeded attributes and add comments
silverwind 53ff681
change createExternalLink to createLink
silverwind 1dd1343
format
silverwind 3dedb85
add check
silverwind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {getFileViewFilePath, getFileViewFileText, createExternalLink} from '../utils/misc.js'; | ||
import {basename, isObject} from '../utils.js'; | ||
|
||
export function initFileView() { | ||
if (document.querySelector('.file-view.code-view')) { | ||
const fileName = basename(getFileViewFilePath()); | ||
if (fileName === 'package.json') { | ||
processPackageJson(); | ||
} | ||
} | ||
} | ||
|
||
function processPackageJson() { | ||
let obj; | ||
try { | ||
obj = JSON.parse(getFileViewFileText()); | ||
} catch { | ||
return; | ||
} | ||
if (!isObject(obj)) return; | ||
|
||
const packages = new Set(); | ||
|
||
for (const key of [ | ||
'dependencies', | ||
'dependenciesMeta', | ||
'devDependencies', | ||
'optionalDependencies', | ||
'overrides', | ||
'peerDependencies', | ||
'peerDependenciesMeta', | ||
'resolutions', | ||
]) { | ||
for (const packageName of Object.keys(obj?.[key] || {})) { | ||
packages.add(packageName); | ||
} | ||
} | ||
|
||
// match chroma NameTag token to detect JSON object keys | ||
for (const el of document.querySelectorAll('.code-inner .nt')) { | ||
const jsonKey = el.textContent.replace(/^"(.*)"$/, '$1'); | ||
if (packages.has(jsonKey)) { | ||
const link = createExternalLink({ | ||
className: 'suppressed', | ||
textContent: jsonKey, | ||
href: `https://www.npmjs.com/package/${jsonKey}`, | ||
}); | ||
el.textContent = ''; | ||
el.append('"', link, '"'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// returns a file's path from repo root, including a leading slash | ||
export function getFileViewFilePath() { | ||
const pathWithRepo = document.querySelector('.repo-path')?.textContent?.trim(); | ||
return `/${pathWithRepo.split('/').filter((_, i) => i !== 0).join('/')}`; | ||
} | ||
|
||
// returns a file's text content | ||
export function getFileViewFileText() { | ||
const lineEls = document.querySelectorAll('.file-view .lines-code'); | ||
return Array.from(lineEls, (el) => el.textContent).join(''); | ||
} | ||
|
||
// create a external link with suitable attributes | ||
export function createExternalLink(props = {}) { | ||
const a = document.createElement('a'); | ||
a.target = '_blank'; | ||
a.rel = 'noopener noreferrer nofollow'; | ||
silverwind marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (const [key, value] of Object.entries(props)) { | ||
a[key] = value; | ||
} | ||
return a; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {createExternalLink} from './misc.js'; | ||
|
||
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'); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is really fragile, it doesn't seem suitable to be done on frontend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the best frontend can do, and I don't think chroma would ever break it and even if it does, it will be graceful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's not right to let frontend do: the JS code only sees the rendered content, and it heavily depends on Chroma behavior.
I am sure there could be a stable backend solution: fully tested and fully controllable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll not be doing that in backend and I think doing it in backend would massively limit future similar "postprocessing".
Doing these file processings in frontend enables to do more that what the backend could do with only HTML rendering. In this case it's possible to do in backend but on other more advanced cases like, frontend side will be required so I prefer to keep it in frontend only.
Take for example GitHub's code view. All the features there like "go to function" and symbol definitions are pure frontend features in React. The backend is just a dumb server of the content and I think that's very preferable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think it would limit any future processings. Instead, backend could handle all future processings better than frontend.
For example, if it needs to handle maven XML, in frontend, you need a lot of tricks to to handle the rendered XML-HTML content. But in backend, it sees the clear origin content and could add links clearly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have fun implementing a VSCode-like code view in backend 😆. I think these features definitely need to be in frontend and in fact can only be done there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
General talking about "frontend" and "backend" is not clear for this case. Let's abstract the logic:
So the question is: add the links between which steps?
My "backend" proposal:
You "frontend" proposal:
VSCode: VSCode has a well-designed plugin system, I haven't really looked into it, so I don't know at which step it could add links. My intuition tells me it is very unlikely to do it at the last step (eg: parse the rendered content). If I was a VSCode developer, I would do it like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding only links or any other HTML content can be done in backend but my point is that if the post-processing goes beyong HTML modification, like for example a JSON "block folding" feature, this has do be done in JS and then it's better if all such post-processing code is in frontend (JS) and not split between backend and frontend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, if we decide to render code using React or Vue, it will be much easier to migrate the JS code than to migrate it from Go.