I always had a hard time to add a single file from a GitHub gist on Medium. Everywhere I found was to use the link to the gist and that adds all the files in the gist.
I used to make different gists for every file I needed to add to the article. That made managing the gists for an article, a nightmare.
That did not stop me from trying to find a way to embed a single file from the gist. I mean it doesn’t make sense that the Medium platform does not allow to embed a single file from the GitHub gist.
Finally, I was able to find this post on Stack Overflow, surprisingly from 8 years ago where someone asked “How do I embed a single file from a GitHub gist with the new gist interface?” and there Mojtaba Hosseini explained it in great detail on how to accomplish it. Due credit to Mojtaba Hosseini.
How do I embed a single file from a GitHub gist with the new gist interface?
Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share…
You can go through the solution on Stack Overflow as well.
I decided to write this article and thought that writing this article on Medium will be the best way to help someone find a solution to the issue.
Here are the steps:
- Copy the link to the gist.
- Paste it in a notepad, this removes any hyperlinks on the text.
- Append the following to the link
gistfile.extis the name of the file in the gist you want to add.
- Copy the text from the notepad.
- Paste it on Medium as
paste as plain text.
The important point here is to append the gist URL with
Let's see what happens when you copy a link to a gist and append
Did not work. There is more to it. Medium doesn’t really take a URL without a hyperlink. Therefore, you can’t really copy and paste the gist URL and then append
?file=file1.py to it.
Here’s how to do it.
Paste the URL in notepad and append
?file=file1.py to it. Copy the whole text and paste it in Medium.
Voilà it works.
Here are the 3 gists inserted from a single gist.
Its simple just a little tricky. Hope this helps you.