Skip to content

Commit 68b403d

Browse files
Merge pull request #96 from nikolay-borzov/typescript-definitions
chore(typescript): add definitions
2 parents 3722662 + 38e6bba commit 68b403d

File tree

5 files changed

+104
-4
lines changed

5 files changed

+104
-4
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ install:
55
- npm install
66
script:
77
- npm run lint
8+
- npm run dtslint
89
- npm run cover
910
- npm run build
1011
- npm run benchmark

package.json

+11-4
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
"description": "An HTML to React parser.",
55
"author": "Mark <[email protected]>",
66
"main": "index.js",
7+
"types": "types",
78
"scripts": {
89
"benchmark": "node benchmark",
910
"build": "npm run clean && npm run build:min && npm run build:unmin",
10-
"build:min": "NODE_ENV=production webpack -o dist/html-react-parser.min.js",
11-
"build:unmin": "NODE_ENV=development webpack -o dist/html-react-parser.js",
12-
"clean": "rm -rf dist",
11+
"build:min": "cross-env NODE_ENV=production webpack -o dist/html-react-parser.min.js",
12+
"build:unmin": "cross-env NODE_ENV=development webpack -o dist/html-react-parser.js",
13+
"clean": "rimraf dist",
1314
"cover": "istanbul cover _mocha -- -R spec \"test/**/*\"",
1415
"coveralls": "cat coverage/lcov.info | coveralls",
1516
"lint": "eslint --ignore-path .gitignore .",
1617
"lint:fix": "npm run lint -- --fix",
18+
"dtslint": "dtslint types",
1719
"prepublishOnly": "npm run build",
1820
"release": "standard-version --no-verify",
1921
"test": "mocha"
@@ -40,8 +42,11 @@
4042
"devDependencies": {
4143
"@commitlint/cli": "^7.2.1",
4244
"@commitlint/config-conventional": "^7.1.2",
45+
"@types/react": "16.8.8",
4346
"benchmark": "2.1.4",
4447
"coveralls": "^3.0.2",
48+
"cross-env": "5.2.0",
49+
"dtslint": "0.5.5",
4550
"eslint": "^5.10.0",
4651
"eslint-plugin-prettier": "^3.0.0",
4752
"husky": "^1.3.0",
@@ -51,6 +56,7 @@
5156
"prettier": "^1.15.3",
5257
"react": "^16",
5358
"react-dom": "^16",
59+
"rimraf": "2.6.3",
5460
"standard-version": "^4.4.0",
5561
"webpack": "^4.27.1",
5662
"webpack-cli": "^3.1.2"
@@ -60,7 +66,8 @@
6066
},
6167
"files": [
6268
"dist",
63-
"lib"
69+
"lib",
70+
"types/index.d.ts"
6471
],
6572
"license": "MIT"
6673
}

types/index.d.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// TypeScript Version: 3.3
2+
3+
import * as React from 'react';
4+
5+
export as namespace HTMLReactParser;
6+
7+
export default HTMLReactParser;
8+
9+
type ReactElement = React.DetailedReactHTMLElement<{}, HTMLElement>;
10+
11+
export interface HTMLReactParserOptions {
12+
// TODO: Replace `object` by type for objects like `{ type: 'h1', props: { children: 'Heading' } }`
13+
replace(domNode: DomNode): React.ReactElement | object | undefined | false;
14+
}
15+
16+
/**
17+
* Convert HTML string to React elements.
18+
* @returns ReactElement on successful parse or string when `html` cannot be
19+
* parsed as HTML
20+
*/
21+
declare function HTMLReactParser(
22+
html: string,
23+
options?: HTMLReactParserOptions
24+
): ReactElement | ReactElement[] | string;
25+
26+
/** domhandler node */
27+
export interface DomNode {
28+
type: 'tag' | 'text' | 'directive' | 'comment' | 'script' | 'style';
29+
name: string;
30+
data?: string;
31+
attribs?: {
32+
[attributeName: string]: string;
33+
};
34+
children?: DomNode[];
35+
parent?: DomNode;
36+
prev?: DomNode;
37+
next?: DomNode;
38+
startIndex?: number;
39+
endIndex?: number;
40+
}

types/test.tsx

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import parse, { HTMLReactParserOptions } from 'html-dom-parser';
2+
import * as React from 'react';
3+
4+
// $ExpectType string | DetailedReactHTMLElement<{}, HTMLElement> | DetailedReactHTMLElement<{}, HTMLElement>[]
5+
parse('<div>text</div>');
6+
7+
// `options.replace`
8+
9+
// Return React.createElement from `replace`
10+
parse('<p id="replace">text</p>', {
11+
replace: domNode => {
12+
if (domNode.attribs && domNode.attribs.id === 'replace') {
13+
return React.createElement('span', {}, 'replaced');
14+
}
15+
}
16+
});
17+
18+
// Return ReactElement
19+
const options: HTMLReactParserOptions = {
20+
replace({ attribs }) {
21+
return attribs && attribs.id === 'remove' && <React.Fragment />;
22+
}
23+
};
24+
25+
parse('<p><br id="remove"></p>', options);
26+
27+
// Return domhandler node
28+
parse('<a id="header" href="#">Heading</a>', {
29+
replace: node => {
30+
if (node.attribs && node.attribs.id === 'header') {
31+
return {
32+
type: 'h1',
33+
props: { children: 'Heading' }
34+
};
35+
}
36+
}
37+
});

types/tsconfig.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"module": "esnext",
4+
"lib": ["es6"],
5+
"noImplicitAny": true,
6+
"noImplicitThis": true,
7+
"strictNullChecks": true,
8+
"strictFunctionTypes": true,
9+
"noEmit": true,
10+
"jsx": "react",
11+
"baseUrl": ".",
12+
"paths": { "html-dom-parser": ["."] },
13+
"moduleResolution": "node"
14+
}
15+
}

0 commit comments

Comments
 (0)