Skip to content

Commit c988f75

Browse files
Merge pull request #100 from AndrewLeedham/master
Add TypeScript definitions for domToReact
2 parents f4d4588 + 306fceb commit c988f75

File tree

9 files changed

+111
-39
lines changed

9 files changed

+111
-39
lines changed
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
// TypeScript Version: 3.3
22

33
import * as React from 'react';
4+
import { DomElement } from 'domhandler';
5+
import domToReact from './lib/dom-to-react';
6+
import htmlToDOM from 'html-dom-parser';
47

5-
export as namespace HTMLReactParser;
6-
7-
export default HTMLReactParser;
8+
export { DomElement };
89

910
type ReactElement = React.DetailedReactHTMLElement<{}, HTMLElement>;
1011

1112
export interface HTMLReactParserOptions {
1213
// TODO: Replace `object` by type for objects like `{ type: 'h1', props: { children: 'Heading' } }`
13-
replace(domNode: DomNode): React.ReactElement | object | undefined | false;
14+
replace(domNode: DomElement): React.ReactElement | object | undefined | false;
1415
}
1516

1617
/**
1718
* Convert HTML string to React elements.
19+
*
20+
* @param - Raw string of HTML to parse.
21+
* @param options - Options to use when converting to react.
1822
* @returns ReactElement on successful parse or string when `html` cannot be
1923
* parsed as HTML
2024
*/
@@ -23,18 +27,5 @@ declare function HTMLReactParser(
2327
options?: HTMLReactParserOptions
2428
): ReactElement | ReactElement[] | string;
2529

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-
}
30+
export { domToReact, htmlToDOM };
31+
export default HTMLReactParser;

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ function HTMLReactParser(html, options) {
2323
* Export HTML to React parser.
2424
*/
2525
module.exports = HTMLReactParser;
26+
27+
module.exports.domToReact = domToReact;
28+
module.exports.htmlToDOM = htmlToDOM;

lib/dom-to-react.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// TypeScript Version: 3.3
2+
3+
import * as React from 'react';
4+
import { HTMLReactParserOptions } from '..';
5+
import { DomElement } from 'domhandler';
6+
7+
/**
8+
* Converts DOM nodes to React elements.
9+
*
10+
* @param nodes - A list of formatted DomNodes to convert to React.
11+
* @param options - Options to use when converting to react.
12+
* @returns ReactElement or and array of ReactElements.
13+
*/
14+
export default function domToReact(
15+
nodes: DomElement[],
16+
options?: HTMLReactParserOptions
17+
): React.ReactElement | React.ReactElement[];

types/test.tsx renamed to lint/types/index.test.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import parse, { HTMLReactParserOptions } from 'html-dom-parser';
1+
import parse, { HTMLReactParserOptions, domToReact, htmlToDOM } from 'html-react-parser';
22
import * as React from 'react';
33

44
// $ExpectType string | DetailedReactHTMLElement<{}, HTMLElement> | DetailedReactHTMLElement<{}, HTMLElement>[]
@@ -35,3 +35,12 @@ parse('<a id="header" href="#">Heading</a>', {
3535
}
3636
}
3737
});
38+
39+
// $ExpectType DomElement[]
40+
const dom = htmlToDOM('<div>text</div>');
41+
42+
/* $ExpectType ReactElement<any, string | ((props: any) => ReactElement<any, string | any | (new (props: any) => Component<any, any, any>)> | null) |
43+
(new (props: any) => Component<any, any, any>)> |
44+
ReactElement<any, string | ((props: any) => ReactElement<any, string | any | (new (props: any) => Component<any, any, any>)> | null) |
45+
(new (props: any) => Component<any, any, any>)>[] */
46+
domToReact(dom);

lint/types/lib/dom-to-react.test.tsx

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

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"description": "An HTML to React parser.",
55
"author": "Mark <mark@remarkablemark.org>",
66
"main": "index.js",
7-
"types": "types",
87
"scripts": {
98
"benchmark": "node benchmark",
109
"build": "npm run clean && npm run build:min && npm run build:unmin",
@@ -15,7 +14,7 @@
1514
"coveralls": "cat coverage/lcov.info | coveralls",
1615
"lint": "eslint --ignore-path .gitignore .",
1716
"lint:fix": "npm run lint -- --fix",
18-
"dtslint": "dtslint types",
17+
"dtslint": "dtslint .",
1918
"prepublishOnly": "npm run build",
2019
"release": "standard-version --no-verify",
2120
"test": "mocha"
@@ -35,7 +34,8 @@
3534
"converter"
3635
],
3736
"dependencies": {
38-
"html-dom-parser": "0.1.3",
37+
"@types/domhandler": "2.4.1",
38+
"html-dom-parser": "0.2.1",
3939
"react-dom-core": "0.0.4",
4040
"style-to-object": "0.2.2"
4141
},
@@ -67,7 +67,7 @@
6767
"files": [
6868
"dist",
6969
"lib",
70-
"types/index.d.ts"
70+
"index.d.ts"
7171
],
7272
"license": "MIT"
7373
}

tsconfig.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": ["es6"],
5+
"noImplicitAny": true,
6+
"noImplicitThis": true,
7+
"strictNullChecks": true,
8+
"strictFunctionTypes": true,
9+
"baseUrl": ".",
10+
"types": [],
11+
"noEmit": true,
12+
"forceConsistentCasingInFileNames": true,
13+
"paths": { "html-react-parser": ["."], "html-react-parser/lib/*": ["./lib/*"] },
14+
"jsx": "react"
15+
},
16+
"files": [
17+
"index.d.ts",
18+
"lib/dom-to-react.d.ts",
19+
"lint/types/index.test.tsx",
20+
"lint/types/lib/dom-to-react.test.tsx"
21+
]
22+
}

tslint.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "dtslint/dtslint.json"
3+
}

types/tsconfig.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)