Skip to content

Commit 2c39cce

Browse files
committed
feat: init
0 parents  commit 2c39cce

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
3+
*-error.log
4+
*-lock.json

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Baoshuo
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# JSX Renderer for Hexo
2+
3+
[![Author](https://img.shields.io/badge/Author-Baoshuo-b68469.svg?style=flat-square)](https://baoshuo.ren)
4+
[![Version](https://img.shields.io/github/v/release/renbaoshuo/hexo-renderer-jsx?color=%235755d9&include_prereleases&label=version&style=flat-square)](https://github.com/renbaoshuo/hexo-renderer-jsx/releases)
5+
[![Repo Size](https://img.shields.io/github/repo-size/renbaoshuo/hexo-renderer-jsx?style=flat-square)](https://github.com/renbaoshuo/hexo-renderer-jsx)
6+
7+
## Install
8+
9+
```bash
10+
npm install hexo-renderer-jsx --save
11+
# or use yarn:
12+
# yarn add hexo-renderer-jsx
13+
```
14+
15+
## Author
16+
17+
**hexo-renderer-jsx** © [Baoshuo](https://github.com/renbaoshuo), Released under the [MIT](./LICENSE) License.<br>
18+
Authored and maintained by Baoshuo with help from [contributors](https://github.com/renbaoshuo/hexo-renderer-jsx/contributors).
19+
20+
> [Personal Website](https://baoshuo.ren) · [Blog](https://blog.baoshuo.ren) · GitHub [@renbaoshuo](https://github.com/renbaoshuo) · Twitter [@renbaoshuo](https://twitter.com/renbaoshuo)

index.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* global hexo */
2+
3+
'use strict';
4+
5+
const compile = require('./lib/compile');
6+
7+
function renderer(data, locals) {
8+
return compile(data)(locals);
9+
}
10+
11+
renderer.compile = compile;
12+
renderer.disableNunjucks = true;
13+
14+
hexo.extend.renderer.register('jsx', 'html', renderer, true);

lib/compile.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const React = require('react');
2+
const ReactDOMServer = require('react-dom/server');
3+
4+
require('@babel/register')({
5+
plugins: [
6+
[
7+
'@babel/plugin-transform-react-jsx',
8+
{
9+
runtime: 'automatic',
10+
},
11+
],
12+
],
13+
presets: [['@babel/preset-env', { targets: { node: true } }]],
14+
});
15+
16+
function compile(data) {
17+
const Component = require(data.path);
18+
19+
return function render(locals) {
20+
const element = React.createElement(Component.default || Component, locals);
21+
let renderedHTML = ReactDOMServer.renderToStaticMarkup(element);
22+
23+
if (renderedHTML.slice(0, 5).toLowerCase() === '<html') {
24+
renderedHTML = '<!DOCTYPE html>' + renderedHTML;
25+
}
26+
27+
return renderedHTML;
28+
};
29+
}
30+
31+
module.exports = compile;

package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "hexo-renderer-jsx",
3+
"version": "1.0.0",
4+
"description": "JSX Renderer for Hexo.",
5+
"main": "index.js",
6+
"repository": "https://github.com/renbaoshuo/hexo-renderer-jsx.git",
7+
"author": "Baoshuo Ren <[email protected]>",
8+
"license": "MIT",
9+
"dependencies": {
10+
"@babel/plugin-transform-react-jsx": "^7.17.3",
11+
"@babel/preset-env": "^7.16.11",
12+
"@babel/register": "^7.17.0",
13+
"react": "^17.0.2",
14+
"react-dom": "^17.0.2"
15+
}
16+
}

0 commit comments

Comments
 (0)