Org Mode and Broken Links

As a couple of you have pointed out to me recently, HTML links in my posts are sometimes getting broken. That happens when the link has a parameter specified with a question mark and equal sign. For example, here’s a link to one of my blog posts that gets affected

Building a Blog with Org Mode

For some reason Org Mode has started escaping equal signs in links so that the above gets turned into

http://irreal.org/blog/?p%3D2168

which is, of course, incorrect. This happens in org-link-escape, which is called by org-make-link-string when I insert a link with 【Ctrl+c Ctrl+l】. No problem, I thought, they must have added the equal sign to the list of characters to escape; I’ll just fix it up with a buffer local variable or something. Unfortunately, when I checked I discovered that the equal sign has always been there and that neither org-link-escape nor org-make-link-string have been changed in over a year.

I put in a little bit of time trying to track down what changed (it didn’t do this in Org 7 and maybe even not in the early Org 8 versions) but couldn’t find anything. I’ll keep looking but in the mean time, here’s a little bit of Elisp that I threw together to fix things up:

(defun jcs-clean-link ()
  "Clean up munged = in an Org link."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (search-forward-regexp "\\[\\[[^]%]*\\(%3D\\)[^]]*\\]" nil t)
      (replace-match "=" nil nil nil 1))))

I can call that before exporting the Org file to HTML and everything will be fine. I may advise the exporting function to call jcs-clean-link for me but for now I’m doing it manually.

The whole thing has not been a total waste of time, though, because I learned two new things. First, calling 【Meta+xvisible-mode shows the links as they really are rather than just the description part. I used to switch to text-mode to do this which is sort of a pain so this is a good find for me.

Second, although it doesn’t help me with the current problem, I discovered that you can set Org variables affecting export—even if they aren’t one of those supported by an option—with the BIND directive. To set variable to value, you just add the line

#+BIND: variable value

to your Org file. You also have to set org-export-allow-bind-keywords to t for this to work. Back when I thought my links were getting munged during export, I speculated that maybe XHTML Strict required the escaping and I was able to disprove that by using BIND to cause the file to be exported as HTML4.

If any of you know what’s going on with the links, please leave a comment.

This entry was posted in General and tagged , . Bookmark the permalink.