@jcottam/html-metadata
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

HTML Metadata

npm license developed by

@jcottam/html-metadata is a JavaScript library that simplifies the extraction of HTML meta and Open Graph tags from HTML content or URLs. These tags provide structured metadata for webpages, useful for social media sharing and SEO purposes.

The library is compatible with both server and browser environments, supporting ES6 and CommonJS module systems. ES6 modules are suitable for modern browsers, while CommonJS modules are commonly used in server-side JavaScript environments like Node.js.

Contributions to this open-source project are welcome.

HTML metadata

Features

NodeJS JavaScript TypeScript

  • Lightning-fast performance for optimal efficiency.
  • Versatile compatibility with both Node.js and web browsers.
  • Effortlessly extract HTML meta and Open Graph tags from HTML code or URLs.
  • Seamlessly parse and access metadata such as og:title, og:description, og:image, favicon, and more.
  • Developed using TypeScript to ensure robust type safety.
  • Simple integration into web applications for hassle-free implementation.
  • Comprehensive support for error handling and customization options.

Please report bugs by filing an issue. Contribute to the project by following the guidelines outlined in the contributing section.

Installation

npm install @jcottam/html-metadata

Usage

ES6 and CommonJS syntax supported. Examples below are in CommonJS syntax.

Extract tags from a URL

const { extractFromUrl } = require("@jcottam/html-metadata")

extractFromUrl("https://www.retool.com").then((data) => console.log(data))

Extract tags from an HTML string

Useful in serverless environments like Cloudflare Workers where server-side fetches and Response objects are at play.

const { extractFromHTML } = require("@jcottam/html-metadata")

const data = extractFromHTML(
  "<html><head><meta property='og:title' content='Hello World' /></head></html>"
)

Documentation

Methods

extractFromHTML: (html: string, options?: Options) => ExtractedData
extractFromUrl: (url: string, options?: Options) =>
  Promise<ExtractedData | null>

Types

type Options = {
  timeout?: number // fetch timeout in milliseconds
  metaTags?: string[] // list of meta tags to extract
}

type ExtractedData = {
  [key: string]: string
}

Example Response

{
  "og:type": "website",
  "og:url": "https://retool.com/",
  "og:title": "Retool | The fastest way to build internal software.",
  "og:description": "Retool is the fastest way to build internal software. Use Retool's building blocks to build apps and workflow automations that connect to your databases and APIs, instantly.",
  "og:image": "https://d3399nw8s4ngfo.cloudfront.net/og-image-default.webp",
  "favicon": "/favicon.png",
  "twitter:card": "summary_large_image",
  "twitter:creator": "@retool",
  "twitter:title": "Retool | The fastest way to build internal software.",
  "twitter:description": "Retool is the fastest way to build internal software. Use Retool's building blocks to build apps and workflow automations that connect to your databases and APIs, instantly.",
  "twitter:image": "https://d3399nw8s4ngfo.cloudfront.net/og-image-default.webp"
}

CORS issues (when using the library in the browser)

To circumvent CORS (Cross-Origin Resource Sharing) issues, one approach is to execute the extractFromUrl function from a server-side environment. Alternatively, if you are working in a browser-based setting, you can proxy the request through a free service such as allorigins.win.

For instance, you can use the following URL format to access the desired resource: https://api.allorigins.win/get?url=https://www.retool.com. This will allow you to retrieve the necessary data without running into CORS restrictions. By leveraging this method, you can effectively work around CORS limitations and access the desired information seamlessly.

Third Party Tools

The module utilizes the following third-party tools for testing and functionality:

  • Vitest: Next-generation testing framework.
  • Cheerio: A fast, flexible, and lean implementation of core jQuery designed for server-side Node.js.
  • Markdwown-Badges: Badges for your personal developer branding, profile, and projects.

Contributing

We welcome contributions to the @jcottam/html-metadata module! If you'd like to contribute, please follow these guidelines:

  1. Fork the repository and create a branch.
  2. Make your changes and ensure that the code style and tests pass.
  3. Submit a pull request with a detailed description of your changes.

Forking the Repository

Click on the "Fork" button in the top right corner of the repository page. This will create a copy of the repository under your GitHub account.

Cloning the Forked Repository

git clone https://github.com/your-username/@jcottam/html-metadata.git

Making Changes

Create a new branch to work on your changes:

git checkout -b feature-branch

Make your desired changes to the codebase, documentation, or any other relevant files.

Build

npm install
npm run build

Test

npm run test

Committing Changes

  1. Stage the changes you have made:
git add .
  1. Commit the staged changes with a descriptive message:
git commit -m "Add feature XYZ"

Pushing Changes

git push origin feature-branch

Issuing a Pull Request

  1. Visit your forked repository on GitHub.
  2. Click on the "New pull request" button next to the branch selection drop-down.
  3. Select the base branch (usually the main branch of the original repository) and the compare branch (your feature branch).
  4. Provide a descriptive title and detailed description of your changes in the pull request.
  5. Click on the "Create pull request" button to submit your pull request for review.

Review and Collaboration

  1. The maintainers of the original repository will review your pull request.
  2. Any feedback or changes required will be communicated through comments on the pull request.
  3. Once the changes are approved, your pull request will be merged into the main repository.

Package Sidebar

Install

npm i @jcottam/html-metadata

Weekly Downloads

14

Version

2.0.2

License

MIT

Unpacked Size

16.7 kB

Total Files

12

Last publish

Collaborators

  • jcottam