50

Is it possible to embed gits into README.md file that resides in a github repo?

Something like:

<code id="gist-3167145"></code>
Francisco Luz
  • 2,225
  • 1
  • 21
  • 31

4 Answers4

27

No, sorry, that is not possible. You will have to either have a link to it in your README.md or copy its contents.

Github Flavored Markdown will show you what you can put in your README.md file.

dave
  • 2,162
  • 4
  • 22
  • 20
philipvr
  • 4,454
  • 3
  • 31
  • 44
  • 3
    Thanks philip, I was hoping that somehow I had missed it when reading the documentation or the documentation itself could be outdated. :-D That would be a cool feature though. – Francisco Luz Jul 24 '12 at 00:58
  • [There is a closed issue](https://github.com/github/markup/issues/199) requesting this feature. Unfortunately it looks like it won't be available anytime soon. – mathielo Oct 20 '15 at 17:20
24

Update : My answer works with github pages, built via jekyll. I use the script tags in markdown which is then processed by jekyll.

Since markdown supports html, one can simply use the <script> tag to embed gist.

Simply copy the embed url of the gist provided by github

enter image description here

..and paste it in you markdown file.

Example : Copy the below and paste in your markdown file.

<script src="https://gist.github.com/nisrulz/11c0d63428b108f10c83.js"></script>

..and this is what you will get

enter image description here

Community
  • 1
  • 1
Nishant.
  • 4,396
  • 2
  • 21
  • 33
  • With GitHub [markup](https://github.com/github/markup/blob/master/README.md) (#2): `The HTML is sanitized, aggressively removing things that could harm you and your kin—such as script tags...` – philipvr Aug 19 '15 at 13:21
  • @philipvr agree with that doc, but this still works and solves the OP's question. – Nishant. Aug 25 '15 at 16:46
  • 2
    How does this solve the OP's problem? If you try to embed a gist this way for a GitHub repo's readme.md file, the embeded gist will not be shown. – philipvr Aug 25 '15 at 18:11
  • 1
    @philipvr i get what you were pointing out. I updated my answer to reflect the environment in which this works for me. – Nishant. Aug 25 '15 at 22:19
4

You can do it if you are using a markdown preprocessor such as Gitdown:

/**
 * Resolve Gist (https://gist.github.com/)
 *
 * @param {Object} config
 * @param {String} config.id Gist ID.
 * @param {String} config.fileName Gist file name. Default to gistfile1.txt.
 */
gitdown.registerHelper('gist', {
    compile: function (config) {
        config = config || {};
        config.fileName = config.fileName || 'gistfile1.txt';

        if (!config.id) {
            throw new Error('Gist ID must be provided.');
        }

        return new Promise(function (resolve) {
            var https = require('https');

            https.get({
                host: 'api.github.com',
                path: '/gists/' + config.id,
                headers: {
                    // User agent is required to communicate with Github API.
                    'user-agent': 'Gitdown – gist'
                }
            }, function(res) {
                var body = '';

                res.setEncoding('utf8');

                res.on('data', function (d) {
                    body += d;
                });

                res.on('end', function () {
                    var gist = JSON.parse(body);

                    if (!gist.files) {
                        throw new Error('Gist ("' + config.id + '") not found.');
                    }

                    if (!gist.files[config.fileName]) {
                        throw new Error('File ("' + config.fileName + '") is not part of the gist ("' + config.id + '").');
                    }

                    resolve(gist.files['gistfile1.txt'].content);
                });
            });
        });
    }
});

Then in your markdown your would reference the Gist using a JSON hook, e.g.

{"gitdown": "gist", "id": "d3e4212c799252bac5fa"}

This feature should become part of the Gitdown in a near future (there is an open issue, https://github.com/gajus/gitdown/issues/7).

Gajus
  • 55,791
  • 58
  • 236
  • 384
3

This is do-able in 2017 when using GitHub Pages and a Jekyll theme:

See https://gist.github.com/benbalter/5555251 from @benbalter

Simple as: {% gist 123456789 %}

Dan
  • 1,837
  • 16
  • 21