Skip to content

Commit

Permalink
Add types
Browse files Browse the repository at this point in the history
Closes GH-6.

Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
Reviewed-by: Remco Haszing <remcohaszing@gmail.com>
Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
Christian Ivicevic committed Nov 11, 2020
1 parent ef02f86 commit 06ca90a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 3 deletions.
14 changes: 11 additions & 3 deletions package.json
Expand Up @@ -23,7 +23,8 @@
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"files": [
"index.js"
"index.js",
"types/index.d.ts"
],
"dependencies": {
"extend": "^3.0.1",
Expand All @@ -32,8 +33,10 @@
"unist-util-visit": "^2.0.0"
},
"devDependencies": {
"@types/hast": "^2.0.0",
"bail": "^1.0.0",
"browserify": "^16.0.0",
"dtslint": "^4.0.0",
"is-hidden": "^1.0.0",
"negate": "^1.0.0",
"nyc": "^15.0.0",
Expand All @@ -44,6 +47,7 @@
"tape": "^5.0.0",
"tinyify": "^2.0.0",
"to-vfile": "^6.0.0",
"unified": "^9.0.0",
"xo": "^0.32.0"
},
"scripts": {
Expand All @@ -53,13 +57,17 @@
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test": "npm run format && npm run build && npm run test-coverage"
"test-types": "dtslint types",
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
"branches": 100,
"ignores": [
"**/*.ts"
]
},
"prettier": {
"tabWidth": 2,
Expand Down
41 changes: 41 additions & 0 deletions types/index.d.ts
@@ -0,0 +1,41 @@
// Minimum TypeScript Version: 3.5
import {Plugin} from 'unified'
import {Node, Properties} from 'hast'

/**
* Automatically add links to headings.
*/
declare const autolinkHeadings: Plugin<[autolinkHeadings.Options?]>

declare namespace autolinkHeadings {
interface Options {
/**
* How to create links.
*
* @default 'prepend'
*/
behavior?: 'prepend' | 'append' | 'wrap' | 'before' | 'after'

/**
* Extra properties to set on the link.
*
* @default {ariaHidden: true, tabIndex: -1}
*/
properties?: Properties | Properties[]

/**
* `hast` nodes to insert in the link.
*
* @default { type: 'element', tagName: 'span', properties: {className: ['icon', 'icon-link']}, children: [] }
*/
content?: Node | ((heading: Node) => Node[])

/**
* `hast` node to wrap the heading and link with, if `behavior` is
* `'before'` or `'after'`. There is no default.
*/
group?: Node | ((heading: Node) => Node)
}
}

export = autolinkHeadings
47 changes: 47 additions & 0 deletions types/rehype-autolink-headings-tests.ts
@@ -0,0 +1,47 @@
import unified = require('unified')
import autolinkHeadings = require('rehype-autolink-headings')

unified().use(autolinkHeadings)
unified().use(autolinkHeadings, {behavior: 'prepend'})
unified().use(autolinkHeadings, {behavior: 'append'})
unified().use(autolinkHeadings, {behavior: 'wrap'})
unified().use(autolinkHeadings, {behavior: 'before'})
unified().use(autolinkHeadings, {behavior: 'after'})
unified().use(autolinkHeadings, {properties: {ariaHidden: true, tabIndex: -1}})
unified().use(autolinkHeadings, {
properties: [{ariaHidden: true, tabIndex: -1}]
})
unified().use(autolinkHeadings, {
content: {
type: 'element',
tagName: 'span',
properties: {className: ['icon', 'icon-link']},
children: []
}
})
unified().use(autolinkHeadings, {
content: (_currentHeading) => [
{
type: 'element',
tagName: 'span',
properties: {className: ['icon', 'icon-link']},
children: []
}
]
})
unified().use(autolinkHeadings, {
group: {
type: 'element',
tagName: 'span',
properties: {className: ['icon', 'icon-link']},
children: []
}
})
unified().use(autolinkHeadings, {
group: (_currentHeading) => ({
type: 'element',
tagName: 'span',
properties: {className: ['icon', 'icon-link']},
children: []
})
})
10 changes: 10 additions & 0 deletions types/tsconfig.json
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"baseUrl": ".",
"paths": {
"rehype-autolink-headings": ["index.d.ts"]
}
}
}
8 changes: 8 additions & 0 deletions types/tslint.json
@@ -0,0 +1,8 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"no-redundant-jsdoc": false,
"semicolon": false,
"whitespace": false
}
}

0 comments on commit 06ca90a

Please sign in to comment.