Skip to content

Commit

Permalink
Merge pull request #96 from nikolay-borzov/typescript-definitions
Browse files Browse the repository at this point in the history
chore(typescript): add definitions
  • Loading branch information
remarkablemark authored Mar 18, 2019
2 parents 3722662 + 38e6bba commit 68b403d
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ install:
- npm install
script:
- npm run lint
- npm run dtslint
- npm run cover
- npm run build
- npm run benchmark
Expand Down
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
"description": "An HTML to React parser.",
"author": "Mark <[email protected]>",
"main": "index.js",
"types": "types",
"scripts": {
"benchmark": "node benchmark",
"build": "npm run clean && npm run build:min && npm run build:unmin",
"build:min": "NODE_ENV=production webpack -o dist/html-react-parser.min.js",
"build:unmin": "NODE_ENV=development webpack -o dist/html-react-parser.js",
"clean": "rm -rf dist",
"build:min": "cross-env NODE_ENV=production webpack -o dist/html-react-parser.min.js",
"build:unmin": "cross-env NODE_ENV=development webpack -o dist/html-react-parser.js",
"clean": "rimraf dist",
"cover": "istanbul cover _mocha -- -R spec \"test/**/*\"",
"coveralls": "cat coverage/lcov.info | coveralls",
"lint": "eslint --ignore-path .gitignore .",
"lint:fix": "npm run lint -- --fix",
"dtslint": "dtslint types",
"prepublishOnly": "npm run build",
"release": "standard-version --no-verify",
"test": "mocha"
Expand All @@ -40,8 +42,11 @@
"devDependencies": {
"@commitlint/cli": "^7.2.1",
"@commitlint/config-conventional": "^7.1.2",
"@types/react": "16.8.8",
"benchmark": "2.1.4",
"coveralls": "^3.0.2",
"cross-env": "5.2.0",
"dtslint": "0.5.5",
"eslint": "^5.10.0",
"eslint-plugin-prettier": "^3.0.0",
"husky": "^1.3.0",
Expand All @@ -51,6 +56,7 @@
"prettier": "^1.15.3",
"react": "^16",
"react-dom": "^16",
"rimraf": "2.6.3",
"standard-version": "^4.4.0",
"webpack": "^4.27.1",
"webpack-cli": "^3.1.2"
Expand All @@ -60,7 +66,8 @@
},
"files": [
"dist",
"lib"
"lib",
"types/index.d.ts"
],
"license": "MIT"
}
40 changes: 40 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// TypeScript Version: 3.3

import * as React from 'react';

export as namespace HTMLReactParser;

export default HTMLReactParser;

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

export interface HTMLReactParserOptions {
// TODO: Replace `object` by type for objects like `{ type: 'h1', props: { children: 'Heading' } }`
replace(domNode: DomNode): React.ReactElement | object | undefined | false;
}

/**
* Convert HTML string to React elements.
* @returns ReactElement on successful parse or string when `html` cannot be
* parsed as HTML
*/
declare function HTMLReactParser(
html: string,
options?: HTMLReactParserOptions
): ReactElement | ReactElement[] | string;

/** domhandler node */
export interface DomNode {
type: 'tag' | 'text' | 'directive' | 'comment' | 'script' | 'style';
name: string;
data?: string;
attribs?: {
[attributeName: string]: string;
};
children?: DomNode[];
parent?: DomNode;
prev?: DomNode;
next?: DomNode;
startIndex?: number;
endIndex?: number;
}
37 changes: 37 additions & 0 deletions types/test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import parse, { HTMLReactParserOptions } from 'html-dom-parser';
import * as React from 'react';

// $ExpectType string | DetailedReactHTMLElement<{}, HTMLElement> | DetailedReactHTMLElement<{}, HTMLElement>[]
parse('<div>text</div>');

// `options.replace`

// Return React.createElement from `replace`
parse('<p id="replace">text</p>', {
replace: domNode => {
if (domNode.attribs && domNode.attribs.id === 'replace') {
return React.createElement('span', {}, 'replaced');
}
}
});

// Return ReactElement
const options: HTMLReactParserOptions = {
replace({ attribs }) {
return attribs && attribs.id === 'remove' && <React.Fragment />;
}
};

parse('<p><br id="remove"></p>', options);

// Return domhandler node
parse('<a id="header" href="#">Heading</a>', {
replace: node => {
if (node.attribs && node.attribs.id === 'header') {
return {
type: 'h1',
props: { children: 'Heading' }
};
}
}
});
15 changes: 15 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "esnext",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noEmit": true,
"jsx": "react",
"baseUrl": ".",
"paths": { "html-dom-parser": ["."] },
"moduleResolution": "node"
}
}

0 comments on commit 68b403d

Please sign in to comment.