Skip to content

Commit

Permalink
Merge branch 'master' into greenkeeper/initial
Browse files Browse the repository at this point in the history
  • Loading branch information
gregswindle committed Jul 19, 2017
2 parents 61dd01d + fd9bb07 commit c729310
Show file tree
Hide file tree
Showing 7 changed files with 1,002 additions and 189 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
language: node_js
node_js:
- 8
- 7
Expand Down
82 changes: 48 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
# `markdown-decorator`

[![Greenkeeper badge](https://badges.greenkeeper.io/gregswindle/markdown-decorator.svg)](https://greenkeeper.io/)
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Codacy Badge][codacy-image]][codacy-url] [![Coverage percentage][coveralls-image]][coveralls-url] [![License][license-image]][license-url]

[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url] [![License][license-image]][license-url]
> A lightweight utility for inserting text into markdown files and templates.
## 1. Table of contents

- [1. Table of contents](#1-table-of-contents)
- [2. Installation](#2-installation)
- [3. Usage](#3-usage)
- [3.1. Pass a markdown string to its constructor](#31-pass-a-markdown-string-to-its-constructor)
- [3.2. Insert your product's (semantic) version](#32-insert-your-products-semantic-version)
- [3.3. Generate a table of contents](#33-generate-a-table-of-contents)
- [3.4. Method chaining](#34-method-chaining)
- [3.5. Templates](#35-templates)
- [3.6. Save the markdown to file](#36-save-the-markdown-to-file)
- [4. Version and CHANGELOG](#4-version-and-changelog)
## Table of contents

<!-- toc -->

- [1. Installation](#1-installation)
- [2. Usage](#2-usage)
* [2.1. Pass a markdown string to its constructor](#21-pass-a-markdown-string-to-its-constructor)
* [2.2. Insert your product's (semantic) version](#22-insert-your-products-semantic-version)
* [2.3. Generate a table of contents](#23-generate-a-table-of-contents)
* [2.4. Method chaining](#24-method-chaining)
* [2.5. Templates](#25-templates)
* [2.6. Insert a string between delimiters](#26-insert-a-string-between-delimiters)
- [3. Version and CHANGELOG](#3-version-and-changelog)
- [4. Contributing](#4-contributing)
- [5. License](#5-license)

<!-- tocstop -->

<!-- tocend -->


## 2. Installation
## 1. Installation

```sh
$ npm install --save markdown-decorator
```

## 3. Usage
## 2. Usage

### 3.1. Pass a markdown string to its constructor
### 2.1. Pass a markdown string to its constructor

```js
const MarkdownDecorator = require('markdown-decorator')
Expand All @@ -39,7 +44,7 @@ const decorator = new MarkdownDecorator(fs.readFileSync('README.md'))

```

### 3.2. Insert your product's (semantic) version
### 2.2. Insert your product's (semantic) version

```text
## Version and CHANGELOG
Expand All @@ -65,13 +70,12 @@ Inserts the semver:
Please read the [CHANGELOG][changelog-url] for details.
```

### 3.3. Generate a table of contents
### 2.3. Generate a table of contents

Use `toc` comment tags as delimiters:

```text
## Table of contents
<!-- toc -->
<!-- tocend -->
```
Expand All @@ -80,7 +84,7 @@ Use `toc` comment tags as delimiters:
decorator.toc(markdown)
```

### 3.4. Method chaining
### 2.4. Method chaining

```js
const md = decorator
Expand All @@ -89,7 +93,7 @@ const md = decorator
.toString()
```

### 3.5. Templates
### 2.5. Templates

```text
<%= header %>
Expand All @@ -112,23 +116,26 @@ const md = decorator.decorate({
})
```

### 3.6. Save the markdown to file
### 2.6. Insert a string between delimiters

```text
# `<!-- title --><!-- titleend -->`
```

```js
decorator.decorate({
version: '1.0.0'
template,
header: '# TEMPLATE HEADER',
body: decorator.markdown,
footer: 'TEMPLATE FOOTER'
}).save('NOTICE.md')
decorator.insert('markdown-decorator', {
open: 'title',
close: 'titleend'
})
decorator.markdown
// => # `<!-- title -->markdown-decorator<!-- titleend -->`
```

## 4. Version and CHANGELOG
## 3. Version and CHANGELOG

`markdown-decorator` is at <!-- semver -->[`v1.0.0`](./CHANGELOG.md)<!-- semverend -->. Please see the [CHANGELOG](./CHANGELOG.md) for details.

## 7. Contributing
## 4. Contributing

[![PRs Welcome][makeapullrequest-image]][makeapullrequest-url] We welcome contributors and pull requests. Check out the guidelines for

Expand All @@ -140,11 +147,18 @@ Contributions are stories with a beginning, a middle, and an end, all told throu
* [Peruse open issues][issues-url] or
* [Open a new pull request (PR)][pr-url]

## 6. License
## 5. License

[![License][license-image]][license-url] © [Greg Swindle](https://github.com/gregswindle)

[code-of-conduct-url]: ./.gihub/CODE_OF_CONDUCT.md
---

[![Greenkeeper badge](https://badges.greenkeeper.io/gregswindle/markdown-decorator.svg)](https://greenkeeper.io/)


[codacy-image]: https://api.codacy.com/project/badge/Grade/be8fc7f9a20f4e178c4fa067a4aad7c7
[codacy-url]: https://www.codacy.com/app/greg_7/markdown-decorator?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=gregswindle/markdown-decorator&amp;utm_campaign=Badge_Grade
[code-of-conduct-url]: ./.github/CODE_OF_CONDUCT.md
[coveralls-image]: https://coveralls.io/repos/gregswindle/markdown-decorator/badge.svg
[coveralls-url]: https://coveralls.io/r/gregswindle/markdown-decorator
[daviddm-image]: https://david-dm.org/gregswindle/markdown-decorator.svg?theme=shields.io
Expand Down
10 changes: 0 additions & 10 deletions lib/__tests__/MarkdownDecorator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,4 @@ describe('MarkdownDecorator', function () {
expect(md).toContain('---')
expect(md).toContain('TEMPLATE FOOTER')
})

it('saves the markdown', () => {
fs.writeFileSync = jest.fn(() => true)
decorator.decorate({
version: '1.0.0'
}).save('mock.md')
expect(fs.writeFileSync.mock.calls.length).toBe(1)

fs.writeFileSync.mockClear()
})
})
24 changes: 0 additions & 24 deletions lib/index.js

This file was deleted.

68 changes: 32 additions & 36 deletions lib/markdown-decorator.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,64 @@
'use strict';
const _ = require('lodash');
const fs = require('fs');
const markdownToc = require('markdown-toc');

/* eslint security/detect-non-literal-regexp: "warn" */
'use strict'
const _ = require('lodash')
const markdownToc = require('markdown-toc')

const regexFactory = options => {
const regex = new RegExp(`${options.open}(.*)${options.close}`, 'igm');
return regex;
};
const regex = new RegExp(`${options.open}(.*)${options.close}`, 'igm')
return regex
}

const optionsFactory = options => {
return {
open: `<!-- ${options.open} -->`,
close: `<!-- ${options.close} -->`
};
};
}
}

const replacementFactory = (text, replacement, options) => {
const regex = regexFactory(options);
return text.replace(regex, `${options.open}${replacement}${options.close}`);
};
const regex = regexFactory(options)
return text.replace(regex, `${options.open}${replacement}${options.close}`)
}

/**
* MarkdownDecorator inserts markdown text between configurable delimiters and
* formats output based on lodash erb templates.
*/
class MarkdownDecorator {
constructor(markdown) {
this.markdown = markdown.toString();
constructor (markdown) {
this.markdown = markdown.toString()
}

decorate(options) {
this.semver(options.version).toc();
return this;
decorate (options) {
this.semver(options.version).toc()
return this
}

save(destPath) {
fs.writeFileSync(destPath, this.markdown, {
encoding: 'utf8'
});
return this;
insert (text, options) {
const opts = optionsFactory(options)
this.markdown = replacementFactory(this.markdown, text, opts)
return this
}

semver(version) {
const opts = optionsFactory({
semver (version) {
return this.insert(version, {
open: 'semver',
close: 'semverend'
});
this.markdown = replacementFactory(this.markdown, version, opts);
return this;
})
}

toc() {
this.markdown = markdownToc.insert(this.markdown);
return this;
toc () {
this.markdown = markdownToc.insert(this.markdown)
return this
}

toString(options) {
const opts = options || {};
toString (options) {
const opts = options || {}
if (opts.template) {
return _.template(opts.template)(options);
return _.template(opts.template)(options)
}
return this.markdown;
return this.markdown
}
}

module.exports = MarkdownDecorator;
module.exports = MarkdownDecorator

0 comments on commit c729310

Please sign in to comment.