Skip to content

Commit da63b16

Browse files
New: Convert html to ast
1 parent 7f82cc2 commit da63b16

File tree

6 files changed

+104
-14
lines changed

6 files changed

+104
-14
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ install:
1111

1212
script:
1313
- npm run lint
14+
- npm run test
1415
- npm run build
1516

1617
after_success:

package-lock.json

Lines changed: 77 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"devDependencies": {
2929
"@semantic-release/changelog": "2.1.1",
3030
"@semantic-release/git": "6.0.1",
31+
"@types/htmlparser2": "3.7.31",
3132
"@types/jest": "23.1.4",
3233
"commitlint": "7.0.0",
3334
"conventional-changelog-eslint": "3.0.0",
@@ -39,5 +40,9 @@
3940
"tslint": "5.10.0",
4041
"tslint-react": "3.6.0",
4142
"typescript": "2.9.2"
43+
},
44+
"dependencies": {
45+
"domhandler": "2.4.2",
46+
"htmlparser2": "3.9.2"
4247
}
4348
}

src/domhandler.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'domhandler';

src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import DomHandler from 'domhandler'
2+
import { Options, Parser } from 'htmlparser2'
3+
4+
export function parse(html: string, options: Options) {
5+
if (typeof html !== 'string') {
6+
throw new TypeError('First argument must be a string.')
7+
}
8+
const handler = new DomHandler()
9+
const parser = new Parser(handler, options)
10+
parser.write(html)
11+
parser.end()
12+
return handler.dom
13+
}

tests/api.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as htmldomToReact from '../src/index'
2+
3+
describe('Public API', () => {
4+
it('should export a parse function', () => {
5+
expect(htmldomToReact.parse).toBeDefined()
6+
})
7+
})

0 commit comments

Comments
 (0)