Compile-time link checking

Hi, I really like Middleman. Thanks for making it. It’s neato!

When I compile my site, I’d really like it if relative links were checked to ensure that they weren’t broken.

In fact, I don’t especially want to have to type them at all. I’m not always sure whether to put a ‘/’ on the front, for example. I don’t want to have to think about it in a “don’t make me think” kind of way. I just want to call a method on a sitemappy data structure, similar to Rails *_path and *_url helpers. And that might be a way to achieve the checking functionality - via Ruby method look up.

Thoughts?

Are you already using the link helpers (http://middlemanapp.com/helpers/#toc_1)? Those should create relative link to files in your project for you. Or do you want additional checking of links after that?

Yep, I use the link helpers.

What I’m talking about is the fact that in Rails, if I muck around with routes and related things, a template that has the following link:

= link_to 'My Page', my_page_path

will, when rendered, cause an exception and alert me to the fact that it’s broken, by virtue of receiving a NoMethodError, without me clicking on it. This affords me a little piece of mind, ahead of clicking on every single link, that they aren’t all totally broke.

So I thought, Middleman could probably give me a similar or greater piece of mind and have something along the lines of

def link_to(label, href)
  if is_relative?(href) and not is_in_sitemap?(href)
    raise InvalidLinkError, 'useful information'
  end
  # stuff link_to normally does
end

There’s nothing built-in, but you could write something easily enough.

def in_sitemap?(path)
  sitemap.resources.any? do |resource|
    resource.path == path
  end
end

You’d probably want to memoize that, if speed became an issue.