Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A light weight cross browser JavaScript microformats parser
JavaScript HTML Other
Branch: master

README.md

build status Coverage Status Codacy Badge MIT license

microformat-shiv

A cross browser javascript microformats parser, which can also be used to build browser extensions.

Installation

Using bower:

$ bower install microformat-shiv

Methods

get

Simple parse of HTML document or a selected section.

    <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>    

Options

  • node - (DOM element) the element to be parse - default current browser document
  • filter - (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.

Output

JSON output. This is an example of a parsed h-card microformat.

    {
        "items": [{
            "type": ["h-card"],
             "properties": {
                "url": ["http://blog.lizardwrangler.com/"],
                "name": ["Mitchell Baker"],
                "org": ["Mozilla Foundation"],
                "note": ["Mitchell is responsible for setting the direction Mozilla ..."],
                "category": ["Strategy", "Leadership"]
             }
        }],
        "rels": {},
        "rel-urls": {}
    }

JSON output with error.

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

getParent

Given an HTML DOM node it will return its first parent microformat.

    <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>

The getParent method takes the same options as the get method. The one difference is how the options.filters property affects the output. Adding a filter list to getParent will allow the search for a parent to pass throught microformats you do not want to target.

    <script src="microformat-shiv.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var items,
            options = {'filters': ['h-entry']},
            node = document.getElementById('target');

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

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 counts data 
    </script>    

Output

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

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 node = document.getElementById('target');
        var isVaild = Microformats.isMicroformat( node );
        // do something with isVaild boolean 
    </script>    

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.isMicroformat( node );
        // do something with isVaild boolean 
    </script>    

Version and livingStandard

The library has two properties to help identify now up todate it is:

Browser Support

TODO update this list with support info.

  • Firefox 40 > 20
  • Chrome
  • Opera
  • Safari
  • IE11
  • IE10
  • IE9 (needs ES6-shim.js)
  • IE8 (needs ES6-shim.js)

Device Browser Support

  • iOS
  • Andriod

Add notes on support:

Microformats definitions object

The library has built-in version 1 microformats definitions, but you can add new definitions using options.add if you wish. Below is an example of a definitions object. More can be found in the directory lib/maps. definitions. You not need definitions object if your using the microformats version 2 schema.

    {
        root: 'hpayment',
        name: 'h-payment',
        properties: {
            'amount': {},
            'currency': {}
        }
    }

License

MIT © Glenn Jones

Something went wrong with that request. Please try again.