Render certain file types without requiring multiple extensions?

I’m thinking of porting a bunch of Jekyll content pages to a Middleman site. The problem is that Jekyll inherently knows that .md files get rendered to .html, so I can just name my content files whatever.md and they’ll render into whatever.html.

With Middleman, it seems like files have to be named whatever.html.md so that Middleman knows to render them from .md into .html. Is there any way to tell Middleman that if a file has just a .md extension, to render it into an .html page? I’d love to just use whatever.md and still have Middleman render the pages.

This is similar to the way that Jekyll runs Liquid on every page, whether it’s .html or .md, and a .liquid extension isn’t required.

I’d really love to port over all my content but it’s several hundred files and renaming them all (even bulk renaming) isn’t an ideal solution.

I know Middleman isn’t Jekyll, and I love Middleman so far. But if there’s a way to get all of Middleman’s power while maintaining some of the patterns I’m used to with Jekyll, that would be awesome.

1 Like

Not quite the answer you’re looking for, but a bit of shell script hackery will get you a long way. You can use ${VAR//} in a shell script (or at the command line) to apply a regular expression to an environment variable. Something along the lines of this should do the trick:

SRCDIR="~/Development/Personal/mathie.github.io"
DESTDIR="~/Development/Wossname/woss.name"
for i in ${SRCDIR}/_posts/*.md; do
  target=$(basename ${i/.md/.html.md})
  echo cp $i ${DESTDIR}/articles/${target}
done

Thanks!

That’s certainly useful, but ideally I’d like to have to do no file manipulation. If it comes to that, I can pretty easily rename them in bulk, but it would be nice to just keep the single .md extension.

Oh, I totally agree! In particular, my current sites all have articles with .html.md.erb extensions because they’ve got lumps of ERB in them that need to be processed, too. I haven’t yet found a way to tell Middleman that files ought to be processed as a particular set of Til template types without expressing it in the extensions.

Which is irritating, because I’d like the extensions to just be .md so text editors (particularly on the iPad) know they’re (mostly) just plain markdown.

Normally, Middleman renders like this:

whatever.md
     ||
     \/
  whatever
(interpreted as text/plain)

From my understanding, you want it to ‘assume’ it’s text/html instead.

What if you tried something along the lines of:

::Rack::Mime::MIME_TYPES[''] = 'text/html'

in your config.rb This way, Middleman knows to treat extensionless files as html. Make sure to check if you have other extensionless files that you don’t want it to render as html.

Thanks calebeby! Unfortunately adding that to config.rb doesn’t seem to do anything. The rendered .md files still end up without an extension.

I’m not using Rack, so that might be a factor.

When you go to the extensionless page in the browser, what does it do?

Do the pages appear in http://127.0.0.1:4567/__middleman/sitemap/? I created a new middleman project, and created source/test.md:

---
layout: layout
title: hi
---

This **is** some markdown content

Going to http://127.0.0.1:4567/test shows, in plain text:

<!doctype html>
<html>
  <head>
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta charset="utf-8">
    <meta content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport">

    <!-- Use title if it's in the page YAML frontmatter -->
    <title>hi</title>

    <link href='//fonts.googleapis.com/css?family=Lato:300,400' rel='stylesheet' type='text/css'>

    <link href="/stylesheets/site.css" rel="stylesheet" />
    <script src="/javascripts/all.js"></script>
  </head>

  <body class="test">
    <p>This <strong>is</strong> some markdown content</p>

  </body>
</html>

I added this to my config.rb

::Rack::Mime::MIME_TYPES[''] = 'text/html'

The page now appears in rendered html at http://127.0.0.1:4567/test:

Do you want it to appear as test.html or test? What is wrong with it being generated as test instead of test.html?

Ah, I tried it again and it works. Thanks! Ideally pages would still be generated as whatever.html, since that’s their file type. I’m not sure how this will affect the built site either. I’ll definitely test some more.

I’d like to find a more elegant solution, but this will do as well. :thumbsup:

1 Like

At least for my “blog” pages, I have found limited success using the Rack MIME_TYPES line above with this:

blog.permalink = "/{year}/{month}/{day}/{title}.html"