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

og-easy

v1.0.15

Published

The og-easy package provides a simple and easy-to-use API for getting Open Graph data. It is a Node.js package for retrieving Open Graph metadata from a given URL, if no data is found, It will parse the html to find a proper intuitive replacement. The pac

Downloads

21

Readme

//====================================================================
//                   ██████╗  ██████╗
//                  ██╔═══██╗██╔════╝
//                  ██║   ██║██║  ███╗█████╗
//                  ██║   ██║██║   ██║╚════╝
//                  ╚██████╔╝╚██████╔╝
//                   ╚═════╝  ╚═════╝
//              ███████╗ █████╗ ███████╗██╗   ██╗
//              ██╔════╝██╔══██╗██╔════╝╚██╗ ██╔╝
//              █████╗  ███████║███████╗ ╚████╔╝
//              ██╔══╝  ██╔══██║╚════██║  ╚██╔╝
//              ███████╗██║  ██║███████║   ██║
//              ╚══════╝╚═╝  ╚═╝╚══════╝   ╚═╝
//                                                          ◎     ◎
//          ╔╗ ┬ ┬  ╔╗╔┌─┐┬─┐┌─┐┌─┐  ╔═╗┌┬┐┌─┐┬┌─┌─┐         \   /
//          ╠╩╗└┬┘  ║║║│ │├┬┘├─┤├─┤  ╚═╗ │ │ │├┴┐├┤        o(👁 👁)o
//          ╚═╝ ┴   ╝╚╝└─┘┴└─┴ ┴┴ ┴  ╚═╝ ┴ └─┘┴ ┴└─┘       🦾  🫦  🤳
//                     MIT LICENSE                            /  \
//                                                           🛼   🛼
//     AUTHOR GITHUB: http://github.com/noraa-july-stoke
//     PROJECT REPO: https://github.com/noraa-july-stoke/og-easy
//====================================================================
//    An easy to use api for getting site metadata to display link
//    previews. It looks for Opengraph tags first, and also
//    provides an "alt" object for convenience/choice, and/or
//    the case of the site not being compliant with OpenGraph
//    protocol.
//====================================================================

og-easy

The og-easy package provides a simple, lightweight and easy-to-use API for getting Open Graph data from a URL. It includes an "alt" object that provides alternatives in case something is wrong with the site's meta/og tags.

Installation

npm install og-easy

Usage

Here is an example route you can implement on the backend

// To use `og-easy`, you can simply require it in your code:
const express = require("express");
const getSiteMetaData = require("og-easy");

const router = express.Router();

// and then implement it in your route as follows:
//==================================================================================
// Fetches URL previews from opengraph API
//==================================================================================
router.get("/opengraph-preview", async (req, res) => {
  const url = req.query?.url;
  if (url) {
    try {
      const metadata = await getSiteMetaData(url);
      res.status(200).json(metadata);
    } catch (error) {
      console.error(error);
      res.status(500).send("Error retrieving metadata");
    }
  }
  else res.status(404).send("No url found");
});

module.exports = router;

This will return an object containing the Open Graph metadata for the website, along with some alternative metadata if no Open Graph metadata is found.

The object looks like this and should always have backup alt properties:

{
  og: {
    title: 'Example Title',
    description: 'Example Description',
    image: 'https://example.com/example.jpg',
    url: 'https://example.com'
  },
  alt: {
    title: 'Example Title',
    description: 'Example Description',
    image: 'https://example.com/example.jpg',
    url: 'https://example.com'
  }
}

You can then access the metadata fields like this:

console.log(metadata.og.title);
console.log(metadata.alt.title);

router.get("/opengraph-preview", async (req, res) => {

}

React Component

Here is an example react component that utilizes this method:


import React, { useState, useEffect } from "react";
import axios from "axios";

  const fetchOpenGraphData = async (url) => {
  try {
    const response = await axios.get("/api/microservices/opengraph-preview", { params: { url } });
    return response.data;
  } catch (error) {
    console.error(error);
  }
};


const PostLink = ({ url }) => {
  const [linkData, setLinkData] = useState({});


  useEffect(() => {
    const fetchData = async () => {
      const data = await fetchOpenGraphData(url);
      setLinkData(data);
    };
    fetchData();
  }, [url]);

  const {og, alt} = linkData;
  const title = og?.title || alt?.title;
  const image = og?.image || alt?.image;
  const description = og?.description || alt?.description;
  const siteUrl = og?.url || alt?.url;

  return (
    <div className="post-link-container">
      <a href={siteUrl} target="_blank" rel="noopener noreferrer">
        {image && <img src={image} alt={title} />}
        <div className="post-link__info">
          <h2 style={{color: "black"}}>{title}</h2>
          {description && <p>{description}</p>}
        </div>
      </a>
    </div>
  );
};
export default PostLink;