From d2ee2181b096de3173c0ebc4fe8c3feaf9d4f218 Mon Sep 17 00:00:00 2001 From: Ritesh Kumar Date: Tue, 10 Nov 2020 13:59:50 +0530 Subject: [PATCH] Improvements in html to JSX transformer (#253) * partially fixes #252 * use fragments * fixes * changes * changes * fixes --- hooks/useLocalStorage.ts | 3 ++- package.json | 2 +- pages/html-to-jsx.tsx | 29 +++++++++++----------- patches/htmltojsx+0.3.0.patch | 46 +++++++++++++++++++++++++++++++++++ utils/prettify.ts | 3 ++- 5 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 patches/htmltojsx+0.3.0.patch diff --git a/hooks/useLocalStorage.ts b/hooks/useLocalStorage.ts index c972ea1..39988e2 100644 --- a/hooks/useLocalStorage.ts +++ b/hooks/useLocalStorage.ts @@ -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 diff --git a/package.json b/package.json index 8bcbfed..4e517a8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "transform", - "version": "2.0.1", + "version": "2.0.2", "repository": "https://github.com/ritz078/transform", "author": "ritz078 ", "license": "MIT", diff --git a/pages/html-to-jsx.tsx b/pages/html-to-jsx.tsx index fa91b0a..358cbb2 100644 --- a/pages/html-to-jsx.tsx +++ b/pages/html-to-jsx.tsx @@ -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() { \n${this.output}` ++ } else if (this.output.trim().startsWith("") && this.output.trim().endsWith("")) { ++ this.output = this.output.replace("", "<>").replace("", "") + } ++ + return this.output; + }, + diff --git a/utils/prettify.ts b/utils/prettify.ts index 896d137..af1b4f1 100644 --- a/utils/prettify.ts +++ b/utils/prettify.ts @@ -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 }); }