Movable Type plugin: ResolveURLs

I noticed the other day that some of my RSS feeds did not validate, because they contained relative URLs. In fact, it occured to me that using relative URLs in weblog entries in general can be dangerous, because an entry can appear in multiple locations, each with a different base (using absolute paths fixes this, although not the RSS feeds, since those need absolute URLs).

So I wrote an MT plugin that adds a global filter resolve_urls. Enabling this in a template tag goes through and makes all links absolute, resolving any relative URLs against the site URL of your weblog. I’m using it now wherever <$MTEntryBody$> appears on my site, both in HTML and RSS.

8 thoughts on “Movable Type plugin: ResolveURLs

  1. Very nice! Just a couple of little problems:

    You might want to support single-quotes as well as double, since some people single-quote URLs (some people leave the quotes out entirely, but supporting that would probably mess up URLs for people who forget to encode spaces, and you have to draw the line somewhere).

    Because MT screws up in places and assumes that your site URL doesn’t end with a slash, so that if you do end it with a slash you get things like links to stylesheets that go to example.com/path//site-styles.css, an awful lot of people don’t end their site URL with a slash, and it looks like in that case URI ignores the path, and turns the combination of example.com/path + path2/file.html into example.com/path2/file.html, so you should probably check for a terminal slash and add it if there isn’t one.

  2. Interesting — I don’t think I knew that single quotes were legal in HTML. I knew you were allowed to leave them out sometimes. I guess I need to read up on SGML some more. I’ve only ever used double quotes, and my weblog’s templates specify XHTML, which I’m pretty sure requires double quotes. If people do use single quotes, though, and browsers support it, maybe I should add that.

    I’m not sure about messing with the site URL, though. The default templates that came with my version of MT (2.51) all seem to assume <$MTBlogURL$> is a directory URL. Was this not true for earlier versions?

  3. Looks to me like single quotes are allowed in XHTML (the XHTML spec doesn’t say anything about it, but the XML spec says in its charming way “Definition: the content of the AttValue (the text between the ‘ or ” delimiters) as the attribute value.”). An unquoted attribute in HTML would only work for a filename, since you can’t have a slash in an unquoted attribute (though browsers do allow it), so I’d skip the unquoted case.

    I’m not quite sure when they got rid of all the issues with BlogURL and slashes, but it has to have either been a 2.2x or 2.5x fix. Before that, there were places with (the more readable) <$MTBlogURL$>/site-styles.css rather than the current <$MTBlogURL$>site-styles.css, and while I see a lot of people who just ignore the fact that they are getting example.com//foo.html links, I assume I’m not the only one who solved the problem by just leaving off the final slash rather than digging through every template to get rid of the doubles. Is there a case where your plugin adding a slash to $blog->site_url if it doesn’t have one would be a bad thing?

  4. Oh, yep, AttValue is defined as taking either ' or ". So that’s legal too. I thought I’d read somewhere that XML allowed only double quotes — I guess it’s only the bare literals that it disallows.

    And given that Movable Type always seems to assumes the site URL is a directory, you’re right that it doesn’t really hurt to add a trailing slash. I was thinking that you could set your site URL to something like http://www.example.com/index.html, but I guess that would break Movable Type in other ways first. Seems like a reasonable addition to me.

    I’ve updated the ResolveURLs page with version 1.0.1, which has these changes.

  5. Pingback: Unix Gal

  6. Are you still working on this plugin?
    I get this error when I activate it :(
    What am I doing wrong?
    Help me please!

Comments are closed.