HTMLtoJSX converts HTML to JSX for use with React.
npm install htmltojsx
Alternatively, a web-based version is available at http://facebook.github.io/react/html-jsx.html
HTMLtoJSX can be used either as a command-line application or as a Node.js module. To use the command-line version, invoke the htmltojsx
command:
$ htmltojsx --help
Converts HTML to JSX for use with React.
Usage: htmltojsx [-c ComponentName] file.htm
Examples:
htmltojsx -c AwesomeComponent awesome.htm Creates React component "AwesomeComponent" based on awesome.htm
Options:
--className, -c Create a React component (wraps JSX in React.createClass call)
--help Show help
To use the Node.js module, require('htmltojsx')
and create a new instance. This is the same interface as the web-based version:
var HTMLtoJSX = require('htmltojsx');
var converter = new HTMLtoJSX({
indent: '\t',
hideComment: true,
createClass: true,
outputClassName: 'AwesomeComponent'
});
var output = converter.convert('<div>Hello world!</div>');
参数说明
- indent 缩进填充符, 默认俩空格.
- hideComment
传
true
时将不会将原文中的注释生成到译文中. - createClass
传
true
时将会在 jsx 外再创建一层React.createClass
进行包裹. - outputClassName
仅当
createClass
为true
时有效, 将会在React.createClass
前再加一个变量var xxx =
来保存这个组件.
- Complete the CLI tool.
- Complete the test cases.
- Support SVG convert.
- Fixed the bug of lost container when there're a DOM element and a comment element both on the root node.
中文说明
- 支持 SVG;
- 修正了当最外层根节点为一个注释与一个 DOM 节点时不会添加 container 的 bug.
- Added hide comment ability to HTMLtoJSX, pass
true
to parameterhideComment
to use it. - Removed the extra spaces while
createElement
option isfalse
. - Found the no instruction option
indent
which can set the indent space of output. - Fixed the bugs of the incorrect out put when the
<thead>
<tr>
tags were the outermost layers. - The
outputClassName
will be capitalized by default. - Updated the React and ReactDOM version of HTMLtoJSX (0.14.x -> 15.x.x).
- Added supports of attributes of SVG tags.
中文说明
- 参数里
hideComment
传true
就可以不再把注释也生成到 jsx 里去了; - 去掉了不创建包裹类的时候多余的空格;
- 找到了作者没有标注出来的用于修改缩进填充字符的
indent
属性. <thead>
<tr>
放在最外层的时候不会被干掉了.- 输入的
outputClassName
总是会被变成首字母大写. - 更新了 HTMLtoJSX 所依赖的 React 和 ReactDOM 版本 (0.14.x -> 15.x.x).
- 支持转换 SVG 标签的属性.
- #33 - Correctly handle
<textarea>
s and<pre>
s - #35 - Handle internal stylesheets (
<style>
tags). Thanks to Trevor Copeland
- #31 - Fixed
jsdom
dependency
- #8 - Handle case-insensitive attributes and style names
- #29 - Switch to
jsdom-no-contextify
to support older versions of Node.js
- #10 - Handle inline CSS shorthand style values
- #13 - Maintain valueless attributes handled by JSX
- #15 - Use uncontrolled input fields so they can still be edited
- #11 - Ensure HTML entities are handled correctly
- Initial release