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

@flyyer/opengraph-properties

v2.0.0

Published

Convert meta-tags into a object with accessor. Supports single and multiple tags.

Downloads

6

Readme

@flyyer/opengraph-properties

Convert meta-tags into a object with accessor. Supports single and multiple tags via pluralization.

yarn add @flyyer/opengraph-properties

Usage

import { parseMetaTags, MetaTag, extractString, extractNumber } from "@flyyer/opengraph-properties";
import * as cheerio from "cheerio";

const raw = `
  <meta property="og:image" content="https://example.com/rock.jpg" />
  <meta property="og:image:width" content="300" />
  <meta property="og:image:height" content="300" />
  <meta property="og:image" content="https://example.com/rock2.jpg" />
  <meta property="og:image" content="https://example.com/rock3.jpg" />
  <meta property="og:image:height" content="1000" />
`;

// means there are 3 images on this page, the first image is 300x300, the middle one has unspecified dimensions, and the last one is 1000px tall.

const $ = cheerio.load(raw);
const metatags: MetaTag[] = Array.from($("meta"), (meta) => meta.attribs);
const parsed = parseMetaTags(metatags, {
  // Convert `name` or `property` key to lowercase and trim whitespace.
  normalizeKey: true,
  // Trim white space from `content`.
  normalizeValue: true,
});

// First appearance take precedence.
extractString(parsed.og["image"]) === "https://example.com/rock.jpg";
extractNumber(parsed.og["image"]["width"]) === 300;
extractNumber(parsed.og["image"]["height"]) === 300;

// A pluralized key is always created to handle multiple appearances.
extractString(parsed.og["images"][0]) === "https://example.com/rock.jpg";
extractNumber(parsed.og["images"][0]["width"]) === 300;
extractNumber(parsed.og["images"][0]["height"]) === 300;
extractString(parsed.og["images"][1]) === "https://example.com/rock2.jpg";
extractString(parsed.og["images"][2]) === "https://example.com/rock3.jpg";
extractNumber(parsed.og["images"][2]["height"]) === 1000;

Undeclared properties are undefined.

extractNumber(parsed.og["images"][2]["width"]) === undefined

Also a property accessor can be used as second argument.

extractNumber(parsed.og, "images.2.height") === 1000;
extractNumber(parsed.og, ["images", 2, "height"]) === 1000;

LiquidJS

Can be used with liquidjs.

import { parseMetaTags, MetaTag, extractString, extractNumber } from "@flyyer/opengraph-properties";
import { Liquid } from "liquidjs";

const metatags: MetaTag[] = [
  {
    property: "og:audio",
    content: "https://example.com/bond/theme.mp3",
  },
  {
    property: "og:locale",
    content: "en_GB",
  },
  {
    property: "og:locale:alternate",
    content: "fr_FR",
  },
  {
    property: "og:locale:alternate",
    content: "es_ES",
  },
];

const parsed = parseMetaTags(metatags);
const engine = new Liquid({ globals: parsed });

const print = engine.parseAndRenderSync(`
  Listening to {{ og.audio }} in {{ og.locale }}

  Available locales:
  {% for locale in og.locale.alternates %}
    Language: {{ locale }}
  {% endfor %}
`);
console.log(print);

Will print:

Listening to https://example.com/bond/theme.mp3 in en_GB

Available locales:
Language: fr_FR
Language: es_ES