Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 2, 2021
1 parent f1e7eb9 commit b293ef3
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
coverage/
node_modules/
.DS_Store
*.d.ts
*.log
yarn.lock
27 changes: 17 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @typedef {import('hast').Root} Root
*/

import Slugger from 'github-slugger'
import {hasProperty} from 'hast-util-has-property'
import {headingRank} from 'hast-util-heading-rank'
Expand All @@ -6,16 +10,19 @@ import {visit} from 'unist-util-visit'

const slugs = new Slugger()

/**
* Plugin to add `id`s to headings.
*
* @type {import('unified').Plugin<[], Root>}
*/
export default function rehypeSlug() {
return transformer
}

function transformer(tree) {
slugs.reset()
return (tree) => {
slugs.reset()

visit(tree, 'element', (node) => {
if (headingRank(node) && !hasProperty(node, 'id')) {
node.properties.id = slugs.slug(toString(node))
}
})
visit(tree, 'element', (node) => {
if (headingRank(node) && node.properties && !hasProperty(node, 'id')) {
node.properties.id = slugs.slug(toString(node))
}
})
}
}
24 changes: 18 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,41 @@
],
"sideEffects": false,
"type": "module",
"types": "index.d.ts",
"main": "index.js",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/hast": "^2.0.0",
"github-slugger": "^1.1.1",
"hast-util-has-property": "^2.0.0",
"hast-util-heading-rank": "^2.0.0",
"hast-util-to-string": "^2.0.0",
"unified": "^10.0.0",
"unist-util-visit": "^4.0.0"
},
"devDependencies": {
"@types/github-slugger": "^1.3.0",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"rehype": "^12.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"unified": "^10.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.39.0"
},
"scripts": {
"build": "rimraf \"*.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 npm run test-api",
"test": "npm run format && npm run test-coverage"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -61,14 +70,17 @@
"trailingComma": "none"
},
"xo": {
"prettier": true,
"ignores": [
"types/"
]
"prettier": true
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"include": ["*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"strict": true
}
}
9 changes: 0 additions & 9 deletions types/index.d.ts

This file was deleted.

4 changes: 0 additions & 4 deletions types/rehype-slug-tests.ts

This file was deleted.

10 changes: 0 additions & 10 deletions types/tsconfig.json

This file was deleted.

7 changes: 0 additions & 7 deletions types/tslint.json

This file was deleted.

0 comments on commit b293ef3

Please sign in to comment.