Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
rebase, update dep versions, make code easier to test, add some test …
Browse files Browse the repository at this point in the history
…coverage
  • Loading branch information
knksmith57 committed Mar 20, 2016
1 parent af62076 commit 76b6cdf
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 20 deletions.
51 changes: 33 additions & 18 deletions bin/marky-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ var fs = require('fs')
var path = require('path')
var marky = require('..')
var yargs = require('yargs')
var omit = require('lodash').omit
var omit = require('lodash.omit')

var argv = yargs
var parser = yargs
.usage([
'npm\'s markdown parser',
'',
'Usage: $0 [options] some.md > some.html'
"npm's markdown parser",
'',
'Usage: $0 [options] some.md > some.html'
].join('\n'))
.version(function () {
return require('../package.json').version
})
.example(
'$0 --no-sanitize some.md > some.html',
'Parse "some.md" without sanitizing and redirect result to "some.html"'
)
)
.example(
'$0 --no-highlight README.md > index.html',
'Parse "README.md" without highlighting fenced code blocks and redirect result to "index.html"'
)
'$0 --no-highlight README.md > index.html',
'Parse "README.md" without highlighting fenced code blocks and redirect result to "index.html"'
)
.option('sanitize', {
default: true,
describe: 'remove script tags and stuff',
Expand All @@ -47,20 +47,35 @@ var argv = yargs
.option('serveImagesWithCDN', {
alias: 'cdn',
default: false,
describe: 'use npm\'s CDN to proxy images over HTTPS',
describe: "use npm's CDN to proxy images over HTTPS",
type: 'boolean'
})
.demand(1)
.wrap(Math.min(125, yargs.terminalWidth()))
.argv

function argvToMarkyArgs (argv, cb) {
var options = omit(argv, ['_', 'version', 'highlight', 'prefix', 'cdn', '$0'])
var filePath = path.resolve(process.cwd(), argv._[0])

var options = omit(argv, ['_', 'version', 'highlight', 'prefix', 'cdn', '$0']);
fs.readFile(filePath, function (err, data) {
if (err) {
cb(err)
} else {
cb(null, [data.toString(), options])
}
})
}

var filePath = path.resolve(process.cwd(), argv._[0])
// invoked via CLI
if (require.main === module) {
argvToMarkyArgs(parser.argv, function (err, markyArgs) {
if (err) throw err
var $ = marky.apply(null, markyArgs)
process.stdout.write($.html())
})
}

fs.readFile(filePath, function (err, data) {
if (err) throw err
var $ = marky(data.toString(), options)
process.stdout.write($.html())
})
module.exports = function (rawArgv, cb) {
var argv = parser.parse(rawArgv)
argvToMarkyArgs(argv, cb)
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,22 @@
"language-stylus": "^0.5.2",
"lodash.assign": "^4.0.2",
"lodash.defaults": "^4.0.1",
"lodash.omit": "^4.1.0",
"lodash.pickby": "^4.2.1",
"markdown-it": "^5.1.0",
"markdown-it-lazy-headers": "^0.1.3",
"markdown-it-emoji": "^1.1.0",
"markdown-it-expand-tabs": "^1.0.7",
"markdown-it-lazy-headers": "^0.1.3",
"property-ttl": "^1.0.0",
"sanitize-html": "^1.6.1",
"similarity": "^1.0.1",
"yargs": "^3.32.0"
"yargs": "^4.3.1"
},
"devDependencies": {
"glob": "^7.0.0",
"intercept-stdout": "^0.1.2",
"mocha": "^2.0.1",
"mock-fs": "^3.8.0",
"standard": "^6.0.4",
"standard-format": "^2.1.0"
},
Expand Down
121 changes: 121 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* globals describe, it, before, after */

var assert = require('assert')
var cli = require('../bin/marky-markdown')
var fixtures = require('./fixtures')
var mock = require('mock-fs')

var filename = '/some/file.md'
var fileContents = fixtures.basic

describe('$ marky-markdown', function () {
before(function () {
var mockObj = {}
mockObj[filename] = fileContents
mock(mockObj)
})

after(function () {
mock.restore()
})

describe('exported properly', function () {
it('is specified in package.json', function () {
assert(require('../package.json').bin === './bin/marky-markdown.js')
})

it('is a function', function () {
assert(cli)
assert(typeof cli === 'function')
})
})

describe('handles options', function () {
it("doesn't require any options", function (cb) {
cli([filename], function (err, args) {
assert(!err)
cb()
})
})

it('--sanitize', function (cb) {
cli(['--sanitize', filename], function (err, args) {
assert(!err)
assert(args[1].sanitize)
cb()
})
})

it('--no-sanitize', function (cb) {
cli(['--no-sanitize', filename], function (err, args) {
assert(!err)
assert(!args[1].sanitize)
cb()
})
})

it('--linkify', function (cb) {
cli(['--linkify', filename], function (err, args) {
assert(!err)
assert(args[1].linkify)
cb()
})
})

it('--no-linkify', function (cb) {
cli(['--no-linkify', filename], function (err, args) {
assert(!err)
assert(!args[1].linkify)
cb()
})
})

it('--highlight', function (cb) {
cli(['--highlight', filename], function (err, args) {
assert(!err)
assert(args[1].highlightSyntax)
cb()
})
})

it('--no-highlight', function (cb) {
cli(['--no-highlight', filename], function (err, args) {
assert(!err)
assert(!args[1].highlightSyntax)
cb()
})
})

it('--prefix', function (cb) {
cli(['--prefix', filename], function (err, args) {
assert(!err)
assert(args[1].prefixHeadingIds)
cb()
})
})

it('--no-prefix', function (cb) {
cli(['--no-prefix', filename], function (err, args) {
assert(!err)
assert(!args[1].prefixHeadingIds)
cb()
})
})

it('--cdn', function (cb) {
cli(['--cdn', filename], function (err, args) {
assert(!err)
assert(args[1].serveImagesWithCDN)
cb()
})
})

it('--no-cdn', function (cb) {
cli(['--no-cdn', filename], function (err, args) {
assert(!err)
assert(!args[1].serveImagesWithCDN)
cb()
})
})
})
})
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// the unit test suite lives in these modules
require('./marky')
require('./cli')
require('./markdown')
require('./sanitize')
require('./badges')
Expand Down

0 comments on commit 76b6cdf

Please sign in to comment.