Skip to content

Commit 903a671

Browse files
Rokt33rwooorm
authored andcommitted
Add types
Closes GH-17. Reviewed-by: Christian Murphy <[email protected]> Reviewed-by: Titus Wormer <[email protected]>
1 parent f387e2d commit 903a671

File tree

5 files changed

+281
-4
lines changed

5 files changed

+281
-4
lines changed

Diff for: package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
],
2121
"files": [
2222
"lib",
23-
"index.js"
23+
"index.js",
24+
"types/index.d.ts"
2425
],
26+
"types": "types/index.d.ts",
2527
"dependencies": {
2628
"ccount": "^1.0.0",
2729
"comma-separated-tokens": "^1.0.1",
@@ -30,12 +32,14 @@
3032
"html-void-elements": "^1.0.0",
3133
"property-information": "^5.2.0",
3234
"space-separated-tokens": "^1.0.0",
33-
"stringify-entities": "^2.0.0",
35+
"stringify-entities": "^3.0.0",
3436
"unist-util-is": "^3.0.0",
3537
"xtend": "^4.0.1"
3638
},
3739
"devDependencies": {
40+
"@types/unist": "^2.0.3",
3841
"browserify": "^16.0.0",
42+
"dtslint": "^2.0.5",
3943
"hastscript": "^5.0.0",
4044
"nyc": "^14.0.0",
4145
"prettier": "^1.0.0",
@@ -47,13 +51,14 @@
4751
"xo": "^0.25.0"
4852
},
4953
"scripts": {
50-
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
54+
"format": "remark . -qfo && prettier --write \"**/*.{js,ts}\" && xo --fix",
5155
"build-bundle": "browserify . -s hastUtilToHtml > hast-util-to-html.js",
5256
"build-mangle": "browserify . -s hastUtilToHtml -p tinyify > hast-util-to-html.min.js",
5357
"build": "npm run build-bundle && npm run build-mangle",
5458
"test-api": "node test",
5559
"test-coverage": "nyc --reporter lcov tape test/index.js",
56-
"test": "npm run format && npm run build && npm run test-coverage"
60+
"test-types": "dtslint types",
61+
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
5762
},
5863
"prettier": {
5964
"tabWidth": 2,

Diff for: types/index.d.ts

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
// TypeScript Version: 3.5
2+
3+
import {Node} from 'unist'
4+
import {StringifyEntitiesOptions} from 'stringify-entities'
5+
6+
declare namespace hastUtilToHtml {
7+
interface HastUtilToHtmlOptions {
8+
/**
9+
* Whether the *root* of the *tree* is in the `'html'` or `'svg'` space.
10+
*
11+
* If an `svg` element is found in the HTML space, `toHtml` automatically switches to the SVG space when entering the element, and switches back when exiting.
12+
*
13+
* @defaultValue 'html'
14+
*/
15+
space: 'html' | 'svg'
16+
17+
/**
18+
* Configuration for `stringify-entities`.
19+
* Do not use `escapeOnly`, `attribute`, or `subset` (`toHtml` already passes those, so they won’t work).
20+
* However, `useNamedReferences`, `useShortestReferences`, and `omitOptionalSemicolons` are all fine.
21+
*
22+
* @defaultValue {}
23+
*/
24+
entities: Partial<
25+
Omit<StringifyEntitiesOptions, 'escapeOnly' | 'attribute' | 'subset'>
26+
>
27+
28+
/**
29+
* Tag names of *elements* to stringify without closing tag.
30+
*
31+
* Not used in the SVG space.
32+
*
33+
* @defaultValue `require('html-void-elements')`
34+
*/
35+
voids: string[]
36+
37+
/**
38+
* Use an `<!DOCTYPE…` instead of `<!doctype…`.
39+
* Useless except for XHTML.
40+
*
41+
* @defaultValue false
42+
*/
43+
upperDoctype: boolean
44+
45+
/**
46+
* Preferred quote to use.
47+
*
48+
* @defaultValue '"'
49+
*/
50+
quote: '"' | "'"
51+
52+
/**
53+
* Use the other quote if that results in less bytes.
54+
*
55+
* @defaultValue false
56+
*/
57+
quoteSmart: boolean
58+
59+
/**
60+
* Leave attributes unquoted if that results in less bytes.
61+
*
62+
* Not used in the SVG space.
63+
*
64+
* @defaultValue false
65+
*/
66+
preferUnquoted: boolean
67+
68+
/**
69+
* Omit optional opening and closing tags.
70+
* For example, in `<ol><li>one</li><li>two</li></ol>`, both `</li>` closing tags can be omitted.
71+
* The first because it’s followed by another `li`, the last because it’s followed by nothing.
72+
*
73+
* Not used in the SVG space.
74+
*
75+
* @defaultValue false
76+
*/
77+
omitOptionalTags: boolean
78+
79+
/**
80+
* Collapse empty attributes: `class=""` is stringified as `class` instead.
81+
* **Note**: boolean attributes, such as `hidden`, are always collapsed.
82+
*
83+
* Not used in the SVG space.
84+
*
85+
* @defaultValue false
86+
*/
87+
collapseEmptyAttributes: boolean
88+
89+
/**
90+
* Close self-closing nodes with an extra slash (`/`): `<img />` instead of `<img>`.
91+
* See `tightSelfClosing` to control whether a space is used before the slash.
92+
*
93+
* Not used in the SVG space.
94+
*
95+
* @defaultValue false
96+
*/
97+
closeSelfClosing: boolean
98+
99+
/**
100+
* Close SVG elements without any content with slash (`/`) on the opening tag instead of an end tag: `<circle />` instead of `<circle></circle>`.
101+
* See `tightSelfClosing` to control whether a space is used before the slash.
102+
*
103+
* Not used in the HTML space.
104+
*
105+
* @defaultValue false
106+
*/
107+
closeEmptyElements: boolean
108+
109+
/**
110+
* Do not use an extra space when closing self-closing elements: `<img/>` instead of `<img />`.
111+
* **Note**: Only used if `closeSelfClosing: true` or `closeEmptyElements: true`.
112+
*
113+
* @defaultValue false
114+
*/
115+
tightSelfClosing: boolean
116+
117+
/**
118+
* Join known comma-separated attribute values with just a comma (`,`), instead of padding them on the right as well (`,·`, where `·` represents a space).
119+
*
120+
* @defaultValue false
121+
*/
122+
tightCommaSeparatedLists: boolean
123+
124+
/**
125+
* Join attributes together, without white-space, if possible: `class="a b" title="c d"` is stringified as `class="a b"title="c d"` instead to save bytes.
126+
* **Note**: creates invalid (but working) markup.
127+
*
128+
* Not used in the SVG space.
129+
*
130+
* @defaultValue false
131+
*/
132+
tightAttributes: boolean
133+
134+
/**
135+
* Drop unneeded spaces in doctypes: `<!doctypehtml>` instead of `<!doctype html>` to save bytes.
136+
* **Note**: creates invalid (but working) markup.
137+
*
138+
* @defaultValue false
139+
*/
140+
tightDoctype: boolean
141+
142+
/**
143+
* Do not encode characters which cause parse errors (even though they work), to save bytes.
144+
* **Note**: creates invalid (but working) markup.
145+
*
146+
* Not used in the SVG space.
147+
*
148+
* @defaultValue false
149+
*/
150+
allowParseErrors: boolean
151+
152+
/**
153+
* Do not encode some characters which cause XSS vulnerabilities in older browsers.
154+
* **Note**: Only set this if you completely trust the content.
155+
*
156+
* @defaultValue false
157+
*/
158+
allowDangerousCharacters: boolean
159+
160+
/**
161+
* Allow `raw` nodes and insert them as raw HTML.
162+
* When falsey, encodes `raw` nodes.
163+
* **Note**: Only set this if you completely trust the content.
164+
*
165+
* @defaultValue false
166+
*/
167+
allowDangerousHTML: boolean
168+
}
169+
}
170+
171+
/**
172+
* Stringify the given **hast** *tree*.
173+
*
174+
* @param tree given hast tree
175+
* @param options configuration for stringifier
176+
*/
177+
declare function hastUtilToHtml(
178+
tree: Node,
179+
options?: Partial<hastUtilToHtml.HastUtilToHtmlOptions>
180+
): string
181+
182+
export = hastUtilToHtml

Diff for: types/tests.ts

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import toHtml = require('hast-util-to-html')
2+
import {Node} from 'unist'
3+
4+
const node: Node = {
5+
type: 'element',
6+
tagName: 'div'
7+
}
8+
9+
const result: string = toHtml(node)
10+
toHtml(node, {
11+
space: 'html'
12+
})
13+
toHtml(node, {
14+
space: 'svg'
15+
})
16+
toHtml(node, {
17+
space: 'svg'
18+
})
19+
toHtml(node, {entities: {useNamedReferences: true}})
20+
// $ExpectError
21+
toHtml(node, {entities: {escapeOnly: true}})
22+
// $ExpectError
23+
toHtml(node, {entities: {attribute: true}})
24+
// $ExpectError
25+
toHtml(node, {entities: {subset: ['subset']}})
26+
toHtml(node, {
27+
voids: ['hr']
28+
})
29+
toHtml(node, {
30+
upperDoctype: true
31+
})
32+
toHtml(node, {
33+
quote: "'"
34+
})
35+
toHtml(node, {
36+
quoteSmart: true
37+
})
38+
toHtml(node, {
39+
preferUnquoted: true
40+
})
41+
toHtml(node, {
42+
omitOptionalTags: true
43+
})
44+
toHtml(node, {
45+
collapseEmptyAttributes: true
46+
})
47+
toHtml(node, {
48+
closeSelfClosing: true
49+
})
50+
toHtml(node, {
51+
closeEmptyElements: true
52+
})
53+
toHtml(node, {
54+
tightSelfClosing: true
55+
})
56+
toHtml(node, {
57+
tightCommaSeparatedLists: true
58+
})
59+
toHtml(node, {
60+
tightAttributes: true
61+
})
62+
toHtml(node, {
63+
tightDoctype: true
64+
})
65+
toHtml(node, {
66+
allowParseErrors: true
67+
})
68+
toHtml(node, {
69+
allowDangerousCharacters: true
70+
})
71+
toHtml(node, {
72+
allowDangerousHTML: true
73+
})

Diff for: types/tsconfig.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2015"],
4+
"strict": true,
5+
"baseUrl": ".",
6+
"paths": {
7+
"hast-util-to-html": ["index.d.ts"]
8+
}
9+
}
10+
}

Diff for: types/tslint.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"semicolon": false,
5+
"whitespace": false
6+
}
7+
}

0 commit comments

Comments
 (0)