Skip to content

Commit

Permalink
refactor(lib): remove module wrapper && minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky committed Jan 18, 2018
1 parent 54562b4 commit aed12f3
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 2,562 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ const html = render(tree, options)

|Name|Type|Default|Description|
|:--:|:--:|:-----:|:----------|
|**[`singleTags`](#singletags)**|`{Array}`|`[]`|Specify custom single tags (self closing)|
|**[`closingSingleTag`](#closingSingleTag)**|`{String\|RegExp}`|[`>`](#default)|Specify the single tag closing format|
|**[`singleTags`](#singletags)**|`{Array<String\|RegExp>}`|`[]`|Specify custom single tags (self closing)|
|**[`closingSingleTag`](#closingSingleTag)**|`{String}`|[`>`](#default)|Specify the single tag closing format|

### `singleTags`

Expand All @@ -110,7 +110,7 @@ const html = render(tree, options)

#### `{RegExp}`

```
```js
const render = require('posthtml-render')

const tree = [ { tag: '%=title%' } ]
Expand Down
2 changes: 1 addition & 1 deletion RENDER.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Options

| Name | Type | Description |
| --- | --- | --- |
| singleTags | <code>Array</code> | Custom single tags (selfClosing) |
| singleTags | <code>Array.&lt;(String\|RegExp)&gt;</code> | Custom single tags (selfClosing) |
| closingSingleTag | <code>String</code> | Closing format for single tag Formats: ``` tag: `<br></br>` ```, slash: `<br />` ```, ```default: `<br>` ``` |

<a name="render..html"></a>
Expand Down
2 changes: 2 additions & 0 deletions lib/browser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/browser.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 168 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
var SINGLE_TAGS = [
'area',
'base',
'br',
'col',
'command',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'menuitem',
'meta',
'param',
'source',
'track',
'wbr',
// Custom (PostHTML)
'import',
'include',
'extend',
'component'
]

/**
* Render PostHTML Tree to HTML
*
* @param {Array|Object} tree PostHTML Tree
* @param {Object} options Options
*
* @return {String} HTML
*/
function render (tree, options) {
/**
* Options
*
* @type {Object}
*
* @prop {Array<String|RegExp>} singleTags Custom single tags (selfClosing)
* @prop {String} closingSingleTag Closing format for single tag
*
* Formats:
*
* ``` tag: `<br></br>` ```, slash: `<br />` ```, ```default: `<br>` ```
*/
options = options || {}

var singleTags = SINGLE_TAGS.concat(options.singleTags || [])
var singleRegExp = singleTags.filter(function (tag) {
return tag instanceof RegExp ? tag : false
})

var closingSingleTag = options.closingSingleTag

return html(tree)

/**
* HTML Stringifier
*
* @param {Array|Object} tree PostHTML Tree
*
* @return {String} result HTML
*/
function html (tree) {
var result = ''

traverse([].concat(tree), function (node) {
if (!node) return

if (typeof node === 'string' || typeof node === 'number') {
result += node

return
}

if (typeof node.tag === 'boolean' && !node.tag) {
typeof node.content !== 'object' && (result += node.content)

return node.content
}

// treat as new root tree if node is an array
if (Array.isArray(node)) {
result += html(node)

return
}

var tag = node.tag || 'div'

if (isSingleTag(tag, singleTags, singleRegExp)) {
result += '<' + tag + attrs(node.attrs)

switch (closingSingleTag) {
case 'tag':
result += '></' + tag + '>'

break
case 'slash':
result += ' />'

break
default:
result += '>'
}
} else {
result += '<' + tag + (node.attrs ? attrs(node.attrs) : '') + '>' + (node.content ? html(node.content) : '') + '</' + tag + '>'
}
})

return result
}
}

/**
* @module posthtml-render
*
* @version 1.0.7
* @license MIT
*/
module.exports = render

/** @private */
function attrs (obj) {
var attr = ''

for (var key in obj) {
if (typeof obj[key] === 'boolean' && obj[key]) {
attr += ' ' + key
} else if (
typeof obj[key] === 'string' ||
typeof obj[key] === 'number'
) {
attr += ' ' + key + '="' + obj[key] + '"'
}
}

return attr
}

/** @private */
function traverse (tree, cb) {
if (Array.isArray(tree)) {
for (var i = 0, length = tree.length; i < length; i++) {
traverse(cb(tree[i]), cb)
}
} else if (typeof tree === 'object' && tree.hasOwnProperty('content')) {
traverse(tree.content, cb)
}

return tree
}

/** @private */
function isSingleTag (tag, singleTags, singleRegExp) {
if (singleRegExp.length) {
for (var i = 0; i < singleRegExp.length; i++) {
return tag.match(singleRegExp[i]) ? true : false
}
}

if (singleTags.indexOf(tag) === -1) {
return false
}

return true
}
154 changes: 0 additions & 154 deletions lib/posthtml-render.js

This file was deleted.

Loading

0 comments on commit aed12f3

Please sign in to comment.