Socket
Book a DemoInstallSign in
Socket

head-metadata

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

head-metadata

Typesafe and straightforward utility for extracting structured metadata (like `<meta>`, `<title>`, and `<link>`) from the `<head>` of an HTML document

latest
Source
npmnpm
Version
0.0.10
Version published
Maintainers
1
Created
Source

head-metadata banner

GitHub License NPM bundle minzipped size NPM total downloads Join Discord

Status: Experimental

head-metadata is a typesafe and straightforward utility for extracting structured metadata (like <meta>, <title>, and <link>) from the <head> of an HTML document.

📖 Usage

Extract Metadata from <head>

import { extractHeadMetadata, linkExtractor, metaExtractor, titleExtractor } from 'head-metadata';

const html = `
  <html>
    <head>
      <title>Example</title>
      <meta name="description" content="An example page" />
      <link rel="canonical" href="https://example.com" />
    </head>
  </html>
`;

const metadata = extractHeadMetadata(html, {
	meta: metaExtractor,
	title: titleExtractor,
	link: linkExtractor
});

console.log(metadata);
/*
{
  title: 'Example',
  meta: {
    description: 'An example page'
  },
  link: {
    canonical: 'https://example.com'
  }
}
*/

Create Custom Extractors

You can write your own extractors to handle any <head> child element:

export const customLinkExtractor = {
	type: 'collection' as const,
	parent: 'link' as const,
	callback: (node) => {
		const rel = node.attributes.find((a) => a.local === 'rel');
		const href = node.attributes.find((a) => a.local === 'href');
		if (rel != null && href != null) {
			return { key: rel.value, value: href.value };
		}

		return null;
	}
} satisfies TCollectionExtractor;

FAQs

Package last updated on 06 Nov 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts