Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
feat: update license section w/o emdedded js
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed May 15, 2016
1 parent 05cfcea commit 6a06de2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ If the package has `peerDependencies`, the installation command will suggest to
## <a name="dependencies">Dependencies</a> [![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

<!--/@-->

Expand Down
40 changes: 36 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 [
Expand All @@ -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 = {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/local-package-via-heading/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# foo

bla bla

## Installation

## bar
11 changes: 11 additions & 0 deletions test/fixtures/local-package-via-heading/output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# foo

bla bla

## Installation

```sh
npm install --save foo
```

## bar
3 changes: 3 additions & 0 deletions test/fixtures/local-package-via-heading/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "foo"
}

0 comments on commit 6a06de2

Please sign in to comment.