1 | # github-embed
|
2 |
|
3 | The tool allows to embed code from Github on a webpage.
|
4 |
|
5 | ![](http://i.imgur.com/d6Ysdpg.png)
|
6 |
|
7 | [Demo](http://finom.github.io/github-embed/demo.html)
|
8 |
|
9 | ## Usage
|
10 |
|
11 | ### CommonJS
|
12 | ```
|
13 | npm install --save github-embed
|
14 | ```
|
15 |
|
16 | ```js
|
17 | const githubEmbed = require('github-embed');
|
18 | githubEmbed('.element', settings);
|
19 | ```
|
20 | CSS file is placed at **node_modules/css/github-embed.css**.
|
21 |
|
22 | ### Direct use
|
23 |
|
24 | Bundled (downloadable) version and the demo of the tool lives at [gh-pages branch](https://github.com/finom/github-embed/tree/gh-pages).
|
25 | ```html
|
26 | <script src="github-embed.min.js"></script>
|
27 | <script>
|
28 | githubEmbed('.element', settings);
|
29 | </script>
|
30 | ```
|
31 |
|
32 |
|
33 | ### API
|
34 |
|
35 | ``githubEmbed`` function accepts two arguments: an element where embedding block will be mount (a selector, a node, jQuery instance etc) and settings object.
|
36 |
|
37 | Settings object should include the following properties:
|
38 |
|
39 | - ``repo: STRING`` a name of a repository whose files will be embedded
|
40 | - ``owner: STRING`` an owner of the repo
|
41 | - ``ref: STRING`` a branch, a tag or commit SHA
|
42 | - ``embed: ARRAY`` a list of embedded files
|
43 | - ``path: STRING`` - a path to embedded file relative to the root of the repo
|
44 | - ``type: STRING`` - a type of file you want to embed (a programming language)
|
45 | - ``label: STRING`` - what to display in navigation. By default it's a name of embedded file
|
46 | - ``active: BOOLEAN`` - is the item shown by default
|
47 | - ``repo: STRING`` - a name of a repository where current file lives (in case if you want to embed a file from another repo)
|
48 | - ``owner: STRING`` - an owner of a repo where current file lives (in case if you want to embed a file from another repo)
|
49 | - ``ref: STRING`` - a branch, a tag or commit SHA of a repo where current file lives (in case if you want to embed a file from another branch or repo)
|
50 |
|
51 | There is one more thing: you can add to your embedding list any webpage. It could be useful if you want to show how does your web tool works. You need to set type option as ``"htmlpage"`` and assign webpage URL to ``"url"`` property
|
52 |
|
53 | Example:
|
54 | ```js
|
55 | githubEmbed('#root', {
|
56 | "owner": "finom",
|
57 | "repo": "github-embed",
|
58 | "ref": "master",
|
59 | "embed": [
|
60 | {
|
61 | "type": "htmlpage",
|
62 | "label": "Embedded HTML page",
|
63 | "url": "http://example.com/"
|
64 | },
|
65 | {
|
66 | "type": "js",
|
67 | "label": "Webpack config",
|
68 | "path": "webpack.config.js"
|
69 | }, {
|
70 | "type": "js",
|
71 | "label": "Entry point",
|
72 | "path": "src/index.js"
|
73 | }, {
|
74 | "type": "json",
|
75 | "path": ".gh-embed.json"
|
76 | }
|
77 | ]
|
78 | });
|
79 | ```
|
80 |
|
81 | ### Remote settings
|
82 |
|
83 | In case if you want to embed your code on few places and you don't want to break something when a file path is changed (eg you have renamed ``app.js`` to ``index.js``) you can store embedding settings remotely inside a file next to embedded files. It allows to get your embedding always up to date and you'll need to modify it when paths are changed.
|
84 |
|
85 | ```js
|
86 | githubEmbed('.embed', 'https://github.com/finom/github-embed/blob/master/.gh-embed.json');
|
87 | ```
|
88 |
|
89 | Usually I call settings file **.gh-embed.json**.
|
90 |
|
91 | It should contain valid JSON object with data described above. The only difference: you don't need to specify ``owner``, ``repo`` and ``ref`` because these properties will be extracted from settings URL.
|
92 |
|
93 | ```js
|
94 | {
|
95 | "embed": [
|
96 | {
|
97 | "type": "htmlpage",
|
98 | "label": "Embedded HTML page",
|
99 | "url": "http://example.com/"
|
100 | },
|
101 | {
|
102 | "type": "js",
|
103 | "label": "Webpack config",
|
104 | "path": "webpack.config.js"
|
105 | }, {
|
106 | "type": "js",
|
107 | "label": "Entry point",
|
108 | "path": "src/index.js"
|
109 | }, {
|
110 | "type": "json",
|
111 | "path": ".gh-embed.json"
|
112 | }
|
113 | ]
|
114 | }
|
115 | ```
|