From 04d9675477655facf521e60bb5484a33630e0b59 Mon Sep 17 00:00:00 2001 From: Revin Guillen Date: Thu, 3 Nov 2016 16:35:31 -0700 Subject: [PATCH] Add new getParser public API --- index.js | 26 ++++++++++++++++---------- lib/render.js | 16 ++++++++++------ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 7a41662..5a9d477 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,17 @@ var defaults = require('lodash.defaults') var render = require('./lib/render') var sanitize = require('./lib/sanitize') +var defaultOptions = { + sanitize: true, + linkify: true, + highlightSyntax: true, + prefixHeadingIds: true, + enableHeadingLinkIcons: true, + serveImagesWithCDN: false, + debug: false, + package: null +} + var marky = module.exports = function (markdown, options) { var html @@ -10,16 +21,7 @@ var marky = module.exports = function (markdown, options) { } options = options || {} - defaults(options, { - sanitize: true, - linkify: true, - highlightSyntax: true, - prefixHeadingIds: true, - enableHeadingLinkIcons: true, - serveImagesWithCDN: false, - debug: false, - package: null - }) + defaults(options, defaultOptions) var log = function (msg) { if (options.debug) { @@ -43,3 +45,7 @@ var marky = module.exports = function (markdown, options) { marky.parsePackageDescription = function (description) { return sanitize(render.renderPackageDescription(description)) } + +marky.getParser = function (options) { + return render.getParser(defaults(options || {}, defaultOptions)) +} diff --git a/lib/render.js b/lib/render.js index ef09457..37a713b 100644 --- a/lib/render.js +++ b/lib/render.js @@ -46,7 +46,11 @@ if (typeof process.browser === 'undefined') { cleanup(highlighter.registry.grammars) } -var render = module.exports = function (html, options) { +var render = module.exports = function (markdown, options) { + return render.getParser(options).render(markdown) +} + +render.getParser = function (options) { var mdOptions = { html: true, langPrefix: 'highlight ', @@ -79,7 +83,11 @@ var render = module.exports = function (html, options) { if (options.highlightSyntax) parser.use(codeWrap) if (options.serveImagesWithCDN) parser.use(cdnImages, {package: options.package}) - return githubLinkify(parser).render(html) + return githubLinkify(parser) +} + +render.renderPackageDescription = function (description) { + return MD({html: true}).renderInline(description) } var mappings = { @@ -108,7 +116,3 @@ function scopeNameFromLang (highlighter, lang) { return name } - -render.renderPackageDescription = function (description) { - return MD({html: true}).renderInline(description) -}