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

1.0.8 • Public • Published

html-metadata-reader

Coverage lines Coverage functions Coverage branches Coverage statements

Node JS solution to read all meta tags and their values from an HTML document. This is useful in cases where we need to read metadata from a document on a Node Js server, where we cannot do a document query. Unlike other solutions, this will retrieve all meta tag information, including arbitrary/non-standard tags.

For a demo + how to use: https://bytethisstore.com/articles/pg/html-document-metadata

Usage

const htmlDocumentMetadataReader = new HtmlDocumentMetadataReader();
const tags = htmlDocumentMetadataReader.readTagsFromDocument(htmlString);
console.log(tags);

The result will have this schema:

/*
* The structure of a tag is split as follows:
* <meta {key.labelName}="{key.value}" {value.labelName}="{value.value}">
* <meta {key.value}="{value.value}">
*/
[
    //title is included, even though it is not technically a meta tag
    {
        "key": {
            "labelName": "",
            "value": "title"
        },
        "value": {
            "labelName": "",
            "value": "Test Page"
        }
    },
    {
        "key": {
            "labelName": "",
            "value": "charset"
        },
        "value": {
            "labelName": "",
            "value": "utf-8"
        }
    },
    {
        "key": {
            "labelName": "name",
            "value": "description"
        },
        "value": {
            "labelName": "value",
            "value": "something"
        }
    },
    {
        "key": {
            "labelName": "name",
            "value": "author"
        },
        "value": {
            "labelName": "content",
            "value": "Byte This!"
        }
    },
    {
        "key": {
            "labelName": "property",
            "value": "og:description"
        },
        "value": {
            "labelName": "content",
            "value": "something"
        }
    }
]

This can be formatted into a key-value pair object:

const keyValueObj = DocumentMetadataFormatter.toDictionary(metaTagsAr);

Readme

Keywords

none

Package Sidebar

Install

npm i @byte-this/html-metadata-reader

Weekly Downloads

2

Version

1.0.8

License

ISC

Unpacked Size

23.3 kB

Total Files

20

Last publish

Collaborators

  • byte-this