-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rework linkify-urls-in-code #467
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cfdbb1e
Correctly linkify repeating URLs on the same line
fregante 075e505
Rework linkifyURLsInCode
fregante 8253c7d
Use sindresorhus/linkify-urls and sindresorhus/linkify-issues
fregante 030db16
Fix escaping issue in title
fregante b88a5df
Use sindresorhus/linkify-issues correctly
fregante ff26f92
Update linkify-urls
fregante db06704
Limit URL parsing to text nodes
fregante e05f9da
Avoid DOM writes if there are no changes
fregante d88cc35
DRY
fregante b37ba0e
Skip textNodes that are too short to contain URLs
fregante 1b4dace
Use for-of instead of .forEach
fregante 6b5b9db
Drop tagged template support in html()
fregante 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 hidden or 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 hidden or 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 hidden or 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,2 @@ | ||
| // Get DOM node from HTML | ||
| export default html => document.createRange().createContextualFragment(html); |
This file contains hidden or 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,12 @@ | ||
| export default el => { | ||
| const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT); | ||
| const next = () => { | ||
| const value = walker.nextNode(); | ||
| return { | ||
| value, | ||
| done: !value | ||
| }; | ||
| }; | ||
| walker[Symbol.iterator] = () => ({next}); | ||
| return walker; | ||
| }; | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,38 +1,59 @@ | ||
| import select from 'select-dom'; | ||
| import {issueRegex, linkifyIssueRef} from './util'; | ||
| import linkifyUrls from 'linkify-urls'; | ||
| import linkifyIssues from 'linkify-issues'; | ||
| import {getOwnerAndRepo} from './page-detect'; | ||
| import getTextNodes from './get-text-nodes'; | ||
| import html from './domify'; | ||
|
|
||
| const URLRegex = /(http(s)?(:\/\/))(www\.)?[a-zA-Z0-9-_.]+(\.[a-zA-Z0-9]{2,})([-a-zA-Z0-9:%_+.~#?&//=]*)/g; | ||
| const linkifiedURLClass = 'rg-linkified-code'; | ||
| const commonURLAttrs = `target="_blank" class="${linkifiedURLClass}"`; | ||
| const linkifiedURLClass = 'refined-github-linkified-code'; | ||
| const { | ||
| ownerName, | ||
| repoName | ||
| } = getOwnerAndRepo(); | ||
|
|
||
| const linkifyURL = url => `<a href="${url}" ${commonURLAttrs}>${url}</a>`; | ||
|
|
||
| export const hasIssue = text => issueRegex.test(text); | ||
| export const findURLs = text => text.match(URLRegex) || []; | ||
| const options = { | ||
| user: ownerName, | ||
| repo: repoName, | ||
| attrs: { | ||
| target: '_blank' | ||
| } | ||
| }; | ||
|
|
||
| export const linkifyCode = repoPath => { | ||
| // Don't linkify any already linkified code | ||
| if (select.exists(`.${linkifiedURLClass}`)) { | ||
| export const editTextNodes = (fn, el) => { | ||
| if (!el) { | ||
| return; | ||
| } | ||
| const codeBlobs = document.querySelectorAll('.blob-code-inner'); | ||
| const commentCodeBlobs = document.querySelectorAll('.blob-code-inner span.pl-c'); | ||
|
|
||
| codeBlobs | ||
| .forEach(blob => { | ||
| for (let match of findURLs(blob.innerHTML)) { | ||
| // Remove < or > from beginning or end of an URL | ||
| match = match.replace(/(^<)|(>$)/, ''); | ||
| blob.innerHTML = blob.innerHTML.replace(match, linkifyURL(match)); | ||
| for (const textNode of getTextNodes(el)) { | ||
| if (textNode.textContent.length < 11) { // Shortest url: http://j.mp | ||
| continue; | ||
| } | ||
| }); | ||
|
|
||
| commentCodeBlobs | ||
| .forEach(blob => { | ||
| const blobHTML = blob.innerHTML; | ||
| if (hasIssue(blobHTML)) { | ||
| const issueMatch = blobHTML.match(issueRegex)[0]; | ||
| blob.innerHTML = blobHTML.replace(issueMatch, linkifyIssueRef(repoPath, issueMatch, commonURLAttrs)); | ||
| const linkified = fn(textNode.textContent, options); | ||
| if (linkified !== textNode.textContent) { | ||
| textNode.replaceWith(html(linkified)); | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| export default () => { | ||
| const untouchedCode = select.all(`.blob-wrapper:not(.${linkifiedURLClass})`); | ||
|
|
||
| // Don't linkify any already linkified code | ||
| if (untouchedCode.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| // Linkify full URLs | ||
| for (const el of select.all('.blob-code-inner', untouchedCode)) { | ||
| editTextNodes(linkifyUrls, el); | ||
| } | ||
|
|
||
| // Linkify issue refs in comments | ||
| for (const el of select.all('.blob-code-inner span.pl-c', untouchedCode)) { | ||
| editTextNodes(linkifyIssues, el); | ||
| } | ||
|
|
||
| // Mark code block as touched | ||
| for (const el of untouchedCode) { | ||
| el.classList.add(linkifiedURLClass); | ||
| } | ||
| }; |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,9 +0,0 @@ | ||
| export const issueRegex = /([a-zA-Z0-9-_.]+\/[a-zA-Z0-9-_.]+)?#[0-9]+/; | ||
| export const linkifyIssueRef = (repoPath, issue, attrs) => { | ||
| if (/\//.test(issue)) { | ||
| const issueParts = issue.split('#'); | ||
| return `<a href="/${issueParts[0]}/issues/${issueParts[1]}" ${attrs}>${issue}</a>`; | ||
| } | ||
| return `<a href="/${repoPath}/issues/${issue.replace('#', '')}" ${attrs}>${issue}</a>`; | ||
| }; | ||
|
|
||
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
This would be a useful npm package. Hint hint ;) Shouldn't block merging this PR though.
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 thought about t but I'm not good at testing browser packages 😅
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.
Yeah, it's annoying, but easier if JSDom supports the tree-walker API. See: https://github.com/sindresorhus/is-blob/blob/927899acea40ce7bd31e2b3c4352248eba3a0609/test.js