Best way to use markdown + erb/liquid + extensions? #448

Closed
stevenringo opened this Issue May 25, 2012 · 11 comments

Comments

Projects
None yet
4 participants

I was initially going to use Octopress but in the end much preferred Middleman's approach and flexibility.

The thing I think that makes Octopress so appealing is its ability to use liquid templates inside markdown and therefore being able to use macros/plugins to do fancy tricks to the output, e.g.


---
layout: page
title: "Codeblock"
date: 2011-07-22 09:13
sidebar: false
footer: false

---

With this plugin you can write blocks of code directly in your posts and optionally add titles and links.

#### Syntax

    {{ "{% codeblock [title] [lang:language] [url] [link text]" }} %}
    code snippet
    {{ "{% endcodeblock" }} %}

#### Example 1

    {{ "{% codeblock" }} %}
    Awesome code snippet
    {{ "{% endcodeblock" }} %}

{% codeblock %}
Awesome code snippet
{% endcodeblock %}

I don't really care too much for liquid, but would like to find a way to run MM extensions or even partials from within pages that are written in Markdown.

Any ideas on how to achieve this will be most welcome. Thanks again for the awesomeness that is MM!

Owner

tdreyno commented May 25, 2012

Middleman 3 allows stacking templating engines by appending additional extensions. For the above code you could have a file named: my-page.html.markdown.liquid

The liquid portion will be processed first, then markdown and finally output as HTML.

I often use this with ERb to add partials into markdown files.

@tdreyno tdreyno closed this May 26, 2012

Contributor

dannguyen commented Jul 11, 2015

Apologies for bumping up a 3-year-issue, but my question didn't seem to justify making a new issue.

For the various pages in my project, sometimes I use erb, sometimes I don't. Sometimes in some pages where I didn't have erb, I add it in later. The finangling of file extensions is a little inconvenient, but the bigger inconveniences is that dev environments, by default, have particular syntax highlighting for .erb versus .md...often times, my use of erb within a specific page (rather than a template) is so minimal that having my editor use the .erb syntax highlighting for a 99%-markdown-file is...cumbersome.

So my question is...is there a way to hook into the Middleman processing so that I can have all .md files be processed through .erb by default?

Owner

tdreyno commented Jul 13, 2015

You mean you want doc.html.md to be processed as doc.html.md.erb? No, I'm afraid not. We believe being explicit is always better than magic.

Contributor

dannguyen commented Jul 14, 2015

Oh don't get me wrong, it's definitely not something that should be at all a default option...I was just wondering what's the most efficient way to tack it on...do I just follow the patterns of the existing Middleman::Renderers module and hook it in as an extension? Sorry not looking for explicit technical support, just wanted to make sure there's not some kind of impassible technical barrier, as I'm sure writing a simple extension will be more than enough challenge for me :)

Owner

tdreyno commented Jul 15, 2015

The sitemap is filled with Resource objects. These know how to render themselves. You could write a sitemap_manipulator to look at the source of each resource, if it matches the correct extension, replace (or extend) the resource with a new implementation that runs through your own render logic and returns the correct string.

After fiddling with the sitemap for hours, I decided to simply overwrite the renderer. Not pretty, but it works: https://gist.github.com/jgillich/ef9347b01b6e7e5b89fc

Owner

tdreyno commented Oct 12, 2015

Could also be possible to create a .myfancymarkdown or something and write a renderer that targets that extension and does both render steps.

Contributor

dannguyen commented Oct 12, 2015

Or just call it .mderb :). Thanks @jgillich!

A custom extension is not always an option, in my case I wanted to use http://prose.io/ and they only detect .md as markdown. I think it would be really nice if we were able to set extension aliases in the config (like ".md" => ".md.erb").

Owner

tdreyno commented Oct 12, 2015

@jgillich Basically, that's just not how Ruby's extname method (and the libraries built on it like Tilt and Sprockets) work. It would require a ton of extra work to override . as the separator :(

I could be possible to use the sitemap to replace anything with a source_file containing .md.erb with a custom Resource who's got a fancy render method.

Oh I don't know much Ruby in general, so I have no idea how hard it would be to implement. I'm sure there are good reasons for the way things are. :)

Manipulating the sitemap was how I started, but I never figured out what to put in that render method. All the core things rely so much on the path, it seemed impossible to get the default rendering without rewriting most of it. At some point I was even desperate enough to generate symlinks on the fly. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment