Markdown Heading Level PHP Function

Published Tuesday, August 07, 2007 in PHP, Semantics

If you use Markdown-syntax for articles/comments or whatever like I do you know that the #-symbol defines a heading.
But depending on where an article is displayed on a page you may want to adjust the heading-levels.
On exscale.se for example the maximum allowed heading-level for an article on the index-page is 4 (<h4>). This is because the title of the article on the index-page is a <h3>.

I've created a little php-function that adjusts markdown heading-levels to whatever the user wants, so that the same article can be displayed on different pages where heading-levels aren't the same.

Usage is quite basic, just go $str = markdownHeadingLevel($str, $level); where $str of course is the string containing the markdown-text and $level the maximum allowed level.

All the headings in the string will be adjusted relatively. If the highest heading in the string is a <h1> and the lowest a <h4> and you set the level to 3 the highest will become a <h3> and the lowest a <h6>.
It works the other way around as well, a string with h4 -> h6 headings will be turned in to h1 -> h3 if you pass 1 as the level.

Obviously you need to run markdownHeadingLevel() before running markdown().

Please have a look at 100% Valid XHTML Database Output With Markdown and HTMLEntities for markdownHeadingLevel's as well as ns's source codes.


Bookmark It

  • del.icio.us
  • Digg
  • Furl
  • Google
  • Technorati
  • Ma.gnolia
  • BlinkList
  • Blogmarks
  • Rojo
  • StumbleUpon


Post It

From June 02 to April 23

  1. Mogrify is what you're looking for if you want to convert multiple images to multiple other images in ImageMagick
  2. Tommorrow, finally, the inFamous demo will be friggin availble on PSN!! Suweeeeeeeeet
  3. Fuck canvas is cool, I've started playing around with old 3D-shit again :)

May 2012

S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31


Recent Comments

  1. quick quid on Bye, bye exscale!:
    jhtmpl...
  2. cheap viagra on Bye, bye exscale!:
    nsikku...
  3. payday loans on Bye, bye exscale!:
    ixrhvy...
  4. buy cialis on Bye, bye exscale!:
    lqlokhr...

Style Switcher

The style switcher allows you to change the look and feel of exscale.se.
Only CSS and JavaScript are changed. The XHTML stays the same.

For more information about the styles, check the styles page.


Categories


Random Quote

We all are the bird of online and online has a position of sky. So come here and use this sky for fly. - Unknown


Random Images




Answer This!

Do you find the "scroll-pagination" annoying? (If you don't know what it is, scroll to the bottom of the first-page)


Blog Roll