Recently a customer wrote asking whether I would consider adding a feature to MarsEdit, so that as you’re writing a blog post, you could also keep an eye on exactly what the resulting HTML will look like. This feature would be handy for folks writing in Markdown or in the Rich Text editor, but also for folks writing in plain HTML, depending on what “preview filter” settings they have configured.
I told the customer I could see the value of such a feature, and that I would consider it for a future update. But the idea stuck with me until I suddenly realized that in fact MarsEdit could support such a feature today, without any update to the app.
Currently MarsEdit’s editor supports writing in either plain-text markup such as HTML or Markdown, or in Rich text. In any of these cases, the final result can be previewed in MarsEdit’s preview window, where the results of e.g. running Markdown or converting from rich text to HTML are shown in the context of a “preview template,” essentially a custom HTML document that users can tweak e.g. to match the appearance of their blog.
MarsEdit’s default preview template contains a body section that looks like this:
<div style="padding:10px 20px;"> #body# #extended# </div>
The #body# and #extended# placeholders are replaced with the actual content of the post while you are writing it. For the purposes of this proof-of-concept, I decided to just focus on the body text, since many people don’t use “extended” entries. I added a bit of HTML in the template to hold the source view I plan to add:
<hr> <pre style="white-space:pre-wrap"> <div id="sourceContent" style="padding:10px 20px;"> </div> </pre>
Now the only trick remaining is to pay attention to changes and update the contents of the “sourceContent” div above with my content’s HTML. This required a little inside-knowledge that would have been admittedly challenging to discover on one’s own. The way MarEdit currently handles updates to the preview window content is to load the HTML template once, and then as you type, recompute and replace just the “body” of the HTML document. So I added a new script element outside the scope of the body tag, where it won’t get replaced constantly:
Here’s what the MarsEdit preview window looks like now, with the live source preview in effect:
While this isn’t as polished as a dedicated feature for previewing source, I found it interesting that I was able to come up with a creative solution using only the powerful and flexible preview infrastructure of the app. If you’re interested to try out the preview, you can download it here. Just copy and paste the text into MarsEdit’s preview template editor.
Let me know if you come are inspired to come up with any creative preview window solutions of your own!