Comparing version 0.3.1 to 0.4.0
14
index.js
@@ -100,3 +100,3 @@ var posthtml = require('posthtml') | ||
case 'a': | ||
txt = getTextInNode(node) || node.attrs.href | ||
txt = getTextInNode(node) || (node.attrs && node.attrs.href) | ||
if (txt) { | ||
@@ -108,3 +108,3 @@ text.push(`[${txt}](${node.attrs.href})`) | ||
case 'img': | ||
if (node.attrs.src) { | ||
if (node.attrs && node.attrs.src) { | ||
text.push(` ![${(node.attrs.title || node.attrs.alt || node.attrs.src).trim()}](${node.attrs.src}) `) | ||
@@ -115,2 +115,4 @@ } | ||
default: | ||
text.push(getTextInNode(node)) | ||
node.content = [] | ||
@@ -173,3 +175,3 @@ } | ||
case 'img': | ||
if (node.attrs.src) { | ||
if (node.attrs && node.attrs.src) { | ||
md.push(`![${(node.attrs.title || node.attrs.alt || node.attrs.src).trim()}](${node.attrs.src})`) | ||
@@ -185,3 +187,3 @@ } | ||
case 'ul': | ||
text = node.content.filter(function (node) { | ||
text = node.content && node.content.filter(function (node) { | ||
return typeof node == 'object' && node.tag == 'li' | ||
@@ -191,3 +193,5 @@ }).map(function (node, index) { | ||
}).join('\n') | ||
md.push(`\n\n${text}`) | ||
if (text) { | ||
md.push(`\n\n${text}`) | ||
} | ||
node.content = [] | ||
@@ -194,0 +198,0 @@ break |
{ | ||
"name": "h2m", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Transform HTML to markdown base on posthtml.", | ||
@@ -8,5 +8,9 @@ "main": "index.js", | ||
"test": "mocha && npm run coverage", | ||
"istanbul": "istanbul cover ./node_modules/mocha/bin/_mocha", | ||
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", | ||
"build": "browserify browser.js -o dist/h2m.js" | ||
}, | ||
"bin": { | ||
"h2m": "bin/h2m-cli.js" | ||
}, | ||
"keywords": [ | ||
@@ -28,4 +32,6 @@ "posthtml", | ||
"dependencies": { | ||
"commander": "^2.9.0", | ||
"posthtml": "^0.8.1", | ||
"posthtml-parser": "^0.1.1" | ||
"posthtml-parser": "^0.1.1", | ||
"request": "^2.67.0" | ||
}, | ||
@@ -32,0 +38,0 @@ "devDependencies": { |
@@ -29,2 +29,47 @@ # h2m | ||
## CLI | ||
### install | ||
```bash | ||
$ npm install h2m -g | ||
``` | ||
### usage | ||
``` | ||
$h2m -h | ||
Usage: h2m [options] <file> | ||
Options: | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
``` | ||
Convert a local file: | ||
```bash | ||
$ h2m index.html | ||
converting HTML to Markdown | ||
made by [@island205](https://github.com/island205) | ||
Can't be convert? welcome to submit an [issue](https://github.com/island205/h2m/issues/new). | ||
``` | ||
Convert an online url: | ||
```bash | ||
$ h2m https://baidu.com | ||
``` | ||
Save result: | ||
```bash | ||
$ h2m https://google.com > google.md | ||
``` | ||
## Support | ||
@@ -31,0 +76,0 @@ |
@@ -32,2 +32,3 @@ var h2m = require('../index') | ||
expect(h2m('<a href="http://island205.github.io/h2m/"></a>')).to.equal('[http://island205.github.io/h2m/](http://island205.github.io/h2m/)') | ||
expect(h2m('<a href="">h2m</a>')).to.equal('') | ||
}) | ||
@@ -39,2 +40,3 @@ | ||
expect(h2m('<img src="https://raw.githubusercontent.com/island205/h2m/master/images/online-converter.png" />')).to.equal('![https://raw.githubusercontent.com/island205/h2m/master/images/online-converter.png](https://raw.githubusercontent.com/island205/h2m/master/images/online-converter.png)') | ||
expect(h2m('<img />')).to.equal('') | ||
}) | ||
@@ -48,2 +50,3 @@ | ||
expect(h2m(fixture('ul.html'))).to.equal(fixture('ul.md')) | ||
expect(h2m('<ul></ul>')).to.equal('') | ||
}) | ||
@@ -87,2 +90,24 @@ | ||
it('should parse nested inline tag', function () { | ||
expect(h2m('<h1><a href="http://island205.github.io/h2m/">h2m</a></h1>')).to.equal('# [h2m](http://island205.github.io/h2m/)') | ||
expect(h2m('<h1><em></em>h2m</h1>')).to.equal('# h2m') | ||
expect(h2m('<h1><strong></strong>h2m</h1>')).to.equal('# h2m') | ||
expect(h2m('<h1><code></code>h2m</h1>')).to.equal('# h2m') | ||
expect(h2m('<h1><a></a>h2m</h1>')).to.equal('# h2m') | ||
expect(h2m('<h1><img />h2m</h1>')).to.equal('# h2m') | ||
expect(h2m('<h1><a href="http://island205.github.io/h2m/"></a></h1>')).to.equal('# [http://island205.github.io/h2m/](http://island205.github.io/h2m/)') | ||
expect(h2m('<a href="http://island205.github.io/h2m/">h2<br/>m</a>')).to.equal('[h2\nm](http://island205.github.io/h2m/)') | ||
expect(h2m('<a href="http://island205.github.io/h2m/">h<em>2</em>m</a>')).to.equal('[h*2*m](http://island205.github.io/h2m/)') | ||
expect(h2m('<h2>h<strong>2</strong>m</h2>')).to.equal('## h**2**m') | ||
expect(h2m('<h2><code>h2m</code></h2>')).to.equal('## `h2m`') | ||
expect(h2m('<a href="http://island205.github.io/h2m/"><img title="h2m" src="https://raw.githubusercontent.com/island205/h2m/master/images/online-converter.png" /></a>')).to.equal('[![h2m](https://raw.githubusercontent.com/island205/h2m/master/images/online-converter.png)](http://island205.github.io/h2m/)') | ||
expect(h2m('<a href="http://island205.github.io/h2m/"><img alt="h2m" src="https://raw.githubusercontent.com/island205/h2m/master/images/online-converter.png" /></a>')).to.equal('[![h2m](https://raw.githubusercontent.com/island205/h2m/master/images/online-converter.png)](http://island205.github.io/h2m/)') | ||
expect(h2m('<a href="http://island205.github.io/h2m/"><img src="https://raw.githubusercontent.com/island205/h2m/master/images/online-converter.png" /></a>')).to.equal('[![https://raw.githubusercontent.com/island205/h2m/master/images/online-converter.png](https://raw.githubusercontent.com/island205/h2m/master/images/online-converter.png)](http://island205.github.io/h2m/)') | ||
}) | ||
it('should ignore other unsupport tag', function () { | ||
expect(h2m('<article>code</article>')).to.equal('code') | ||
expect(h2m('<h2>co<i>d</i>e</h2>')).to.equal('## code') | ||
}) | ||
}) |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
1010482
29
8984
95
4
7
9
+ Addedcommander@^2.9.0
+ Addedrequest@^2.67.0
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcommander@2.20.3(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.15.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)