Using code

Microformat.get()

simple


<script src="microformat-shiv.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var items;

    items = Microformats.get()
    // do something with data `items`
</script>

using options


<script src="microformat-shiv.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var items,
        options;

    options = {'filters': ['h-card']};
    var items = Microformats.get( options )
    // do something with data `items`
</script>

targeting just part of a page


<script src="microformat-shiv.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var items,
        options;

    options = {
        'filters': ['h-card'],
        'node': document.getElementById('target')
    };
    var items = Microformats.get( options )
    // do something with data `items`
</script>

parsing a HTML string


<script src="microformat-shiv.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var items,
        options;

    options = {
        'baseUrl': 'http://glennjones.net',
        'html': '<a class="h-card" href="/about.html">Glenn</a>'
    };
    var items = Microformats.get( options )
    // do something with data `items`
</script>
Note: The `baseUrl` is optional and is used to resolve relative URLs

Available options

  • html - (String) the html to be parse (only get and count methods)
  • node - (DOM element)
    the element to be parse - default current browser document
  • baseUrl - (String) optional URL used to resolve relative URLs
  • filters - (Array)
    microformats types returned - i.e. ['h-card'] - always adds rels.
  • textFormat - (String)
    text style whitespacetrimmed or normalised default is whitespacetrimmed
  • dateFormat - (String)
    the ISO date profile auto, w3c rfc3339 or html5 default is auto
  • add - (Array)
    adds microformat version 1 definitions

I would recommended always setting textFormat option to normalised. This is not part of the microformat parsing rules, but in most cases provides more usable output.

Options example.

{
    "filters": ["h-card"],
    "textFormat": "normalised",
    "dateFormat": "w3c"
}

Microformat.getParent()

The getParent method returns the first parent microformat for the passed node. The method can take a options object as a second parameter.


<script src="microformat-shiv.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var items,
        node = document.getElementById('target');

    items = Microformats.getParent( node )
    // do something with data `items`
</script>

Microformat.count()

The count method returns the number of each microformat type found. It does not do a full parse so it is much quicker than get and can be used for tasks such as adding notifications to the UI. The method can take a options object as a parameter.


<script src="microformat-shiv.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var counts = Microformats.count()
    // do something with data `counts`
</script>
Output

{
    'h-event': 1,
    'h-card': 2,
    'rels': 6
}

Microformat.isMicroformat()

The isMicroformat method returns weather a node has a valid microformats class. It currently does not work consider rel=* a microformats. The method can take a options object as a second parameter.


<script src="microformat-shiv.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var isVaild,
        node = document.getElementById('target');

        isVaild = Microformats.isMicroformat( node );
        // do something with isVaild boolean
</script>

Microformat.hasMicroformats()

The hasMicroformats method returns weather a document or node has any valid microformats class. It currently does not take rel=* microformats into account. The method can take a options object as a second parameter.


<script src="microformat-shiv.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var isVaild,
        node = document.getElementById('target');

        isVaild = Microformats.hasMicroformat( node );
        // do something with isVaild boolean
</script>

Supported formats

h-adr, h-card, h-entry, h-event, h-geo, h-news, h-product, h-recipe, h-resume, h-review-aggregate, h-review, adr, hCard, hEntry, hEvent, geo, hNews hProduct, hRecipe, hResume, hReview-aggregate, hReview, rel=tag, rel=licence, rel=no-follow, rel=author and XFN

Response

In 2013 microformat-shiv was completely rewriten to conform to the new version 2 specification of microformats JSON output. Typical data structure. Below is an example of a h-card microformat.


{
    "items": [{
        "type": ["h-adr"],
        "properties": {
            "extended-address": ["Suite 300"],
            "street-address": ["650 Castro Street"],
            "locality": ["Mountain View"],
            "region": ["CA"],
            "postal-code": ["94041"],
            "country-name": ["United States"],
            "name": ["Suite 300, 650 Castro Street, Mountain View, CA 94041, United States"]
        }
    }],
    "rels": {},
    "rel-urls": {}
}
Typical error structure.

{
    "items": [],
    "rels": {},
    "rel-urls": {}
    "errors": ['No options.node was provided and no document object could be found.']
}

Mocha tests

The library includes web pages which run the microfomats test suite.The tests are built using Mocha.js.

Support or Contact

Having trouble, please raise an issue at: https://github.com/glennjones/microformat-shiv/issues