summaryrefslogtreecommitdiff
path: root/source/features/linkify-code.tsx
diff options
context:
space:
mode:
authorGravatar Kid <44045911+kidonng@users.noreply.github.com> 2020-08-10 18:24:32 +0800
committerGravatar GitHub <noreply@github.com> 2020-08-10 11:24:32 +0100
commitd01cff491608e5ddf8f7e5abc685c7c84a054ad8 (patch)
treeeac816369e241833750f20dfc6c82c6cd8612fb4 /source/features/linkify-code.tsx
parent8414271f5635ffda9c1106eecc2236af9a5ff544 (diff)
downloadrefined-github-d01cff491608e5ddf8f7e5abc685c7c84a054ad8.tar.gz
refined-github-d01cff491608e5ddf8f7e5abc685c7c84a054ad8.tar.zst
refined-github-d01cff491608e5ddf8f7e5abc685c7c84a054ad8.zip
Apply `linkify-code` to unfolded diffs (#3409)
Diffstat (limited to '')
-rw-r--r--source/features/linkify-code.tsx51
1 files changed, 26 insertions, 25 deletions
diff --git a/source/features/linkify-code.tsx b/source/features/linkify-code.tsx
index ffb8aec3..d2bd8d2b 100644
--- a/source/features/linkify-code.tsx
+++ b/source/features/linkify-code.tsx
@@ -1,36 +1,37 @@
import select from 'select-dom';
+import onetime from 'onetime';
+import {observe} from 'selector-observer';
import * as pageDetect from 'github-url-detection';
import features from '.';
import {linkifiedURLClass, linkifyURLs, linkifyIssues} from '../github-helpers/dom-formatters';
-function init(): false | void {
- const wrappers = select.all(`
- .js-blob-wrapper:not(.${linkifiedURLClass}),
- .blob-wrapper:not(.${linkifiedURLClass}),
- .comment-body:not(.${linkifiedURLClass})
- `);
+function init(): void {
+ const selectors = [
+ '.js-blob-wrapper',
+ '.blob-wrapper',
+ '.comment-body',
+ '.blob-expanded'
+ ].map(selector => selector + `:not(.${linkifiedURLClass})`).join();
- if (wrappers.length === 0) {
- return false;
- }
+ observe(selectors, {
+ add(wrappers) {
+ // Linkify full URLs
+ // `.blob-code-inner` in diffs
+ // `pre` in GitHub comments
+ for (const element of select.all('.blob-code-inner, pre', wrappers)) {
+ linkifyURLs(element);
+ }
- // Linkify full URLs
- // `.blob-code-inner` in diffs
- // `pre` in GitHub comments
- for (const element of select.all('.blob-code-inner, pre', wrappers)) {
- linkifyURLs(element);
- }
+ // Linkify issue refs in comments
+ for (const element of select.all('span.pl-c', wrappers)) {
+ linkifyIssues(element);
+ }
- // Linkify issue refs in comments
- for (const element of select.all('span.pl-c', wrappers)) {
- linkifyIssues(element);
- }
-
- // Mark code block as touched
- for (const element of wrappers) {
- element.classList.add(linkifiedURLClass);
- }
+ // Mark code block as touched
+ wrappers.classList.add(linkifiedURLClass);
+ }
+ });
}
void features.add({
@@ -41,5 +42,5 @@ void features.add({
include: [
pageDetect.hasCode
],
- init
+ init: onetime(init)
});