Skip to content

Commit

Permalink
Add augmentation of types in file.data.meta
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 12, 2022
1 parent 81e2c1a commit 81fa111
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
*.d.ts
*.log
coverage/
node_modules/
index.d.ts
test.d.ts
yarn.lock
15 changes: 15 additions & 0 deletions complex-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Need a random export to turn this into a module?
export type Whatever = unknown

declare module 'vfile' {
interface DataMap {
meta: {
/**
* Estimated reading time of a document.
*
* Populated by `rehype-infer-reading-time-meta` from the HTML.
*/
readingTime?: number | [number, number]
}
}
}
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @typedef {import('./complex-types.js')} DoNotTouchThisAsItIncludesAugmentation
*
* @typedef {import('hast').Root} Root
* @typedef {import('hast').Element} Element
*
* @typedef Options
* Configuration.
Expand Down Expand Up @@ -48,9 +49,7 @@ export default function rehypeInferReadingTimeMeta(options = {}) {
const matter = /** @type {Record<string, unknown>} */ (
file.data.matter || {}
)
const meta = /** @type {Record<string, unknown>} */ (
file.data.meta || (file.data.meta = {})
)
const meta = file.data.meta || (file.data.meta = {})

if (!matter.readingTime && !meta.readingTime) {
meta.readingTime = time
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"main": "index.js",
"types": "index.d.ts",
"files": [
"complex-types.d.ts",
"index.d.ts",
"index.js"
],
Expand All @@ -56,7 +57,7 @@
"xo": "^0.50.0"
},
"scripts": {
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"build": "rimraf \"{index,test}.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
Expand Down
52 changes: 32 additions & 20 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ is shown on Slack or certain other services.
## Install

This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [npm][]:

```sh
npm install rehype-infer-reading-time-meta
Expand Down Expand Up @@ -82,25 +82,21 @@ import rehypeMeta from 'rehype-meta'
import rehypeFormat from 'rehype-format'
import rehypeStringify from 'rehype-stringify'

main()

async function main() {
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeInferReadingTimeMeta)
.use(rehypeDocument)
.use(rehypeMeta, {twitter: true})
.use(rehypeFormat)
.use(rehypeStringify)
.process(
'<h1>Build</h1><p><strong>We provide the building blocks</strong>: from tiny, focussed, modular utilities to plugins that combine them to perform bigger tasks. And much, much more. You can build on unified, mixing and matching building blocks together, to make all kinds of interesting new things.</p>'
)

console.log(String(file))
}
const file = await unified()
.use(rehypeParse, {fragment: true})
.use(rehypeInferReadingTimeMeta)
.use(rehypeDocument)
.use(rehypeMeta, {twitter: true})
.use(rehypeFormat)
.use(rehypeStringify)
.process(
'<h1>Build</h1><p><strong>We provide the building blocks</strong>: from tiny, focussed, modular utilities to plugins that combine them to perform bigger tasks. And much, much more. You can build on unified, mixing and matching building blocks together, to make all kinds of interesting new things.</p>'
)

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

Now running `node example.js` yields:
…now running `node example.js` yields:

```html
<!doctype html>
Expand Down Expand Up @@ -152,13 +148,29 @@ other random stuff, by focussing on one element.
## Types

This package is fully typed with [TypeScript][].
The extra type `Options` is exported.
The additional type `Options` is exported.

It also registers the `file.data.meta` fields 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.readingTime) //=> TS now knows that this is a `(number | [number, number])?`.
```

## Compatibility

Projects maintained by the unified collective are compatible with all maintained
versions of Node.js.
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
Our projects sometimes work with older versions, but this is not guaranteed.

This plugin works with `rehype-parse` version 3+, `rehype-stringify` version 3+,
Expand Down

0 comments on commit 81fa111

Please sign in to comment.