Attribute references

John Gruber gruber at fedora.net
Sun Jan 9 13:19:39 EST 2005


Here's an idea that struck me yesterday while out for a walk. (I
should take more walks.)

People, including me, want the ability to assign classes and id's to
block elements in Markdown. I.e. it'd be nice if you could somehow
assign id's to headers in long documents, so you can create inline
#anchors.

But I kept thinking that adding support for two new attributes --
`class` and `id` -- had the potential to junk up the syntax. Plus,
what about other attributes? What about `style`?

So I took a step backward, and realized the better solution would be
a single syntax for applying tag attributes. Imagine this:

    ## The Lousiana Purchase  {lp}
    
    Napolean was, unquestionably, a brilliant military strategist, but
    he was not a very good real estate manager.
    
    {lp}: id="louisiana-purchase"  class="h2-section-start"

Which would generate:

    <h2 id="louisiana-purchase"  class="h2-section-start">
    The Louisiana Purchase</h2>
    
    <p>Napolean was, unquestionably, a brilliant military strategist, but
    he was not a very good real estate manager.</p>


The similarity to link references is, hopefully, obvious. I'm
thinking there would *only* be this reference-style syntax, with no
provision for assigning tag attributes inline in the article text
itself. (If you want to do that, just use real HTML tags.)

One advantage of this is that for something like classes, you can
define one class attribute defintion, and reuse it on as many
elements as you need.

One disadvantage is that you could do the same thing with id's,
which of course would produce invalid HTML if the same `id` value is
used on more than one element per page. I think we'll just have to
trust the user to know what they're doing here -- if you don't know
the rules of HTML, it's unlikely you'd ever use this anyway.

The only thing I'm not sure about is what the rules should be for
how these attribute references are assigned. For headers, I'm
imagining they would be assigned by putting them at the end of the
header text:

    ## Like This  {foo}


    Or Like This  {foo}
    -------------------


Although I'm not sure what to do about #-style headers that use
closing #'s. I guess we could support both:

    ## This {foo} ##

    ## That ## {foo}


Assigning to normal paragraphs should be easy, too. Just stick the
`{...}` at the end of the paragraph.

But, how would one assign attributes to an entire list? Not just to
individual items in the list, but the `<ol>` or `<ul>` tags?

Or to a code block or blockquote?

The best I can think of for elements such as these is that the
`{...}` could come on a line by itself after the element. (Or maybe
before?)

So to assign an attribute reference to a list:

    * This
    * That
    * The other
    
    {foo}
    
    Now back to regular text.
    

Comments/suggestions welcome.

-J.G.


More information about the Markdown-Discuss mailing list