import React from 'dom-chef'; import domify from 'doma'; import select from 'select-dom'; import features from '../libs/features'; const isGist = (link: HTMLAnchorElement): boolean => !link.pathname.includes('.') && // Exclude links to embed files ( (link.hostname.startsWith('gist.') && link.pathname.includes('/', 1)) || // Exclude user links link.pathname.startsWith('gist/') ); const isOnlyChild = (link: HTMLAnchorElement): boolean => link.textContent!.trim() === link.parentNode!.textContent!.trim(); async function embedGist(link: HTMLAnchorElement): Promise { const info = (loading); link.after(info); try { const response = await fetch(`${link.href}.json`); const gistData = await response.json(); const files = domify.one(gistData.div)!; const fileCount = files.children.length; if (fileCount > 1) { info.textContent = ` (${fileCount} files)`; } else { link.parentElement!.attachShadow({mode: 'open'}).append( , , files ); } } catch { info.replaceWith(' (embed failed)'); } } function init(): void { select.all('.js-comment-body p a:only-child') .filter(item => isGist(item) && isOnlyChild(item)) .forEach(embedGist); } features.add({ id: __featureName__, description: 'Embeds linked gists.', screenshot: 'https://user-images.githubusercontent.com/6978877/33911900-c62ee968-df8b-11e7-8685-506ffafc60b4.', include: [ features.hasComments ], load: features.onAjaxedPages, init });