# rehype-infer-title-meta

> rehype plugin to infer file metadata from the main title of a document

Latest version **2.0.0** (published 2023-09-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install rehype-infer-title-meta
pnpm add rehype-infer-title-meta
yarn add rehype-infer-title-meta
bun add rehype-infer-title-meta
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2023-09-02 |
| First published | 2021-09-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 4 |
| Unpacked size | 13.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Titus Wormer |
| Maintainers | wooorm, kmck |
| Keywords | file, hast, html, meta, plugin, rehype, rehype-plugin, title, unified |

## Links

- npm: https://www.npmjs.com/package/rehype-infer-title-meta
- Repository: https://github.com/rehypejs/rehype-infer-title-meta
- Homepage: https://github.com/rehypejs/rehype-infer-title-meta#readme
- Issues: https://github.com/rehypejs/rehype-infer-title-meta/issues
- Funding: https://opencollective.com/unified
- npm.io page: https://npm.io/package/rehype-infer-title-meta

## Dependencies (4)

- [vfile](https://npm.io/package/vfile.md) ^6.0.0
- [@types/hast](https://npm.io/package/@types/hast.md) ^3.0.0
- [hast-util-select](https://npm.io/package/hast-util-select.md) ^6.0.0
- [hast-util-to-text](https://npm.io/package/hast-util-to-text.md) ^4.0.0

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2023-09-02
- 1.1.0 — 2022-06-12
- 1.0.1 — 2022-01-23
- 1.0.0 — 2021-09-09

## README

# rehype-infer-title-meta

[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]

**[rehype][]** plugin to infer the title of a document.

## Contents

*   [What is this?](#what-is-this)
*   [When should I use this?](#when-should-i-use-this)
*   [Install](#install)
*   [Use](#use)
*   [API](#api)
    *   [`unified().use(rehypeInferTitleMeta, options?)`](#unifieduserehypeinfertitlemeta-options)
    *   [`Options`](#options)
*   [Types](#types)
*   [Compatibility](#compatibility)
*   [Security](#security)
*   [Related](#related)
*   [Contribute](#contribute)
*   [License](#license)

## What is this?

This package is a [unified][] ([rehype][]) plugin to infer the title of a
document.

**unified** is a project that transforms content with abstract syntax trees
(ASTs).
**rehype** adds support for HTML to unified.
**vfile** is the virtual file interface used in unified.
**hast** is the HTML AST that rehype uses.
This is a rehype plugin that inspects hast and adds metadata to vfiles.

## When should I use this?

This plugin is particularly useful in combination with
[`rehype-document`][rehype-document] or [`rehype-meta`][rehype-meta].
When both are used together, the `<title>` is populated with the document’s
title.

## Install

This package is [ESM only][esm].
In Node.js (version 16+), install with [npm][]:

```sh
npm install rehype-infer-title-meta
```

In Deno with [`esm.sh`][esmsh]:

```js
import rehypeInferTitleMeta from 'https://esm.sh/rehype-infer-title-meta@2'
```

In browsers with [`esm.sh`][esmsh]:

```html
<script type="module">
  import rehypeInferTitleMeta from 'https://esm.sh/rehype-infer-title-meta@2?bundle'
</script>
```

## Use

Say our module `example.js` looks as follows:

```js
import rehypeDocument from 'rehype-document'
import rehypeFormat from 'rehype-format'
import rehypeInferTitleMeta from 'rehype-infer-title-meta'
import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {unified} from 'unified'

const file = await unified()
  .use(remarkParse)
  .use(remarkRehype)
  .use(rehypeInferTitleMeta)
  .use(rehypeDocument)
  .use(rehypeFormat)
  .use(rehypeStringify)
  .process('# Hello, world!')

console.log(file.data)
console.log(String(file))
```

…now running `node example.js` yields:

```js
{meta: {title: 'Hello, world!'}}
```

```html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Hello, world!</title>
    <meta content="width=device-width, initial-scale=1" name="viewport">
  </head>
  <body>
    <h1>Hello, world!</h1>
  </body>
</html>
```

## API

This package exports no identifiers.
The default export is [`rehypeInferTitleMeta`][api-rehype-infer-title-meta].

### `unified().use(rehypeInferTitleMeta, options?)`

Infer file metadata from the main title of a document.

The result is stored on `file.data.meta.title`.

###### Parameters

*   `options` ([`Options`][api-options], optional)
    — configuration

###### Returns

Transform ([`Transformer`][unified-transformer]).

### `Options`

Configuration (TypeScript type).

###### Fields

*   `selector` (`string`, default: `'h1'`)
    — CSS selector to the title; can be a selector list (such as `'title, h1,
    .main-heading'`), in which case the first element in the tree that matches
    is used

## Types

This package is fully typed with [TypeScript][].
It exports the additional type [`Options`][api-options].

It also registers `file.data.meta` with `vfile`.
If you’re working with the file, make sure to import this plugin somewhere in
your types, as that registers the new fields on the file.

```js
/**
 * @typedef {import('rehype-infer-title-meta')}
 */

import {VFile} from 'vfile'

const file = new VFile()

console.log(file.data.meta.title) //=> TS now knows that this is a `string?`.
```

## Compatibility

Projects maintained by the unified collective are compatible with maintained
versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line,
`rehype-infer-title-meta@^2`, compatible with Node.js 16.

This plugin works with `rehype-parse` version 3+, `rehype-stringify` version 3+,
`rehype` version 4+, and `unified` version 6+.

## Security

Use of `rehype-infer-title-meta` is safe.

## Related

*   [`rehype-document`][rehype-document]
    — wrap a fragment in a document
*   [`rehype-meta`][rehype-meta]
    — add metadata to the head of a document
*   [`unified-infer-git-meta`](https://github.com/unifiedjs/unified-infer-git-meta)
    — infer file metadata from Git
*   [`rehype-infer-description-meta`](https://github.com/rehypejs/rehype-infer-description-meta)
    — infer file metadata from the description of a document
*   [`rehype-infer-reading-time-meta`](https://github.com/rehypejs/rehype-infer-reading-time-meta)
    — infer file metadata from the reading time of a document

## Contribute

See [`contributing.md`][contributing] in [`rehypejs/.github`][health] for ways
to get started.
See [`support.md`][support] for ways to get help.

This project has a [code of conduct][coc].
By interacting with this repository, organization, or community you agree to
abide by its terms.

## License

[MIT][license] © [Titus Wormer][author]

<!-- Definitions -->

[build-badge]: https://github.com/rehypejs/rehype-infer-title-meta/workflows/main/badge.svg

[build]: https://github.com/rehypejs/rehype-infer-title-meta/actions

[coverage-badge]: https://img.shields.io/codecov/c/github/rehypejs/rehype-infer-title-meta.svg

[coverage]: https://codecov.io/github/rehypejs/rehype-infer-title-meta

[downloads-badge]: https://img.shields.io/npm/dm/rehype-infer-title-meta.svg

[downloads]: https://www.npmjs.com/package/rehype-infer-title-meta

[size-badge]: https://img.shields.io/bundlejs/size/rehype-infer-title-meta

[size]: https://bundlejs.com/?q=rehype-infer-title-meta

[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg

[backers-badge]: https://opencollective.com/unified/backers/badge.svg

[collective]: https://opencollective.com/unified

[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg

[chat]: https://github.com/rehypejs/rehype/discussions

[npm]: https://docs.npmjs.com/cli/install

[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

[esmsh]: https://esm.sh

[health]: https://github.com/rehypejs/.github

[contributing]: https://github.com/rehypejs/.github/blob/main/contributing.md

[support]: https://github.com/rehypejs/.github/blob/main/support.md

[coc]: https://github.com/rehypejs/.github/blob/main/code-of-conduct.md

[license]: license

[author]: https://wooorm.com

[rehype]: https://github.com/rehypejs/rehype

[rehype-meta]: https://github.com/rehypejs/rehype-meta

[rehype-document]: https://github.com/rehypejs/rehype-document

[typescript]: https://www.typescriptlang.org

[unified]: https://github.com/unifiedjs/unified

[unified-transformer]: https://github.com/unifiedjs/unified#transformer

[api-options]: #options

[api-rehype-infer-title-meta]: #unifieduserehypeinfertitlemeta-options

---
_Source: https://npm.io/package/rehype-infer-title-meta · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
