npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fb-text2html

v2.1.2

Published

simple utility that transforms Facebook graph data: (text, tags) => html

Downloads

5

Readme

fb-text2html

Transform Facebook Graph data: (text, tags) => html.

When querying the FB graph using the Javascript SDK, it will return content as plain text (as the message property in a Post object) - even if the original post included all sorts of links. URLs in the text need to be transformed into HTML links and newlines characters into HTML breaks. Links to other pages can be obtained from the message_tags property (see the example of such an object below).

Emoji's

Some special care needs to be taken to split text by Unicode code points rather than simple characters. I liked the following introduction into the subject: http://blog.jonnew.com/posts/poo-dot-length-equals-two. Good thing is that Array.from provides an easy way to deal with this.

Installing

Use the package manager of your choosing:

# npm
npm install fb-text2html

# yarn
yarn add fb-text2html

Or load directly into your browser:

<script src="./dist/fbtext2html.min.js"></script>
<script>
  window.fbtext2html(message, message_tags, options);
</script>

Usage

import { text2html } from 'fb-text2html';

// multiline plain text in;
const text = `hey! check out this cool FB page 😋,

and also don't miss out on this link: goo.gl/Q377nS
bye! 😹`;

// tags come in the following format:
const tags = [
  {
    'id': '189217720153',
    'name': 'cool FB page',
    'type': 'page',
    'offset': 20,
    'length': 12
  }
];

// combine here...
const html = text2html(text, tags);

After running, the html const will contain the following value:

<!-- formatted for readability, actually a string with no extra whitespace -->
<p>
  hey! check out this <a href="https://www.facebook.com/189217720153">cool FB page</a> 😋,
</p>
<p>
  and also don't miss out on this link: <a href="http://goo.gl/Q377nS">goo.gl/Q377nS</a>
  <br/>
  bye! 😹
</p>

Using CommonJS require

Note above example uses ES Modules. When using CommonJS modules (ie in Node), you include the library as follows, with all else staying the same:

const fbText2html = require("fb-text2html").text2html;

Options

You can set some options. Here you see the defaults:

const options = {
  // should anchor links be added for tags?
  addTags: true,

  // should autolinker add links for:
  // - urls
  // - email addresses
  // - hashtags (link to facebook's hashtag search results)
  autolinker: true,

  // should newline characters be replaced by paragraph + break markup?
  // single newline are replaced with <br/> tag
  // double newlines are processed as delineators of paragraphs
  addParagraphs: true
};

const html = fbText2html(text, tags, options);

Acknowledgements

This lib uses AutoLinker. Good lib, made my life easy :+1:.

Uses Array.from to split the Facebook post body properly in separate unicode code points. If the function is not available, a fallback is used. The fallback was inspired by this StackOverflow page.

License

MIT, see LICENSE.

Author

Erik Hagreis