mirror of
https://github.com/ritz078/transform.git
synced 2026-11-21 08:33:55 +03:00
Improvements in html to JSX transformer (#253)
* partially fixes #252 * use fragments * fixes * changes * changes * fixes
This commit is contained in:
1 parent
2c50187d5e
commit
d2ee2181b0
5 files changed
+66
-17
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { useState } from "react";
|
||||
import { version } from "../package.json";
|
||||
|
||||
const prefix = "transform:";
|
||||
const prefix = `transform:${version}:`;
|
||||
|
||||
export function useLocalStorage(key, initialValue) {
|
||||
// State to store our value
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "transform",
|
||||
"version": "2.0.1",
|
||||
"version": "2.0.2",
|
||||
"repository": "https://github.com/ritz078/transform",
|
||||
"author": "ritz078 <rkritesh078@gmail.com>",
|
||||
"license": "MIT",
|
||||
|
||||
+15
-14
@@ -10,20 +10,15 @@ import { Alert, Heading } from "evergreen-ui";
|
||||
import Router from "next/router";
|
||||
|
||||
interface Settings {
|
||||
createClass: boolean;
|
||||
outputClassName: string;
|
||||
createFunction: boolean;
|
||||
outputFunctionName: string;
|
||||
}
|
||||
|
||||
const formFields = [
|
||||
{
|
||||
type: InputType.SWITCH,
|
||||
key: "createClass",
|
||||
label: "Create Class"
|
||||
},
|
||||
{
|
||||
type: InputType.TEXT_INPUT,
|
||||
key: "outputClassName",
|
||||
label: "Output Class Name"
|
||||
key: "createFunction",
|
||||
label: "Create function component"
|
||||
}
|
||||
];
|
||||
|
||||
@@ -31,8 +26,7 @@ export default function() {
|
||||
const name = "HTML to JSX";
|
||||
|
||||
const [settings, setSettings] = useSettings(name, {
|
||||
createClass: false,
|
||||
outputClassName: "TransformedComponent"
|
||||
createFunction: false
|
||||
});
|
||||
|
||||
const [_isSvg, setSvg] = useState(false);
|
||||
@@ -41,9 +35,16 @@ export default function() {
|
||||
async ({ value }) => {
|
||||
setSvg(isSvg(value));
|
||||
|
||||
const converter = new HtmlToJsx(settings);
|
||||
const converter = new HtmlToJsx({
|
||||
createClass: false
|
||||
});
|
||||
let result = converter.convert(value);
|
||||
|
||||
return converter.convert(value);
|
||||
if (settings.createFunction) {
|
||||
result = `export const Foo = () => (${result})`;
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
[settings]
|
||||
);
|
||||
@@ -68,7 +69,7 @@ export default function() {
|
||||
<ConversionPanel
|
||||
transformer={transformer}
|
||||
editorTitle="HTML"
|
||||
resultLanguage="javascript"
|
||||
resultLanguage={settings.createFunction ? "javascript" : "text"}
|
||||
resultTitle="JSX"
|
||||
editorLanguage="html"
|
||||
editorSettingsElement={getSettingsElement}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
diff --git a/node_modules/htmltojsx/src/htmltojsx.js b/node_modules/htmltojsx/src/htmltojsx.js
|
||||
index afe4c85..b326beb 100644
|
||||
--- a/node_modules/htmltojsx/src/htmltojsx.js
|
||||
+++ b/node_modules/htmltojsx/src/htmltojsx.js
|
||||
@@ -326,19 +326,9 @@ HTMLtoJSX.prototype = {
|
||||
convert: function(html) {
|
||||
this.reset();
|
||||
|
||||
- var containerEl = createElement('div');
|
||||
+ var containerEl = createElement('null');
|
||||
containerEl.innerHTML = '\n' + this._cleanInput(html) + '\n';
|
||||
|
||||
- if (this.config.createClass) {
|
||||
- if (this.config.outputClassName) {
|
||||
- this.output = 'var ' + this.config.outputClassName + ' = React.createClass({\n';
|
||||
- } else {
|
||||
- this.output = 'React.createClass({\n';
|
||||
- }
|
||||
- this.output += this.config.indent + 'render: function() {' + "\n";
|
||||
- this.output += this.config.indent + this.config.indent + 'return (\n';
|
||||
- }
|
||||
-
|
||||
if (this._onlyOneTopLevel(containerEl)) {
|
||||
// Only one top-level element, the component can return it directly
|
||||
// No need to actually visit the container element
|
||||
@@ -351,13 +341,14 @@ HTMLtoJSX.prototype = {
|
||||
this._visit(containerEl);
|
||||
}
|
||||
this.output = this.output.trim() + '\n';
|
||||
- if (this.config.createClass) {
|
||||
- this.output += this.config.indent + this.config.indent + ');\n';
|
||||
- this.output += this.config.indent + '}\n';
|
||||
- this.output += '});';
|
||||
- } else {
|
||||
- this.output = this._removeJSXClassIndention(this.output, this.config.indent);
|
||||
+ this.output = this._removeJSXClassIndention(this.output, this.config.indent);
|
||||
+
|
||||
+ if (this.output.trim().startsWith("{") || this.output.trim().endsWith("}")) {
|
||||
+ this.output = `<>\n${this.output}</>`
|
||||
+ } else if (this.output.trim().startsWith("<null>") && this.output.trim().endsWith("</null>")) {
|
||||
+ this.output = this.output.replace("<null>", "<>").replace("</null>", "</>")
|
||||
}
|
||||
+
|
||||
return this.output;
|
||||
},
|
||||
|
||||
+2
-1
@@ -22,7 +22,8 @@ export async function prettify(language: string, value: string) {
|
||||
} else {
|
||||
result = prettier.format(value, {
|
||||
parser: prettierParsers[language] || language,
|
||||
plugins
|
||||
plugins,
|
||||
semi: false
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user