Skip to content

Commit b420479

Browse files
committed
🎄
0 parents  commit b420479

File tree

29 files changed

+5243
-0
lines changed

29 files changed

+5243
-0
lines changed

‎.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["env"]
3+
}

‎.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/dist

‎.eslintrc.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = {
2+
extends: [
3+
'airbnb-base'
4+
],
5+
parser: 'babel-eslint',
6+
env: {
7+
es6: true
8+
},
9+
overrides: [{
10+
files: ['*.test.*'],
11+
env: {
12+
jest: true
13+
}
14+
}, {
15+
files: ['src/*'],
16+
env: {
17+
browser: true
18+
},
19+
}],
20+
rules: {
21+
'no-use-before-define': 'off',
22+
},
23+
};

‎.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/node_modules
2+
npm-debug.log
3+
yarn-error.log
4+
.DS_Store
5+
/dist

‎LICENSE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(ISC License)
2+
3+
Copyright (c) 2018 Keith McKnight <[email protected]>
4+
5+
Permission to use, copy, modify, and/or distribute this software for any purpose
6+
with or without fee is hereby granted, provided that the above copyright notice
7+
and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
13+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
14+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
15+
THIS SOFTWARE.

‎README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# hast-util-from-parse5
2+
3+
Transform a DOM tree to [HAST][]
4+
5+
## Installation
6+
7+
[yarn][]:
8+
9+
```bash
10+
yarn add hast-util-from-dom
11+
```
12+
13+
[npm][]:
14+
15+
```bash
16+
npm install hast-util-from-dom
17+
```
18+
19+
## Usage
20+
21+
This utility is similar to [`hast-util-from-parse5`][hast-util-from-parse5], but is intended for browser user and therefore relies on the native DOM API instead of an external parsing library.
22+
23+
Say we have the following file, `example.html`:
24+
25+
```html
26+
<!doctype html><title>Hello!</title><h1 id="world">World!<!--after--><script src="example.js"></script>
27+
```
28+
29+
Suppose `example.js` is a bundled version of something like this:
30+
31+
```js
32+
import inspect from 'unist-util-inspect';
33+
import fromDOM from 'hast-util-from-dom';
34+
35+
const hast = fromDOM(document.documentElement.parentNode);
36+
37+
console.log(inspect(hast));
38+
```
39+
40+
Viewing `example.html` should yield the following in the console:
41+
42+
```text
43+
root[2]
44+
├─ doctype [name="html"]
45+
└─ element[2] [tagName="html"]
46+
├─ element[1] [tagName="head"]
47+
│ └─ element[1] [tagName="title"]
48+
│ └─ text: "Hello!"
49+
└─ element[1] [tagName="body"]
50+
└─ element[3] [tagName="h1"][properties={"id":"world"}]
51+
├─ text: "World!"
52+
├─ comment: "after"
53+
└─ element[0] [tagName="script"][properties={"src":"example.js"}]
54+
```
55+
56+
## API
57+
58+
### `fromDOM(node)`
59+
60+
Transform a DOM `Node` to a [HAST Node][node].
61+
62+
This works in a similar way to the `parse5` version except that it works directly from the DOM rather than a string of HTML. Consequently, it does not maintain location infomation.
63+
64+
## License
65+
66+
[ISC][license] © [Keith McKnight][author]
67+
68+
<!-- Definitions -->
69+
70+
[yarn]: https://yarnpkg.com/lang/en/docs/install
71+
72+
[npm]: https://docs.npmjs.com/cli/install
73+
74+
[license]: LICENSE
75+
76+
[author]: https://keith.mcknig.ht
77+
78+
[hast]: https://github.com/syntax-tree/hast
79+
80+
[hast-util-from-parse5]: https://github.com/syntax-tree/hast-util-from-parse5
81+
82+
[node]: https://github.com/syntax-tree/hast#ast
83+
84+
[vfile]: https://github.com/vfile/vfile

‎package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "hast-util-from-dom",
3+
"version": "1.0.0-beta.0",
4+
"description": "Transform a DOM tree to HAST",
5+
"main": "dist/hast-util-from-dom.cjs.js",
6+
"module": "dist/hast-util-from-dom.esm.js",
7+
"repository": "https://github.com/kmck/hast-util-from/dom/issues",
8+
"author": "Keith McKnight <[email protected]> (https://keith.mcknig.ht)",
9+
"license": "ISC",
10+
"scripts": {
11+
"build": "rollup -c",
12+
"lint": "eslint .",
13+
"jest": "jest",
14+
"jest:dev": "jest --watchAll",
15+
"test": "eslint . && jest"
16+
},
17+
"devDependencies": {
18+
"babel-core": "^6.26.3",
19+
"babel-eslint": "^8.2.3",
20+
"babel-jest": "^23.0.1",
21+
"babel-preset-env": "^1.7.0",
22+
"eslint": "^4.19.1",
23+
"eslint-config-airbnb-base": "^12.1.0",
24+
"eslint-plugin-import": "^2.12.0",
25+
"glob": "^7.1.2",
26+
"jest-cli": "^23.1.0",
27+
"jsdom": "^11.11.0",
28+
"rollup": "^0.59.4"
29+
},
30+
"files": [
31+
"dist"
32+
],
33+
"dependencies": {
34+
"hastscript": "^3.1.0"
35+
}
36+
}

‎rollup.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pkg from './package.json';
2+
3+
export default {
4+
input: 'src/index.js',
5+
output: [
6+
{ file: pkg.main, format: 'cjs' },
7+
{ file: pkg.module, format: 'es' },
8+
],
9+
external: ['hastscript'],
10+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!DOCTYPE>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"type": "root",
3+
"children": [
4+
{
5+
"type": "doctype",
6+
"name": "",
7+
"public": null,
8+
"system": null
9+
},
10+
{
11+
"type": "element",
12+
"tagName": "html",
13+
"properties": {},
14+
"children": [
15+
{
16+
"type": "element",
17+
"tagName": "head",
18+
"properties": {},
19+
"children": []
20+
},
21+
{
22+
"type": "element",
23+
"tagName": "body",
24+
"properties": {},
25+
"children": []
26+
}
27+
]
28+
}
29+
]
30+
}

0 commit comments

Comments
 (0)