Skip to content

Commit 780e53f

Browse files
committed
initial working version
0 parents  commit 780e53f

File tree

21 files changed

+400
-0
lines changed

21 files changed

+400
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
node_modules
3+
tmp
4+
*.log*

LICENSE.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2016 Titus Wormer <[email protected]>, Seth Vincent <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# hast-util-to-mdast
2+
3+
Convert [HAST trees](https://github.com/syntax-tree/hast) to [MDAST trees](https://github.com/syntax-tree/mdast).
4+
5+
## About
6+
7+
Based on the code in [mdast-util-to-hast](https://github.com/wooorm/mdast-util-to-hast), this module provides a way to transform an HTML abstract syntax tree into a markdown abstract syntax tree.
8+
9+
Used with [rehype](https://npmjs.com/rehype) and [remark](https://npmjs.com/remark), this allows you to convert HTML into markdown.
10+
11+
### Work in progress
12+
13+
So far this only transforms a few HTML elements, and there's more work to do. To see what's been completed so far look at [the handlers directory](/handlers).
14+
15+
## Install
16+
17+
```sh
18+
npm install --save hast-util-to-mdast
19+
```
20+
21+
## Usage
22+
23+
```js
24+
var unified = require('unified')
25+
var parse = require('rehype-parse')
26+
var stringify = require('remark-stringify')
27+
28+
var toMDAST = require('hast-util-to-mdast')
29+
30+
var result = unified()
31+
.use(parse)
32+
.use(function () { return toMDAST })
33+
.use(stringify)
34+
.process(html, { fragment: true })
35+
36+
console.log(result.contents)
37+
```
38+
39+
40+
## License
41+
42+
[MIT](LICENSE.md)

all.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = all
2+
3+
var trim = require('trim')
4+
var one = require('./one')
5+
6+
function all (h, parent) {
7+
var nodes = parent.children || []
8+
var length = nodes.length
9+
var values = []
10+
var index = -1
11+
var result
12+
var head
13+
14+
while (++index < length) {
15+
result = one(h, nodes[index], parent)
16+
if (result) {
17+
values = values.concat(result)
18+
}
19+
}
20+
21+
return values
22+
}

handlers/heading.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = function (depth) {
2+
return heading(depth)
3+
}
4+
5+
var u = require('unist-builder')
6+
var all = require('../all')
7+
8+
function heading (depth) {
9+
return function (h, node) {
10+
return h(node, 'heading', { depth: depth }, all(h, node))
11+
}
12+
}

handlers/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
heading: require('./heading'),
3+
text: require('./text'),
4+
root: require('./root'),
5+
strong: require('./strong'),
6+
p: require('./p')
7+
}

handlers/p.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = p
2+
3+
var u = require('unist-builder')
4+
var all = require('../all')
5+
6+
7+
function p (h, node) {
8+
return h(node, 'paragraph', all(h, node))
9+
}

handlers/root.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = root
2+
3+
var u = require('unist-builder')
4+
var all = require('../all')
5+
6+
function root (h, node) {
7+
return h(node, 'root', all(h, node))
8+
}

handlers/strong.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = strong
2+
3+
var u = require('unist-builder')
4+
var all = require('../all')
5+
6+
function strong (h, node) {
7+
return h(node, 'strong', all(h, node))
8+
}

handlers/text.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var u = require('unist-builder')
2+
var trimLines = require('trim-lines')
3+
4+
module.exports = function text (h, node) {
5+
if (node.value.match(/(\n+)/g)) {
6+
return null
7+
}
8+
9+
return h.augment(node, u('text', trimLines(node.value)))
10+
}

0 commit comments

Comments
 (0)