summaryrefslogtreecommitdiff
path: root/source/features/embed-gist-inline.tsx
diff options
context:
space:
mode:
authorGravatar Federico Brigante <github@bfred.it> 2019-01-17 19:57:53 +0700
committerGravatar GitHub <noreply@github.com> 2019-01-17 19:57:53 +0700
commit11b927189bab258a226e251c1bd87504feee51f4 (patch)
tree89b3cf4ba9e6b49afd65b8f9a59a7ca33e46efe3 /source/features/embed-gist-inline.tsx
parentf25483fa128d9dcc8e1e74fcf2d2751d604fe11f (diff)
downloadrefined-github-11b927189bab258a226e251c1bd87504feee51f4.tar.gz
refined-github-11b927189bab258a226e251c1bd87504feee51f4.tar.zst
refined-github-11b927189bab258a226e251c1bd87504feee51f4.zip
Drop Babel in favor of esm and TypeScript (#1726)
To get some meaningful errors during feature development, I thought it'd be useful to use TypeScript on `features.js` only, hoping to keep it contained to that file. @sindresorhus suggested we just extend it the whole extension and maybe that can be done incrementally, without having to necessarily use types on everything.
Diffstat (limited to 'source/features/embed-gist-inline.tsx')
-rw-r--r--source/features/embed-gist-inline.tsx58
1 files changed, 58 insertions, 0 deletions
diff --git a/source/features/embed-gist-inline.tsx b/source/features/embed-gist-inline.tsx
new file mode 100644
index 00000000..55841f16
--- /dev/null
+++ b/source/features/embed-gist-inline.tsx
@@ -0,0 +1,58 @@
+import {React} from 'dom-chef/react';
+import select from 'select-dom';
+import domify from '../libs/domify';
+import features from '../libs/features';
+
+const isGist = link =>
+ !link.pathname.includes('.') && // Exclude links to embed files
+ (
+ link.hostname.startsWith('gist.') ||
+ link.pathname.startsWith('gist/')
+ );
+
+const isOnlyChild = link => link.textContent.trim() === link.parentNode.textContent.trim();
+
+async function embedGist(link) {
+ const info = <em> (loading)</em>;
+ link.after(info);
+
+ try {
+ const response = await fetch(`${link.href}.json`);
+ const gistData = await response.json();
+
+ const files = domify(gistData.div).firstElementChild;
+ const fileCount = files.children.length;
+
+ if (fileCount > 1) {
+ info.textContent = ` (${fileCount} files)`;
+ } else {
+ link.parentNode.attachShadow({mode: 'open'}).append(
+ <style>{`
+ .gist .gist-data {
+ max-height: 16em;
+ overflow-y: auto;
+ }
+ `}</style>,
+ <link rel="stylesheet" href={gistData.stylesheet} />,
+ files
+ );
+ }
+ } catch (_) {
+ info.remove(' (embed failed)');
+ }
+}
+function init() {
+ select.all('.js-comment-body p a:only-child')
+ .filter(item => isGist(item) && isOnlyChild(item))
+ .forEach(embedGist);
+}
+
+features.add({
+ id: 'embed-gist-inline',
+ include: [
+ features.isPR,
+ features.isIssue
+ ],
+ load: features.onAjaxedPages,
+ init
+});