• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 12 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Microformats parser for node.js

build status Coverage Status Codacy Badge MIT license

microformat-node

A node.js microformats parser. It is the same codebase as microformat-shiv project, but used the ultra-fast HTML DOM cheerio to parse HTML. The microformat-shiv library is also used in Firefox browser as an internal component.

Installation

Using bower:

$ npm install microformat-node

Methods

get

The get method parses microformats data from either a html string or a cheerio object.

Simple parse of HTML string.

    var Microformats = require('microformat-node'),
        options = {};

    options.html = '<a class="h-card" href="http://glennjones.net">Glenn</a>';
    Microformats.get(options, function(err, data){
        // do something with data
    });

Simple parse of a Cheerio parsed page

    var Microformats = require('microformat-node'),
        Cheerio = require('cheerio'),
        options = {};

    options.node = Cheerio.load('<a class="h-card" href="http://glennjones.net">Glenn</a>');
    Microformats.get(options, function(err, data){
        // do something with data
    });

Options

  • html - (String) the html to be parse
  • node - (Cheerio DOM object) the element to be parse
  • filter - (Array) microformats types returned - i.e. ['h-card'] - always adds rels
  • baseUrl - (String) a base URL to resolve any relative URL:s to
  • textFormat - (String) text style whitespacetrimmed or normalised default is whitespacetrimmed
  • dateFormat - (String) the ISO date profile auto, microformat2, 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.

Experimental Options

These options are part of ongoing specification development. They maybe removed or renamed in future.

  • lang (Boolean) Parses and adds the language value to e-* default is false
  • parseLatLonGeo (Boolean) Parse geo date writen as latlon i.e. 30.267991;-97.739568 default is false

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": {}
    }

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.

    var Microformats = require('microformat-node'),
        options = {};

    options.html = '<a class="h-card" href="http://glennjones.net">Glenn</a>';
    Microformats.count(options, function(err, data){
        // do something with data
    });

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.

    var Microformats = require('microformat-node'),
        options = {};

    options.html = '<a class="h-card" href="http://glennjones.net">Glenn</a>';
    Microformats.isMicroformat(options, function(err, isValid){
        // do something with isValid
    });

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.

    var Microformats = require('microformat-node'),
        options = {};

    options.html = '<div><a class="h-card" href="http://glennjones.net">Glenn</a></div>';
    Microformats.hasMicroformats(options, function(err, isValid){
        // do something with isValid
    });

using a promise

There are promise based version of the four public methods, each is appended with the text Async. So the names for promise methods are getAsync, countAsync, isMicroformatAsync and hasMicroformatsAsync.

    var Microformats = require('microformat-node'),
        options = {};

    options.html = '<a class="h-card" href="http://glennjones.net">Glenn</a>';
    Microformats.getAsync(options)
        .then(function (data) {
            // do something with data
        })
        .catch(function(err){
            // do something with err
        })

Version and livingStandard

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

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. Examples of existing definitions found in the directory lib/maps. You not need to add new definitions object if your using the microformats version 2.

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

Running simple demo page

$ git clone https://github.com/glennjones/microformat-node.git
$ cd microformat-node
$ npm i
$ npm start

Then open http://0.0.0.0:3000

License

MIT Β© Copyright Glenn Jones

More Repositories

1

microformat-shiv

A light weight cross browser JavaScript microformats parser
JavaScript
136
star
2

be-more-hapi

A example site using HAPI - Built for Asyncjs talk
JavaScript
85
star
3

webmentions

An API of helper function for providing and consuming webmentions
JavaScript
43
star
4

Draggables

A javascript library that allows users to exchange data between web sites using drag and drop
32
star
5

hapi-token-docs

A example site using HAPI, JWT tokens and swagger documentation
JavaScript
26
star
6

time-to-be-hapi

Time to be HAPI - london.js talk
HTML
23
star
7

elsewhere-profiles

Social media profile aggregator for node.js
JavaScript
23
star
8

UfXtract

UfXtract is a .Net microformats parser
C#
21
star
9

elsewhere-mapper

Social media profile endpoint mapper for node.js
JavaScript
17
star
10

mani

Pure javascript search - browser and node.js
JavaScript
12
star
11

note-editor

A note editor for indie blogging platforms
CSS
11
star
12

kinect-drone

Control an AR-Drone by waving your hands. Links a Kinect through node.js to a drone
JavaScript
9
star
13

text-autolinker

An tool for adding HTML links and tags to plain text
JavaScript
8
star
14

hapi-workshop

Async Special - Hapi.js Workshop
JavaScript
6
star
15

extending-the-offline-web

Extending the offline web experience - asyncjs talk
JavaScript
5
star
16

sw-toolbox-example

Basic sw-toolbox example with HAPI backend, to be used as template
JavaScript
4
star
17

nunjucks-extras

A set of tag extensions and filters for nunjucks templating lib
JavaScript
2
star
18

node-introduction

A small introduction talk for node.js
JavaScript
2
star
19

url-tools

A very simple node.js module for comparing URLs
JavaScript
2
star
20

edinburghjs-text-wrangling

2
star
21

waypointer

A HAPI plug-in that provides a template framework for OpenAPI (aka swagger)
2
star
22

swagger-har

A module that converts OpenAPI (aka Swagger) schema objects into an array HAR request objects.
JavaScript
2
star
23

microformats-testrunner

A microformat 2 test runner
HTML
2
star
24

hapi-waypointer

A HAPI plug-in that provides modern auto documentation of APIs using Waypointer themes
JavaScript
2
star
25

hapi-bootstrap

A basic hapi.js bootstrap to build projects from
JavaScript
1
star
26

prompt

JavaScript
1
star
27

hapi-microformats

A HAPI wrap of microformat-node project
JavaScript
1
star
28

node-express-template

This is an example project built with node and express
JavaScript
1
star
29

html-to-markdown

Turns HTML into Markdown
JavaScript
1
star