Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 73 additions & 9 deletions bin/marky-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,80 @@
var fs = require('fs')
var path = require('path')
var marky = require('..')
var yargs = require('yargs')
var omit = require('lodash.omit')

if (process.argv.length < 3) {
console.log('Usage:\n\nmarky-markdown some.md > some.html')
process.exit()
var parser = yargs
.usage([
"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"'
)
.option('sanitize', {
default: true,
describe: 'remove script tags and stuff',
type: 'boolean'
})
.option('linkify', {
default: true,
describe: 'turn orphan URLs into hyperlinks',
type: 'boolean'
})
.option('highlightSyntax', {
alias: 'highlight',
default: true,
describe: 'run highlights on fenced code blocks',
type: 'boolean'
})
.option('prefixHeadingIds', {
alias: 'prefix',
default: true,
describe: 'prevent DOM id collisions',
type: 'boolean'
})
.option('serveImagesWithCDN', {
alias: 'cdn',
default: false,
describe: "use npm's CDN to proxy images over HTTPS",
type: 'boolean'
})
.demand(1)
.wrap(Math.min(125, yargs.terminalWidth()))

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

fs.readFile(filePath, function (err, data) {
if (err) {
cb(err)
} else {
cb(null, [data.toString(), options])
}
})
}

var filePath = path.resolve(process.cwd(), process.argv[2])
// 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())
process.stdout.write($.html())
})
module.exports = function (rawArgv, cb) {
var argv = parser.parse(rawArgv)
argvToMarkyArgs(argv, cb)
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +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"
"similarity": "^1.0.1",
"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