2017-10-30 16:06:25 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import { $$ } from './util.js';
|
|
|
|
|
|
|
|
export default class GitHubEmbed {
|
|
|
|
/**
|
|
|
|
* Embed code from GitHub repositories, similar to Gist embeds. Fetches the
|
|
|
|
* raw text and places it inside element.
|
|
|
|
* Usage: <pre><code data-gh-embed="spacy/master/examples/x.py"></code><pre>
|
|
|
|
* @param {string} user - GitHub user or organization.
|
|
|
|
* @param {string} attr - Data attribute used to select containers. Attribute
|
|
|
|
* value should be path to file relative to user.
|
|
|
|
*/
|
|
|
|
constructor(user, attr) {
|
|
|
|
this.url = `https://raw.githubusercontent.com/${user}`;
|
|
|
|
this.attr = attr;
|
|
|
|
[...$$(`[${this.attr}]`)].forEach(el => this.embed(el));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch code from GitHub and insert it as element content. File path is
|
|
|
|
* read off the container's data attribute.
|
|
|
|
* @param {node} el - The element.
|
|
|
|
*/
|
|
|
|
embed(el) {
|
|
|
|
el.parentElement.setAttribute('data-loading', '');
|
|
|
|
fetch(`${this.url}/${el.getAttribute(this.attr)}`)
|
|
|
|
.then(res => res.text().then(text => ({ text, ok: res.ok })))
|
2017-10-30 18:16:19 +03:00
|
|
|
.then(({ text, ok }) => ok ? this.render(el, text) : false)
|
2017-10-30 16:06:25 +03:00
|
|
|
el.parentElement.removeAttribute('data-loading');
|
|
|
|
}
|
2017-10-30 18:16:19 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add text to container and apply syntax highlighting via Prism, if available.
|
|
|
|
* @param {node} el - The element.
|
|
|
|
* @param {string} text - The raw code, fetched from GitHub.
|
|
|
|
*/
|
|
|
|
render(el, text) {
|
|
|
|
el.textContent = text;
|
|
|
|
if (window.Prism) Prism.highlightElement(el);
|
|
|
|
}
|
2017-10-30 16:06:25 +03:00
|
|
|
}
|