Is there a way of embedding the content of a file (from the projects repo) in Githubs markdown?
I have a code sample and want to embed it in the projects readme.
Is there a way of embedding the content of a file (from the projects repo) in Githubs markdown?
I have a code sample and want to embed it in the projects readme.
UPDATE:
It turns out this is now possible if you paste a permalink of the code in your README.md file.
To figure out how to get the permalink, check GitHub's docs on the topic. The permalink and the README.md needs to be in the same repository.
OLD ANSWER:
You can't do that currently. You will have to copy your code sample and paste it between triple ticks in your README file. Here is an example:
``` Ruby class MyClass end ```
This will be static of course and you must update it manually when that code changes.
@Petros's answer is (still) correct, however I got annoyed enough with this that I've created a tool to automate the copying and keep your readme synchronised with the source - https://github.com/zakhenry/embedme
Example usage from the accepted answer
Insert a comment into the code fence
```rb
# path/to/your/file.rb
```
Run npx embedme README.md (this assumes you have Nodejs installed)
The README will be written in-place, with the content of the file following your comment
```rb
# path/to/your/file.rb
class MyClass
end
```
Note that as the comment is still there, you can happily re-run npx embedme README.md again and it won't make any change.
If you have CI, you can also run npx embedme --verify README.md which will return 1 if it detects a diff (this would mean that the source was updated without the readme being updated).
Github now supports rendering permalink in markdown files. example - https://github.com/stevemar/code-reference-in-readme
If you are willing to use github actions, I found Markdown autodocs on Github Marketplace to work for my project.
All you have to do is to add a line in your .github/workflows/your-workflow.yml file:
uses: dineshsonachalam/[email protected]
Then insert these snippets in your Markdown files:
<!-- MARKDOWN-AUTO-DOCS:START (CODE:src=./relative/path/to/code.js) -->
<!-- MARKDOWN-AUTO-DOCS:END -->
I also like the fact that it allows for remote files to be used:
<!-- MARKDOWN-AUTO-DOCS:START (CODE:src=https://raw.githubusercontent.com/username/project/master/path/to/file) -->
<!-- MARKDOWN-AUTO-DOCS:END -->
And also the fact that you can restrict the number of lines printed:
<!-- MARKDOWN-AUTO-DOCS:START (CODE:src=https://raw.githubusercontent.com/username/project/master/path/to/file&lines=1-42) -->
<!-- MARKDOWN-AUTO-DOCS:END -->
Not a direct answer, since GFM doesn't support includes, but I keep a list of projects I found that let you include things with a preprocessing step to produce the final markdown file: realazthat/snipinator: Related Projects.
Disclosure: I wrote snipinator.
Not complete, and not necessarily up to date. Make a PR to README.md.jinja, (see realazthat/snipinator/Contributions) to insert/modify the table, and edit this answer.
| Project | Stars | Last Update | Language | Platform | Similarity X Obviousness |
|---|---|---|---|---|---|
| mdx-js / mdx | 16.8k | 2024/04/17 |
JS | N/A | ⭐⭐⭐⭐⭐ |
| fletcher / MultiMarkdown-6 | 599 | 2023/12/30 |
C | CLI | ⭐⭐⭐⭐⭐ |
| gajus / gitdown | 448 | 2022/03/01 |
JS | CLI | ⭐⭐⭐⭐⭐ |
| gpoore / codebraid | 362 | 2023/10/17 |
Python | CLI | ⭐⭐⭐⭐⭐ |
| amyreese / markdown-pp (archived) | 307 | 2021/09/02 |
Python | CLI | ⭐⭐⭐⭐⭐ |
| zakhenry / embedme | 222 | 2023/11/08 |
JS | CLI | ⭐⭐⭐⭐⭐ |
| DCsunset / pandoc-include | 62 | 2024/04/30 |
Python | Pandoc / CLI | ⭐⭐⭐⭐⭐ |
| BurdetteLamar / markdown_helper | 38 | 2020/03/16 |
Ruby | CLI | ⭐⭐⭐⭐⭐ |
| SimonCropp / MarkdownSnippets | 23 | 2024/04/23 |
.NET | CLI | ⭐⭐⭐⭐⭐ |
| endocode / snippetextractor | 4 | 2014/08/16 |
C++ | CLI | ⭐⭐⭐⭐⭐ |
| polywrap / doc-snippets | 3 | 2023/09/26 |
JS | CLI | ⭐⭐⭐⭐⭐ |
| hxtmike / markdown_include | 2 | 2024/05/09 |
Python | CLI | ⭐⭐⭐⭐⭐ |
| JulianCataldo / remark-embed | 2 | 2022/09/22 |
JS | JS / library | ⭐⭐⭐⭐⭐ |
| xrd / oreilly-snippets | 2 | 2015/10/15 |
Ruby | Ruby / library | ⭐⭐⭐⭐⭐ |
| DamonOehlman / injectcode | 1 | 2021/08/01 |
JS | CLI | ⭐⭐⭐⭐⭐ |
| electrovir / markdown-code-example-inserter | 1 | 2024/02/19 |
JS | CLI | ⭐⭐⭐⭐⭐ |
| andersfischernielsen / Simple-Embedded-Markdown-Code-Snippets | 1 | 2021/02/12 |
JS | CLI | ⭐⭐⭐⭐⭐ |
| ildar-shaimordanov / git-markdown-snippet | 0 | 2021/09/14 |
Perl | CLI | ⭐⭐⭐⭐⭐ |
| teyc / markdown-snippet | 0 | 2024/01/22 |
Powershell | Powershell / function | ⭐⭐⭐⭐ |
| marc-bouvier-graveyard / baldir_markdown | 0 | 2020/06/15 |
Python | CLI | ⭐⭐⭐⭐⭐ |
| facelessuser / pymdown-extensions (snippets) | 903 | 2024/05/05 |
Python | Python / PyMarkdown | ⭐⭐⭐⭐ |
| dineshsonachalam / markdown-autodocs | 176 | 2022/09/19 |
JS | GH Action | ⭐⭐⭐⭐ |
| sethen / markdown-include | 148 | 2017/10/26 |
JS | CLI+config | ⭐⭐⭐⭐ |
| cmacmackin / markdown-include | 95 | 2023/02/07 |
Python | Python / library | ⭐⭐⭐⭐ |
| tokusumi / markdown-embed-code | 28 | 2022/01/05 |
Python | GH Action | ⭐⭐⭐⭐ |
| sammndhr / gridsome-remark-embed-snippet | 2 | 2021/06/14 |
JS | Gridsome | ⭐⭐⭐⭐ |
| NativeScript / markdown-snippet-injector | 4 | 2019/01/24 |
JS | CLI | ⭐⭐⭐⭐ |
| fossunited/markdown-macros | 0 | 2021/06/10 |
Python | Python / Python-Markdown | ⭐⭐⭐⭐ |
| fuxingloh / remark-code-import-replace | 0 | 2022/12/21 |
JS | Remark? | ⭐⭐⭐⭐ |
| szkiba / mdcode | 15 | 2014/02/12 |
Go | CLI | ⭐⭐⭐ |
| devincornell/pymddoc | 0 | 2023/12/01 |
Python | Python | ⭐⭐⭐ |
| shiftkey / scribble (docs) | 40 | 2013/08/08 |
.NET | CLI | ⭐⭐ |
| calebpeterson / jest-transformer-test-md | 2 | 2020/08/21 |
JS | Jest Tests | ⭐⭐ |
| tjstankus / commitate | 0 | 2014/05/29 |
Ruby | CLI | ⭐ |
| GitHub Docs: Creating a permanent link to a code snippet | N/A | N/A | N/A | GitHub | ⭐ |
| javierfernandes / markdown-exercises | 1 | 2017/05/01 |
JS | N/A | ⭐ |
| gatsby-remark-embed-snippet | N/A (55k) | 2024/01/23 |
JS | Gatsby | ⭐ |
| ARMmbed / snippet (docs) | 6 | 2021/08/05 |
Python | CLI | ⭐ |
| drewavis / markdowninclude | 1 | 2024/04/06 |
JS | VSCode Extension | ? |
| romnn / embedme | 0 | 2024/04/18 |
Go | N/A | ⭐ |
I created a tool that keeps your code snippets up to date in Readme:) it can be used either as 🪝 pre-commit hook or as a Github action, and it supports whole scripts, sections of scripts and 🐍 Python objects. It’s extremely easy to use, feel free to check it out! https://github.com/kvankova/code-embedder
You could write a script that will load the source code from a public repo and then show it within the container. Here is an example of an MD file which loads data from github and displays that within a container.
## Welcome to the Simple Alarm Server Example
This page will load and show a code example.
<div id="code-element"></div>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
axios({
method: 'get',
url: 'https://raw.githubusercontent.com/iotify/nsim-examples/master/functional-testing/alarm-server.js'
})
.then(function (response) {
document.getElementById("code-element").innerHTML = response.data;
});
</script>