Quoting Code

23 Jul 2025

While writing the previous article for this blog, I realised that I did not have an easy way to retrieve code snippets (entire files or just some sections) from GitHub. I needed something that would allow me to retrieve the code from an external source without having to copy and paste it into the post.

I looked online, but I could not find much. prismjs does offer this feature, but it also comes with syntax highlighting, whereas I wanted to use highlight.js for that.

To solve this problem, I created a very small JavaScript library that can fetch code snippets from a specified URL and insert them into the post. It’s called codequote.js, and you can find it on GitHub.

Here’s a sample usage:


This example is itself ‘quoted’ from the README of the library’s GitHub repository. It is also possible to specify a range of lines:


This approach has two advantages: it allows you to keep the code in a single place (which is DRY), and makes it possible to update the code in the post without having to edit the post itself. When the source code changes, the post will automatically update.

Unfortunately, there are limitations. When quoting specific sections, updating the source can result in the line numbers changing, which can break existing quotes. For this reason, when a file is likely to change, it is better to quote it in its entirety. Usually the source code for blog posts is stable, so this is not a big issue. On the other hand, in this post I have quoted code from a README file, which is a bad idea as the README is very likely to change (but I’ll take the risk).