Skip to content

Commit

Permalink
feat(index): tree fromString/toString, close #242
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrum committed Oct 10, 2019
1 parent 958f480 commit 2647d53
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,50 @@ function PostHTML (plugins) {
*/
this.messages = []

/**
* Tree method parsing string inside plugins.
*
* @memberof tree
* @type {Function} fromString
*
* @example
* ```js
* export default function plugin (options = {}) {
* return function (tree) {
* tree.match({ tag: 'include' }, function(node) {
* node.content = tree.fromString(fs.readFileSync(node.attr.src))
* })
*
* return tree
* }
* }
* ```
*/
this.fromString = parser

/**
* Tree method rendering tree to string inside plugins.
*
* @memberof tree
* @type {Function} toString
*
* @example
* ```js
* export default function plugin (options = {}) {
* return function (tree) {
* function (tree) {
* var outherTree = ['\n', {tag: 'div', content: ['1']}, '\n\t', {tag: 'div', content: ['2']}, '\n'];
* var htmlWitchoutSpaceless = tree.toString(outherTree).replace(/[\n|\t]/g, '');
* return tree.fromString(htmlWitchoutSpaceless)
* }
*
* return tree
* }
* }
* ```
*/
this.toString = render

// extend api methods
Api.call(this)
}
Expand Down Expand Up @@ -102,8 +146,8 @@ PostHTML.prototype.process = function (tree, options) {
*/
options = this.options = options || {}

if (options.parser) parser = options.parser
if (options.render) render = options.render
if (options.parser) parser = this.fromString = options.parser
if (options.render) render = this.toString = options.render

tree = options.skipParse
? tree || []
Expand Down

0 comments on commit 2647d53

Please sign in to comment.