Skip to content

Commit

Permalink
Merge pull request #295 from posthtml/milestone-0.12.0
Browse files Browse the repository at this point in the history
Milestone 0.12.0
  • Loading branch information
Scrum committed Oct 11, 2019
2 parents e012fd8 + b4e064e commit e951b9f
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 78 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [0.12.0](https://github.com/posthtml/posthtml/compare/v0.11.6...v0.12.0) (2019-10-11)


### Features

* **index:** tree fromString/toString, close [#242](https://github.com/posthtml/posthtml/issues/242) ([2647d53](https://github.com/posthtml/posthtml/commit/2647d53f07cbb850004b259b9272946b56fa434a))


### Bug Fixes

* **index:** example for parser/render ([d25743f](https://github.com/posthtml/posthtml/commit/d25743fbc383cdd867159703263a8b7ec5943e7b))
* **index:** forgot change methods name ([72a8640](https://github.com/posthtml/posthtml/commit/72a8640ed0e28898601ea196c4908f55e3082fd0))
* **index:** remove static method because not return class constructor ([7825559](https://github.com/posthtml/posthtml/commit/7825559cdec47d50788633031d56c0c6938b10d7))

### [0.11.6](https://github.com/posthtml/posthtml/compare/v0.11.5...v0.11.6) (2019-08-30)


Expand Down
18 changes: 0 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -722,24 +722,6 @@ In case you want to develop your own plugin, we recommend using [posthtml-plugin
<br />
<a href="https://github.com/voischev">Ivan Voischev</a>
</td>
<td align="center">
<img width="150 height="150"
src="https://avatars.githubusercontent.com/u/982072?v=3&s=150">
<br />
<a href="https://github.com/awinogradov">Anton Winogradov</a>
</td>
<td align="center">
<img width="150 height="150"
src="https://avatars.githubusercontent.com/u/677518?v=3&s=150">
<br />
<a href="https://github.com/zxqfox">Alexej Yaroshevich</a>
</td>
<td align="center">
<img width="150 height="150"
src="https://avatars.githubusercontent.com/u/1813468?v=3&s=150">
<br />
<a href="https://github.com/Yeti-or">Vasiliy</a>
</td>
</tr>
<tbody>
</table>
Expand Down
36 changes: 4 additions & 32 deletions docs/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,13 @@ const ph = posthtml([ plugin() ])
**Kind**: global class
**Requires**: <code>module:api</code>, <code>module:posthtml-parser</code>, <code>module:posthtml-render</code>
**Author**: Ivan Voischev (@voischev),
Anton Winogradov (@awinogradov),
Alexej Yaroshevich (@zxqfox),
Vasiliy (@Yeti-or)
Ivan Demidov (@scrum)

* [PostHTML](#PostHTML)
* [new PostHTML(plugins)](#new_PostHTML_new)
* _instance_
* [.use(plugin)](#PostHTML+use) ⇒ <code>Constructor</code>
* [.process(html, options)](#PostHTML+process) ⇒ <code>Object.&lt;{html: String, tree: PostHTMLTree}&gt;</code> \| <code>Promise.&lt;{html: String, tree: PostHTMLTree}&gt;</code>
* [~options](#PostHTML+process..options) : <code>Object</code>
* _static_
* [.parser](#PostHTML.parser) ⇒ <code>Array</code>
* [.render](#PostHTML.render) ⇒ <code>String</code>
* [.use(plugin)](#PostHTML+use) ⇒ <code>Constructor</code>
* [.process(html, options)](#PostHTML+process) ⇒ <code>Object.&lt;{html: String, tree: PostHTMLTree}&gt;</code> \| <code>Promise.&lt;{html: String, tree: PostHTMLTree}&gt;</code>
* [~options](#PostHTML+process..options) : <code>Object</code>

<a name="new_PostHTML_new"></a>

Expand Down Expand Up @@ -121,28 +115,6 @@ ph.process('<html>..</html>', {}).then((result) => result))
| options.render | <code>function</code> | use custom render, replaces default (posthtml-render) |
| options.skipParse | <code>Boolean</code> | disable parsing |

<a name="PostHTML.parser"></a>

### PostHTML.parser ⇒ <code>Array</code>
**Kind**: static property of [<code>PostHTML</code>](#PostHTML)
**Returns**: <code>Array</code> - tree - PostHTMLTree (JSON)
**Requires**: <code>module:posthtml-parser</code>

| Param | Type | Description |
| --- | --- | --- |
| html | <code>String</code> | Input (HTML) |

<a name="PostHTML.render"></a>

### PostHTML.render ⇒ <code>String</code>
**Kind**: static property of [<code>PostHTML</code>](#PostHTML)
**Returns**: <code>String</code> - html - HTML
**Requires**: <code>module:posthtml-render</code>

| Param | Type | Description |
| --- | --- | --- |
| tree | <code>Array</code> | PostHTMLTree (JSON) |

<a name="version"></a>

## version
Expand Down
46 changes: 46 additions & 0 deletions docs/plugins/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,50 @@ export default (tree) => {
}
```

## Messages
> Tree messages to store and pass metadata between plugins
```js
export default function plugin (options = {}) {
return function (tree) {
tree.messages.push({
type: 'dependency',
file: 'path/to/dependency.html',
from: tree.options.from
})

return tree
}
}
```

## parser
> Tree method parsing string inside plugins.
```js
export default function plugin (options = {}) {
return function (tree) {
tree.match({ tag: 'include' }, function(node) {
node.tag = false;
node.content = tree.parser(fs.readFileSync(node.attr.src))
return node
})
return tree
}
}
```

## render
> Tree method rendering tree to string inside plugins.
```js
export default function plugin (options = {}) {
return function (tree) {
var outherTree = ['\n', {tag: 'div', content: ['1']}, '\n\t', {tag: 'div', content: ['2']}, '\n'];
var htmlWitchoutSpaceless = tree.render(outherTree).replace(/[\n|\t]/g, '');
return tree.parser(htmlWitchoutSpaceless)
}
}
```

[plugin-boilerplate]: https://github.com/posthtml/posthtml-plugin-boilerplate
65 changes: 45 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ var render = require('posthtml-render')

/**
* @author Ivan Voischev (@voischev),
* Anton Winogradov (@awinogradov),
* Alexej Yaroshevich (@zxqfox),
* Vasiliy (@Yeti-or)
* Ivan Demidov (@scrum)
*
* @requires api
* @requires posthtml-parser
Expand Down Expand Up @@ -51,25 +49,52 @@ function PostHTML (plugins) {
*/
this.messages = []

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

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

// extend api methods
Api.call(this)
}

/**
* @requires posthtml-parser
*
* @param {String} html - Input (HTML)
* @returns {Array} tree - PostHTMLTree (JSON)
*/
PostHTML.parser = parser
/**
* @requires posthtml-render
*
* @param {Array} tree - PostHTMLTree (JSON)
* @returns {String} html - HTML
*/
PostHTML.render = render

/**
* @this posthtml
* @param {Function} plugin - A PostHTML plugin
Expand Down Expand Up @@ -117,8 +142,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.parser = options.parser
if (options.render) render = this.render = options.render

tree = options.skipParse
? tree || []
Expand Down
12 changes: 4 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "posthtml",
"version": "0.11.6",
"version": "0.12.0",
"description": "HTML/XML processor",
"keywords": [
"html",
Expand Down Expand Up @@ -45,19 +45,15 @@
"docs:core": "jsdoc2md lib/index.js > docs/core.md",
"release": "standard-version"
},
"author": "Anton Winogradov <winogradovaa@gmail.com>",
"author": "Ivan Voischev <voischev.ivan@ya.ru>",
"contributors": [
{
"name": "Ivan Voischev",
"email": "voischev.ivan@ya.ru"
},
{
"name": "Anton Winogradov",
"email": "winogradovaa@gmail.com"
},
{
"name": "Alexej Yaroshevich",
"email": "zxqfox@gmail.com"
"name": "Ivan Demidov",
"email": "scrum@list.ru"
}
],
"homepage": "https://github.com/posthtml/posthtml",
Expand Down
62 changes: 62 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var it = require('mocha').it
var expect = require('chai').expect
var describe = require('mocha').describe

var posthtml = require('../lib')

describe('core methods', function () {
it('tree should have methods', function () {
posthtml()
.use(function (tree) {
tree.should.have.property('render')
tree.should.have.property('parser')
tree.toString.should.be.a('function')
})
.process('<div></div>')
})

it('core methods parser', function (done) {
var html = '<import>'
var ref = '<div>import</div>'

posthtml()
.use(function (tree) {
tree.match({ tag: 'import' }, function (node) {
node.tag = false
node.content = tree.parser('<div>import</div>')
return node
})
return tree
})
.process(html)
.then(function (result) {
expect(ref).to.eql(result.html)

done()
})
.catch(function (error) {
done(error)
})
})

it('core methods render', function (done) {
var html = '\n<div>1</div>\n\t<div>2</div>\n'
var ref = '<div>1</div><div>2</div>'

posthtml()
.use(function (tree) {
var outherTree = ['\n', { tag: 'div', content: ['1'] }, '\n\t', { tag: 'div', content: ['2'] }, '\n']
var htmlWitchoutSpaceless = tree.render(outherTree).replace(/[\n|\t]/g, '')
return tree.parser(htmlWitchoutSpaceless)
})
.process(html)
.then(function (result) {
expect(ref).to.eql(result.html)

done()
})
.catch(function (error) {
done(error)
})
})
})

0 comments on commit e951b9f

Please sign in to comment.