mf-obj
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

mf-obj

npm Build Status Coverage Status

Microformat objects are a set of utility classes for working with indieweb posts.

  • Read different kinds of posts:
    • notes
    • articles
    • replies
    • likes
    • reposts
  • Parse comments and reply contexts as nested objects
  • Resolve author with the authorship algorithm
  • Get a list of webmention targets
  • Serialize and deserialize from JSON

Installation

Microformat objects makes use of ES6 features and requires Node >= 4.0.0.

npm install mf-obj --save

Examples

Get entry from url

mfo.getEntry('http://somesite/2016/5/1/1')
.then(entry => {
    if (entry.isReply() {
        console.log('I\'m a reply to "' + entry.replyTo.name + '"');
    }
});

API

  1. Utility functions
  1. Entry
  1. Card
  1. Event
  1. Feed

Utility functions

getEntry()

mfo.getEntry(url)
.then(entry => {
  //...
});
mfo.getEntry(url, ['entry','event','oembed'])
.then(entry => {
    //...
});

Fetches the page at url and returns a Promise for an Entry. This will perform the authorship algorithm and fetch the author h-card from a separate url if necessary.

The second parameter strategies is an optional array of strategies to attempt to marshal to an Entry. Strategies are tried in order and if all fail, an exception is thrown. This can be used for displaying comments or reply contexts of URLs that don't contain h-entries. The default value for this parameter is ['entry'].

  • entry - Default h-entry strategy.
  • event - Marshall an h-event to an Entry. Useful for creating RSVP reply-contexts to an h-event.
  • oembed - Marshall oembed data to an Entry. Useful for creating reply-contexts or reposts of silo content.
  • opengraph - Marshall opengraph data to an Entry. Useful for creating reply-contexts or reposts of silo content.
  • html - Most basic strategy. Marshalls html <title> to name and <body> to content.

getCard()

mfo.getCard(url)
.then(card => {
  //...
});

Fetches the page at url and returns a Promise for a Card. This will return null if an h-card could not be found according to the authorship algorithm.

getEvent()

mfo.getEvent(url)
.then(event => {
  //...
});

Fetches the page at url and returns a Promise for an Event.

getFeed()

mfo.getFeed(url)
.then(feed => {
  //...
});

Fetches the page at url and returns a Promise for a Feed.

Entry

Represents an h-entry or h-cite. Properties of this object correspond to output from the mf2 parser, but have been converted from arrays of string to other data types for convenience.

var entry = new mfo.Entry();
var entry2 = new mfo.Entry('http://somesite/2016/5/2/1');

The constructor takes an optional argument to set the url.

name

string || null

published

Date || null

content

{html: string, value: string} || null

summary

string || null

url

string || null

author

Card || null

See Card.

category

string[]

syndication

string[]

syndicateTo

Parsed from syndicate-to.

string[]

photo

string[]

audio

string[]

video

string[]

replyTo

Parsed from in-reply-to.

Entry[] || null

likeOf

Parsed from like-of.

Entry[] || null

repostOf

Parsed from repost-of.

Entry[] || null

embed

{html: string, value: string} || null

Experimental property for storing oembed content. Parsed from e-x-embed.

getDomain()

Returns the domain component of the url.

getPath()

Returns the path component of the url.

getReferences()

Returns an array of urls from the reply-to, like-of, or repost-of properties.

getMentions()

Returns an array of urls found in links in the e-content, in addition to getReferences(). Intended for sending webmentions.

getChildren()

Returns an array of Entries. Use this instead of directly accessing the children property. Takes an optional argument to sort the results.

var unsorted = entry.getChildren();
var sorted = entry.getChildren(mfo.Entry.byDate);

addChild()

Adds an Entry to the list of children. If there is an existing child with the same url, it will be overwritten.

function receiveWebmention(sourceUrl, targetUrl) {
  // ...
  var sourceEntry = mfo.getEntryFromUrl(sourceUrl);
  targetEntry.addChild(sourceEntry);
  // ...
}

deleteChild()

Remove an entry from the list of children by url.

function receiveWebmention(sourceUrl, targetUrl) {
  // ...
  if (got404) {
    targetEntry.deleteChild(sourceUrl);
  }
  // ...
}

isReply()

Tests if reply-to is non-empty.

isLike()

Tests if like-of is non-empty.

isRepost()

Tests if repost-of is non-empty.

isArticle()

Tests if name and content.value properties exist and differ, in addition to other heuristics.

serialize()

Serialize object to JSON. Nested Entry objects in replyTo, likeOf, repostOf, and children are serialized as an url string.

Example output:

{
  "name":"Hello World!",
  "published":"2015-08-28T08:00:00.000Z",
  "content":{
    "value":"Hello World!",
    "html":"Hello <b>World!</b>"
  },
  "summary":"Summary",
  "url":"http://testsite/2015/8/28/2",
  "author":{
    "name":"Test User",
    "photo":null,
    "url":"http://testsite",
    "uid":null
    },
  "category":["indieweb"],
  "syndication":[],
  "syndicateTo":[],
  "photo":[],
  "audio":[],
  "video":[],
  "replyTo":["http://testsite/2015/8/28/2"],
  "likeOf":[],
  "repostOf":[],
  "embed":null,
  "children":["http://testsite/2015/8/28/3"]
}

deserialize

Static method to deserialize json. Nested objects from replyTo, likeOf, repostOf, and children are deserialized as stub Entry objects with only url set.

var entry = mfo.Entry.deserialize(json);

Card

Represents an h-card. Properties of this object correspond to output from the mf2 parser, but have been converted from arrays of string to string for convenience.

var author = new mfo.Card();
var author = new mfo.Card('http://somesite');

The constructor takes an optional argument to set the url.

name

string || null

photo

string || null

url

string || null

uid

string || null

Event

Represents an h-event. Properties of this object correspond to output from the mf2 parser, but have been converted from arrays of string to other datatypes for convenience.

var event = new mfo.Event();
var event = new mfo.Event('http://somesite/event');

The constructor takes an optional argument to set the url.

name

string || null

url

string || null

start

Date || null

stop

Date || null

Location

Card || null

Feed

Represents an h-feed. Properties of this object correspond to output from the mf2 parser, but have been converted from arrays of string to other datatypes for convenience.

var event = new mfo.Feed();
var event = new mfo.Feed('http://somesite');

The constructor takes an optional argument to set the url.

name

string || null

url

string || null

author

Card || null

See Card.

prev

Parsed from rel="prev" or rel="previous".

string || null

next

Parsed from rel="next".

string || null

getChildren()

Returns an array of Entries. Use this instead of directly accessing the children property. Takes an optional argument to sort the results.

addChild()

Adds an Entry to the list of children. If there is an existing child with the same url, it will be overwritten.

deleteChild()

Remove an entry from the list of children by url.

Readme

Keywords

Package Sidebar

Install

npm i mf-obj

Weekly Downloads

1

Version

2.1.0

License

MIT

Last publish

Collaborators

  • notenoughneon