-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(scm): merge pull request feat/markdown-decorator
docs(readme): fix contributing url
- Loading branch information
Showing
7 changed files
with
112 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
language: node_js | ||
node_js: | ||
- 8 | ||
- 7 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.