CornerPixel Blog RSS 2.0

# Wednesday, September 09, 2009

Today I was trying to change how the Table of Contents drop down menu worked in MindTouch Core.

The drop down menu is dynamically created as a list of headers on the page.  For example if your page has an h1 header of Hello and an h2 header of You, the drop down menu would be.
1. Hello
    1.1 You

mindtouch-toc

As you see above, “Hello” and “You” are both links to their respective header on the page.

This is great for wiki's, but for the intranet site I am working on, we are using the headers manly for design this could be a problem.  For example, it is not uncommon to have an h2 of “Hello” and an h5 of “You” because the odd numbered headers I have styled with a yellow background, but I still want it to appear under the h2 in the Table of Contents. 

So in the above example the TOC will display like this.
1. Hello               <--h2
    1.1.1 You        <--h5

This is confusing for my users so I wanted this.
Hello
    You

Notice that it is still a list, but there is no number structure.

I looked at the markup via Firebug (in Firefox) and found that this should be easy.  The numbers I wanted to remove where in a span.  (i.e. <span>1.1</span> and the span was in a <li> inside of div with a class of pageToc.

Figuring this would be easy, I just added this to CSS.

.pageToc li {
    display: none;
    }

After adding this to the CSS it worked GREAT in Firefox, but not so well in IE 7/8.  The numbers were gone, but there was still a big space where the span used to be.  At first I thought that the span just some how wasn't going away, so I tried all sorts of CSS with NO luck.

mindtouch-toc-firefox
Above is the TOC without the numbers viewed in Firefox

MindTouch TOC in IE 8 and IE 7
Above is the TOC without the numbers viewed in IE7/8

After a lot of frustration I actually looked at the source and noticed that the markup had a space before the anchor links (i.e. Hello and You) I am not sure why MindTouch did this and it might just be a bug or something they overlooked.

MindTouch TOC markup via viewing source

I had to look at the source because I could not tell viewing the markup via Firebug (in Firefox) that the spaces were there.

MindTouch TOC markup as viewed in Firebug of Firefox

However when I viewed the webpage with Internet Explorer’s Developer Toolbar and I could see them a little better.  As you can see below you can see the Text – Empty Text Node.  The problem with Developer Toolbar is that it doesn’t display closing tags, so I didn’t catch this right away.

mindtouch-toc-markup-ie

After all that troubleshooting I was happy to know at least what the issue was.  Now I just need to add some jQuery to remove those blank spaces.  Below is the jQuery I have come up on page load.

$(document).ready(function(){
            // Used to replace the spaces in the TOC menu before the anchor links
            $(".pageToc li").each (  function ( i , val  )
            {
                $(val).html($(val).html().replace(/\s<a/ig, "<a"));
            });
        });

I had to use Regular Expression to replace only in front of the anchor.  I created another post to go a little further into JavaScript's Regex Syntax.

After I added this jQuerry script all was good and now I had the following TOC display. 

mindtouch-toc-firefox

I know this isn’t a common thing to do, but it works well for my customer’s intranet site, and the way they use the headers in content more as display elements than hierarchy elements.

I hope this helps anyone in the same boat.

Wednesday, September 09, 2009 3:07:26 PM (Pacific Standard Time, UTC-08:00)

#       Comments [1] - Trackback

Deki Wiki | jQuery | MindTouch

# Friday, September 04, 2009

In JavaScript, a regular expression is written in the form of /pattern/modifiers where "pattern" is the regular expression itself, and "modifiers" are a series of characters indicating various options. The "modifiers" part is optional.

    * /g enables "global" matching. When using the replace() method, specify this modifier to replace all matches, rather than only the first one.
    * /i makes the regex match case insensitive.
    * /m enables "multi-line mode". In this mode, the caret and dollar match before and after newlines in the subject string.

You can combine multiple modifiers by stringing them together as in /regex/gim.

Since forward slashes delimit the regular expression, any forward slashes that appear in the regex need to be escaped. For example, the regex 1/2 is written as /1\/2/ in JavaScript.

As with anything, you can find much more information about JavaScript on the web if you Google or Bing it. 

Friday, September 04, 2009 2:30:01 PM (Pacific Standard Time, UTC-08:00)

#       Comments [0] - Trackback

Code | JavaScript | jQuery

Archive

<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Email Subscription

Sign up to get the CornerPixel Blog delivered to your email.

About the Author

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Sign In

Statistics

Total Posts: 52
This Year: 3
This Month: 0
This Week: 0
Comments: 28