There are several different ways you could get Markdown-based onto a blog:
- Static site generators
- I looked at a number of static generators, like Gollum from github and Nikola. Both of these were conceptually appealing, but suffered from similar issues, most notably the usual run of configuration and install issues the come with any web-world endeavor these days. I had a hell of time getting either one working install on the macbook I use for most of my writing and eventually decided that I wasn’t that interested in wrestling with those kind of things into the future. More importantly, I’m too busy to really admin my own server - I want a hosted service. If you’re more into running your own server – and you don’t mind sleuthing out things like getting the right Ruby package manager setup running – they could both work fine.
- Markdown-based blogs
- There are a few markdown based blogging hosts out there. The one I looked at most carefully was Silvrback, and I enjoyed it a good deal. The writing was clean and simple and it came with a built-in syntax highlighting: the biggest damn hassle about all these posts has been getting the code samples into a reasonable format without hopping around between multiple sites and pasting a lot of esoteric formatting codes in HTML by hand. So, I had a good experience with Silvrback and I seriously considered switching. If you’re just getting started it’s definitely worth a look, particulary for technical blogs like this one.
- Custom markdown
- The last option would be trying to take control of the markdown to HTML conversion process and spit out a minimal set of HTML that would play nice with Blogger but need no hand-work to make it pretty (you’ll recall that I bitched about my earlier efforts getting bogged down in
<p/>
vs<br/>
and other HTML nonsense I don’t want to think about. There are lots of Markdown generators out there at varying levels of sophistication, but I also don’t want to think too much about micromanaging those.
Sticking with Blogger for hosting means figuring out to reconcile my markdown text and Blogger’s style sheets. Luckily, it turns out that Sublime Text has a nicely configurable Markdown preview plugin. I love Sublime anyway – it’s my go-to text editor for everything except heavy-duty C# and Python (VS and PyCharm, respectively, btw).
In ordinary use, MarkdownPreview “bakes” your Markdown info nicely formatted HTML with all sorts of swanky CSS formatting – which is precisely what makes things hard for Blogger. With a little extra work, however, you can get it to produce a stripped-down HTML with the right tags and links but without all the extra CSS that conflicts with the Blogger template.
It took a bunch of fiddling to figure out how it works, but once I got over the n00bishness it turns out to be very simple. Here’s a step-by-step to setting it up for yourself.
This is pretty simple using Sublime’s excellent Package Manager, so I’ll skip the details ( 1. Install the MarkdownEditing and MarkdownPreview packages for Sublime Text.here’s some help if you need it.)
You want to make a simple HTML template that Sublime can render Markdown into. You can add some customisations if you like but the main business of the template is to prevent MarkdownPreview from filling in all of it’s own CSS styles. 2. Create a simplified HTML template
Luckily, it’s easy to do this: if you don’t add a placeholder for the HTML
<head>
tag, MardownPreview can’t stick all the styles in there. This means you can force Sublime to give you a stripped-down output that won’t override your Blogger template.If all you want to do is get uncluttered HTML, you can just use
{{ Body }}
Since I do a lot of code samnples in this blog, I opted to include a little bit of custom CSS to pretty it up. MarkdownPreview uses Pygments to generate highlighted code. Pygments marks up the html with a bunch of custom HTML classes for different language parts, and you just need to provide some CSS that will decorate those guys. There are plenty of examples that work with Pygments which you can cut-and-paste like this set from Rich Leland.
For myself, I grabbed a free one that looked a lot like the highlighting style on Github – to keep my template simple I put it in a public folder on DropBox and used an HTML link to include it in the blog. Feel free to use it if you’d like, but don’t link to it: I may keep fiddling with it and you don’t want my changes!. You can also get it here
<head> <link rel="stylesheet" title="github" href="https://dl.dropboxusercontent.com/u/2977490/github.css"> </head> {{ BODY }}
Open up the user settings for MarkdownPreview (Package Settings > Markdown Preview > Settings – User). This will be a blank file the first time you open it, but like all Sublime settings files it’s a JSON file. We just need to tell the MarkdownPreview plugin to use our template: 3. Customize the MarkdownPreview package.
{ "enable_highlight" : true, "enable_pygments" : true, "html_template" : "C:/path/to/blog_template.html", "skip_default_stylesheet": true }
html_template
, which is the path to the template file from step 2, and skip_default_stylesheet
, which tells MarkdownPreview not to insert all those other styles. With the template and settings in place, create some Markdown. In Sublime, generating the HTML uses the same ‘build’ model as compiling code. So you first set the build system to Markdown (Tools > Build System > Markdown). Then you build it (CMD + B or Tools > Build), which by default will create an HTML file alongside your markdown file. You can view the results in a browser directly or just open the HTML file in Sublime. 4. Build your blog!
What you should have at this point is nicely formed HTML without tons of extra CSS (and, if you added code-highlight styles, some colorful highlited text as well). You can just cut the HTML and paste it straight into the HTML view in Blogger and you should have a perfect, Blogger friendly but Markdown-clean and easy blog entry. Once you’ve done the setup steps once, your only job is a single cut-and-paste (you can even configure MarkdownPreview to copy the HTML to the clipboard instead of writing to a file!).
This is not the perfect blogging system, by a long shot – but it’s a whole lot better than Blogger’s slow GUI, and it offers flexible code highlighting for all of the languages that Pygments supports. Once all the spadework is done, posting is as simple as writing in Markdown, hitting the build button, then pasting into Blogger. So far I’m finding it a huge improvement. Wrapup
But of course, TA’s are never satisfied so I bet this will come up again some other time…
No comments:
Post a Comment