是否可以在存储在GitHub仓库中的README.md文件中嵌入gist?
像这样:
<code id="gist-3167145"></code>
不好意思,这是不可能的。你必须在README.md中有链接或者复制其内容。
Github Flavored Markdown会展示你可以在README.md文件中放置什么。
<script>
标签来嵌入gist。将其复制并粘贴到您的Markdown文件中。
示例:复制以下内容并粘贴到您的Markdown文件中。
<script src="https://gist.github.com/nisrulz/11c0d63428b108f10c83.js"></script>
这就是你会得到什么
?file=file_name_here
。例如,<script src="https://gist.github.com/nisrulz/11c0d63428b108f10c83.js?file=test.sh"></script>
。 - undefined如果使用GitHub Pages和Jekyll主题,这在2017年是可行的:
请参见https://gist.github.com/benbalter/5555251(来自@benbalter)
只需简单地输入:{% gist 123456789 %}
{% gist 5555251 gist.md %}
。如果gist包含该文件名,则只会显示该文件名。 - undefined如果你在使用Markdown预处理器,比如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);
});
});
});
}
});
然后在你的markdown中,你可以使用JSON hook来引用Gist,例如:
{"gitdown": "gist", "id": "d3e4212c799252bac5fa"}
这个功能应该在不久的将来成为Gitdown的一部分(有一个开放问题,https://github.com/gajus/gitdown/issues/7)。