Skip to content

Commit 2647d53

Browse files
committed
feat(index): tree fromString/toString, close #242
1 parent 958f480 commit 2647d53

1 file changed

Lines changed: 46 additions & 2 deletions

File tree

lib/index.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,50 @@ function PostHTML (plugins) {
5151
*/
5252
this.messages = []
5353

54+
/**
55+
* Tree method parsing string inside plugins.
56+
*
57+
* @memberof tree
58+
* @type {Function} fromString
59+
*
60+
* @example
61+
* ```js
62+
* export default function plugin (options = {}) {
63+
* return function (tree) {
64+
* tree.match({ tag: 'include' }, function(node) {
65+
* node.content = tree.fromString(fs.readFileSync(node.attr.src))
66+
* })
67+
*
68+
* return tree
69+
* }
70+
* }
71+
* ```
72+
*/
73+
this.fromString = parser
74+
75+
/**
76+
* Tree method rendering tree to string inside plugins.
77+
*
78+
* @memberof tree
79+
* @type {Function} toString
80+
*
81+
* @example
82+
* ```js
83+
* export default function plugin (options = {}) {
84+
* return function (tree) {
85+
* function (tree) {
86+
* var outherTree = ['\n', {tag: 'div', content: ['1']}, '\n\t', {tag: 'div', content: ['2']}, '\n'];
87+
* var htmlWitchoutSpaceless = tree.toString(outherTree).replace(/[\n|\t]/g, '');
88+
* return tree.fromString(htmlWitchoutSpaceless)
89+
* }
90+
*
91+
* return tree
92+
* }
93+
* }
94+
* ```
95+
*/
96+
this.toString = render
97+
5498
// extend api methods
5599
Api.call(this)
56100
}
@@ -102,8 +146,8 @@ PostHTML.prototype.process = function (tree, options) {
102146
*/
103147
options = this.options = options || {}
104148

105-
if (options.parser) parser = options.parser
106-
if (options.render) render = options.render
149+
if (options.parser) parser = this.fromString = options.parser
150+
if (options.render) render = this.toString = options.render
107151

108152
tree = options.skipParse
109153
? tree || []

0 commit comments

Comments
 (0)