Skip to content

Commit 81fa111

Browse files
committed
Add augmentation of types in file.data.meta
1 parent 81e2c1a commit 81fa111

File tree

5 files changed

+54
-26
lines changed

5 files changed

+54
-26
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
2-
*.d.ts
32
*.log
43
coverage/
54
node_modules/
5+
index.d.ts
6+
test.d.ts
67
yarn.lock

complex-types.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Need a random export to turn this into a module?
2+
export type Whatever = unknown
3+
4+
declare module 'vfile' {
5+
interface DataMap {
6+
meta: {
7+
/**
8+
* Estimated reading time of a document.
9+
*
10+
* Populated by `rehype-infer-reading-time-meta` from the HTML.
11+
*/
12+
readingTime?: number | [number, number]
13+
}
14+
}
15+
}

index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
2+
* @typedef {import('./complex-types.js')} DoNotTouchThisAsItIncludesAugmentation
3+
*
24
* @typedef {import('hast').Root} Root
3-
* @typedef {import('hast').Element} Element
45
*
56
* @typedef Options
67
* Configuration.
@@ -48,9 +49,7 @@ export default function rehypeInferReadingTimeMeta(options = {}) {
4849
const matter = /** @type {Record<string, unknown>} */ (
4950
file.data.matter || {}
5051
)
51-
const meta = /** @type {Record<string, unknown>} */ (
52-
file.data.meta || (file.data.meta = {})
53-
)
52+
const meta = file.data.meta || (file.data.meta = {})
5453

5554
if (!matter.readingTime && !meta.readingTime) {
5655
meta.readingTime = time

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"main": "index.js",
3333
"types": "index.d.ts",
3434
"files": [
35+
"complex-types.d.ts",
3536
"index.d.ts",
3637
"index.js"
3738
],
@@ -56,7 +57,7 @@
5657
"xo": "^0.50.0"
5758
},
5859
"scripts": {
59-
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
60+
"build": "rimraf \"{index,test}.d.ts\" && tsc && type-coverage",
6061
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
6162
"test-api": "node --conditions development test.js",
6263
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",

readme.md

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ is shown on Slack or certain other services.
4949
## Install
5050

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

5454
```sh
5555
npm install rehype-infer-reading-time-meta
@@ -82,25 +82,21 @@ import rehypeMeta from 'rehype-meta'
8282
import rehypeFormat from 'rehype-format'
8383
import rehypeStringify from 'rehype-stringify'
8484

85-
main()
86-
87-
async function main() {
88-
const file = await unified()
89-
.use(rehypeParse, {fragment: true})
90-
.use(rehypeInferReadingTimeMeta)
91-
.use(rehypeDocument)
92-
.use(rehypeMeta, {twitter: true})
93-
.use(rehypeFormat)
94-
.use(rehypeStringify)
95-
.process(
96-
'<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>'
97-
)
98-
99-
console.log(String(file))
100-
}
85+
const file = await unified()
86+
.use(rehypeParse, {fragment: true})
87+
.use(rehypeInferReadingTimeMeta)
88+
.use(rehypeDocument)
89+
.use(rehypeMeta, {twitter: true})
90+
.use(rehypeFormat)
91+
.use(rehypeStringify)
92+
.process(
93+
'<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>'
94+
)
95+
96+
console.log(String(file))
10197
```
10298

103-
Now running `node example.js` yields:
99+
…now running `node example.js` yields:
104100

105101
```html
106102
<!doctype html>
@@ -152,13 +148,29 @@ other random stuff, by focussing on one element.
152148
## Types
153149

154150
This package is fully typed with [TypeScript][].
155-
The extra type `Options` is exported.
151+
The additional type `Options` is exported.
152+
153+
It also registers the `file.data.meta` fields with `vfile`.
154+
If you’re working with the file, make sure to import this plugin somewhere in
155+
your types, as that registers the new fields on the file.
156+
157+
```js
158+
/**
159+
* @typedef {import('rehype-infer-title-meta')}
160+
*/
161+
162+
import {VFile} from 'vfile'
163+
164+
const file = new VFile()
165+
166+
console.log(file.data.meta.readingTime) //=> TS now knows that this is a `(number | [number, number])?`.
167+
```
156168

157169
## Compatibility
158170

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

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

0 commit comments

Comments
 (0)