summaryrefslogtreecommitdiff
path: root/source/features/linkify-code.tsx
diff options
context:
space:
mode:
authorGravatar Sven Lechner <SirWindfield@users.noreply.github.com> 2019-09-06 21:41:37 +0200
committerGravatar Federico Brigante <github@bfred.it> 2019-09-07 02:41:37 +0700
commit546ee4bdca550fdeb0885ba142b15e9644a8a343 (patch)
tree78ac8f47f028311fc16878efa44efe267c24ff39 /source/features/linkify-code.tsx
parent84d0df7ef8a38afa0ab0011c1b674a8db45f0a8d (diff)
downloadrefined-github-546ee4bdca550fdeb0885ba142b15e9644a8a343.tar.gz
refined-github-546ee4bdca550fdeb0885ba142b15e9644a8a343.tar.zst
refined-github-546ee4bdca550fdeb0885ba142b15e9644a8a343.zip
Cleanup mess around formatting/linkifying code (#2381)
Co-authored-by: Federico Brigante <opensource@bfred.it>
Diffstat (limited to 'source/features/linkify-code.tsx')
-rw-r--r--source/features/linkify-code.tsx43
1 files changed, 43 insertions, 0 deletions
diff --git a/source/features/linkify-code.tsx b/source/features/linkify-code.tsx
new file mode 100644
index 00000000..7cafca2a
--- /dev/null
+++ b/source/features/linkify-code.tsx
@@ -0,0 +1,43 @@
+import select from 'select-dom';
+import features from '../libs/features';
+import {linkifiedURLClass, linkifyURLs, linkifyIssues} from '../libs/dom-formatters';
+
+function init(): false | void {
+ const wrappers = select.all(`
+ .js-blob-wrapper:not(.${linkifiedURLClass}),
+ .blob-wrapper:not(.${linkifiedURLClass}),
+ .comment-body:not(.${linkifiedURLClass})
+ `);
+
+ if (wrappers.length === 0) {
+ return false;
+ }
+
+ // 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);
+ }
+
+ // Mark code block as touched
+ for (const el of wrappers) {
+ el.classList.add(linkifiedURLClass);
+ }
+}
+
+features.add({
+ id: __featureName__,
+ description: 'Linkifies URLs and issue references in code.',
+ screenshot: 'https://cloud.githubusercontent.com/assets/170270/25370217/61718820-29b3-11e7-89c5-2959eaf8cac8.png',
+ include: [
+ features.hasCode
+ ],
+ load: features.onAjaxedPages,
+ init
+});