Share a code snippet as a link with syntax highlighting
Put the code in one HTML file that loads highlight.js, upload it and send a link. The reader sees colored, readable code in any browser, with no GitHub account, gist or code editor.
Code pasted into email or chat loses its indentation, wraps mid-line and turns quotes into curly quotes. A .py or .js attachment opens in whatever the reader has, or downloads and sits there.
One HTML file that holds the code and a highlighter fixes this. Hosted at a link, it opens in any browser with the code colored by language and the indentation intact, which suits code review with someone outside your repo, a snippet for a client or a worked example for a class.
Step 1: build the HTML file
Create snippet.html with these parts:
- ✓<!doctype html>, <meta charset="utf-8"> and <meta name="viewport" content="width=device-width, initial-scale=1"> in the head.
- ✓In the head, a theme: <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/styles/github.min.css">. Swap github.min.css for github-dark.min.css for a dark theme.
- ✓A style rule so long lines scroll instead of breaking the layout: pre { overflow-x: auto; }
- ✓In the body, the code inside <pre><code class="language-python"> and </code></pre>. Change python to javascript, sql, bash, typescript or another language name.
- ✓At the end of the body: <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/highlight.min.js"></script> and then <script>hljs.highlightAll();</script>
- ✓Tested with highlight.js 11.9.0 in Chrome: a Python function came out with keywords, strings, numbers and the function name each in its own color.
Step 2: escape three characters
The code sits inside HTML, so the browser reads a < in your code as the start of a tag. Replace & with & first, then < with < and > with >. Most editors can do this with find and replace; skip it and a line such as if a < b can vanish from the page.
Several snippets fit in one file: give each its own <pre><code> block with a heading above it. highlightAll colors every block on the page.
Keep it to one file
The theme and the highlighter load from the jsDelivr CDN. To make the file work with no outside requests, paste the contents of the .css file into a <style> tag and the .js file into a <script> tag in place of the links. More on this: making an HTML file self-contained.
Upload it and send the link
Drag snippet.html onto the FileShare Pro dashboard and copy the link. The code opens in the browser, and viewers need no account.
Turn on comments and reviewers can leave a note on the page after typing a name, such as a question about one line. When the code changes, upload the new file over the old one; the link stays the same: updating a shared link without changing it.
Related: sharing API docs as a link, a CodePen as a link and a Jupyter notebook as a link.
Frequently asked
Put the code in a <pre><code class="language-..."> block in one HTML file that loads highlight.js, upload the file and send the link. It opens colored in any browser.
A < in the code is read as an HTML tag. Replace & with &, < with < and > with > inside the code block.
No. The link opens in a browser, with no account.
Yes. The free plan hosts up to 50 files of up to 100 MB each, with view counts and comments included. Password-protected links are on Pro.
Drop the file, copy the link — live in seconds, free plan included.
Start sharing free