├── .gitignore
├── package.json
├── index.js
└── readme.md
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "posthtml-static-react",
3 | "version": "1.0.1",
4 | "description": "A PostHTML plugin to render custom elements as static React components.",
5 | "keywords": [
6 | "parser",
7 | "transform",
8 | "manipulation",
9 | "posthtml",
10 | "posthtml-plugin",
11 | "react",
12 | "static-react",
13 | "components"
14 | ],
15 | "main": "index.js",
16 | "repository": {
17 | "type": "git",
18 | "url": "https://github.com/rasmusfl0e/posthtml-static-react.git"
19 | },
20 | "author": {
21 | "name": "rasmusfl0e",
22 | "email": "rasmusfl0e@gmail.com"
23 | },
24 | "license": "ISC",
25 | "bugs": {
26 | "url": "https://github.com/rasmusfl0e/posthtml-static-react/issues"
27 | },
28 | "homepage": "https://github.com/rasmusfl0e/posthtml-static-react#readme",
29 | "dependencies": {
30 | "posthtml-match-helper": "^1.0.0",
31 | "react": "^0.14.0",
32 | "react-dom": "^0.14.0"
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | var React = require("react");
2 | var server = require("react-dom/server");
3 |
4 | var matchHelper = require("posthtml-match-helper");
5 |
6 | var attrToProp = {
7 | "class": "className",
8 | "for": "htmlFor"
9 | };
10 |
11 | function toReact (node, components) {
12 | var tag = node.tag;
13 | var attrs = node.attrs;
14 | var element = (tag in components) ? components[tag] : (tag in React.DOM) ? tag : "div";
15 | var props = {
16 | key: Math.random().toString(32).slice(2)
17 | };
18 |
19 | if (attrs) {
20 | Object.keys(attrs).map(function (attr) {
21 | var prop = attr in attrToProp ? attrToProp[attr] : attr;
22 | props[prop] = attrs[attr];
23 | });
24 | }
25 |
26 | var children = null;
27 | if (Array.isArray(node.content)) {
28 | children = node.content.map(function (_node) {
29 | return typeof _node === "string" ? _node : toReact(_node, components);
30 | });
31 | }
32 |
33 | return React.createElement(element, props, children);
34 | }
35 |
36 | module.exports = function (matcher, components) {
37 |
38 | return function posthtmlStaticReact (tree) {
39 |
40 | tree.match(matchHelper(matcher), function (node) {
41 | return server.renderToStaticMarkup(toReact(node, components));
42 | });
43 |
44 | };
45 |
46 | };
47 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # posthtml-static-react
2 |
3 | > A [PostHTML](https://github.com/posthtml/posthtml) plugin to render custom elements as static React components.
4 |
5 | Basic usage
6 |
7 | ```js
8 | var React = require("react");
9 | var posthtml = require("posthtml");
10 | var renderStaticReact = require("posthtml-static-react");
11 |
12 | var html = "