I quite frequently would like to have parts of code work as links (e.g. uncommon methods linking to relevant documentation). Achieving that is possible with HTML <pre> and explicit <a> tags; however, the syntax highlighting doesn't seem to work on these manual <pre> blocks. Is there a way to have both?


If you're really up to it, you can enclose a <code> tag inside the <pre> tag. We don't have syntax highlighting here on Meta, so you can't see it, though.

I caution against this, even though it works. Most people probably will not expect hyperlinks to be inside code blocks because most programs don't include functional hyperlinks in their bodies, so it will not be as intuitive to readers as including the hyperlink before or after your code not unlike what you did here.

if (true) {
    WebObject R = 9;
} else {
    Hyperlink B = 5;

if (true) {
    WebObject R = <a href="http://gaming.stackexchange.com">9</a>;
} else {
    <a href="http://stackoverflow.com">Hyperlink</a> B = 5;

For a demonstration of how it looks on Stack Overflow, here's an image. Notice how the hyperlink has the same color as a standard formatting for both the number and the type. It's not at all visible that it is a hyperlink. I did test this on an actual post, and the rendering is the same as the preview. But I probably should've done it as an edit suggestion, since that would've been properly rejected. Ah well.

Shows source code for this, and the rendering on Stack Overflow.

  • I didn't know this!! nice! – Trufa Feb 8 '11 at 14:32
  • Would it be worth having some styling support as a feature request? – Jakub Hampl Feb 8 '11 at 16:56
  • @Jakub People still won't expect hyperlinks in the code block, and furthermore, wouldn't that break the syntax highlighting that you're gunning for? If you still want to make a feature request to see what people at large thing, please feel free to do so. My own thought is that it won't get much headway, though. – Grace Note Feb 8 '11 at 16:59
  • Well I thought that having something underlined (though not necessarily in link color) makes it kind of obvious. Also quite a few web syntax highlighters do this automatically and can be really helpful for beginners. – Jakub Hampl Feb 8 '11 at 17:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .