From 6a06de208ece9c233f45ae6a24718453043c8680 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Sun, 15 May 2016 19:32:35 +0300 Subject: [PATCH] feat: update license section w/o emdedded js --- README.md | 1 + index.js | 40 +++++++++++++++++-- package.json | 3 +- .../local-package-via-heading/input.md | 7 ++++ .../local-package-via-heading/output.md | 11 +++++ .../local-package-via-heading/package.json | 3 ++ 6 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/local-package-via-heading/input.md create mode 100644 test/fixtures/local-package-via-heading/output.md create mode 100644 test/fixtures/local-package-via-heading/package.json diff --git a/README.md b/README.md index 4d1097c..3153155 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ If the package has `peerDependencies`, the installation command will suggest to ## Dependencies [![Dependency status for master](https://img.shields.io/david/zkochan/mos-plugin-installation/master.svg?style=flat-square)](https://david-dm.org/zkochan/mos-plugin-installation/master) - [markdownscript](https://github.com/zkochan/markdownscript): Creates markdown Abstract Syntax Tree +- [mdast-util-to-string](https://github.com/wooorm/mdast-util-to-string): Utility to get the plain text content of a node diff --git a/index.js b/index.js index ae65a49..0fde882 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ 'use strict' +const toString = require('mdast-util-to-string') const m = require('markdownscript') const h2 = m.h2 const code = m.code @@ -18,18 +19,27 @@ const shortCommands = { } module.exports = (mos, md) => { + mos.compile.pre((next, ast, opts) => { + ast.children = updateInstallationSection(ast.children) + return next(ast, opts) + }) + Object.assign(mos.scope, { - installation: opts => [ + installation: compileInstallation, + }) + + function compileInstallation (opts) { + opts = Object.assign({}, md.options, opts || {}) + return [ h2(['Installation']), code({ lang: 'sh', value: createCommand(opts), }), - ], - }) + ] + } function createCommand (opts) { - opts = Object.assign({}, md.options, opts || {}) const commands = opts.useShortAlias ? shortCommands : fullCommands if (md.pkg.private || md.pkg.license === 'private') { return [ @@ -43,6 +53,28 @@ module.exports = (mos, md) => { } return `npm ${commands.install} ${md.pkg.preferGlobal ? commands.global : commands.save} ${installedPkgs}` } + + function updateInstallationSection (children) { + if (!children.length) { + return [] + } + const child = children.shift() + if (child.type === 'heading' && toString(child).match(/^installation$/i)) { + return compileInstallation().concat(removeSection(children)) + } + return [child].concat(updateInstallationSection(children)) + } + + function removeSection (children) { + if (!children.length) { + return [] + } + const child = children.shift() + if (~['heading', 'markdownScript', 'thematicBreak'].indexOf(child.type)) { + return [child].concat(children) + } + return removeSection(children) + } } module.exports.attributes = { diff --git a/package.json b/package.json index 82b0c98..989defb 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,8 @@ }, "homepage": "https://github.com/zkochan/mos-plugin-installation#readme", "dependencies": { - "markdownscript": "^1.1.0" + "markdownscript": "^1.1.0", + "mdast-util-to-string": "^1.0.1" }, "devDependencies": { "chai": "^3.4.1", diff --git a/test/fixtures/local-package-via-heading/input.md b/test/fixtures/local-package-via-heading/input.md new file mode 100644 index 0000000..0b7256b --- /dev/null +++ b/test/fixtures/local-package-via-heading/input.md @@ -0,0 +1,7 @@ +# foo + +bla bla + +## Installation + +## bar diff --git a/test/fixtures/local-package-via-heading/output.md b/test/fixtures/local-package-via-heading/output.md new file mode 100644 index 0000000..95facc0 --- /dev/null +++ b/test/fixtures/local-package-via-heading/output.md @@ -0,0 +1,11 @@ +# foo + +bla bla + +## Installation + +```sh +npm install --save foo +``` + +## bar diff --git a/test/fixtures/local-package-via-heading/package.json b/test/fixtures/local-package-via-heading/package.json new file mode 100644 index 0000000..bde99de --- /dev/null +++ b/test/fixtures/local-package-via-heading/package.json @@ -0,0 +1,3 @@ +{ + "name": "foo" +}