diff --git a/.gitignore b/.gitignore index b512c09..cd4211e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +index.js \ No newline at end of file diff --git a/README.md b/README.md index 8d689a2..d73561d 100644 --- a/README.md +++ b/README.md @@ -10,16 +10,23 @@ npm i react-interactive-cljs ```js "use client" -import { CljsCodeBlock } from "react-interactive-cljs" +import { CodeBlock } from "react-interactive-cljs" export function Page() { - return ({`(println "Hello") -(+ 2 2)`}) + return ({`(println "Hello") +(+ 2 2)`}) ) + ``` The code above will render like the image below ![](before.png) After clicking the "Run" button it will render like the image below -![](after.png) \ No newline at end of file +![](after.png) + +Accepts the same props as [react-syntax-highlighter](https://github.com/react-syntax-highlighter/react-syntax-highlighter?tab=readme-ov-file#props) +plus: + +* `language` defaults to "clojure" +* `allowEval` a boolean that defaults to true if the language is "clojure". \ No newline at end of file diff --git a/index.js b/index.js deleted file mode 100644 index 051922f..0000000 --- a/index.js +++ /dev/null @@ -1,40 +0,0 @@ -import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; -import { darcula as style } from 'react-syntax-highlighter/dist/esm/styles/prism'; -import React from 'react'; -import { evaluate } from 'eval-cljs'; -function PlayIcon() { - return /*#__PURE__*/React.createElement("svg", { - viewBox: "0 0 24 24", - fill: "currentColor", - className: "size-6" - }, /*#__PURE__*/React.createElement("path", { - fillRule: "evenodd", - d: "M4.5 5.653c0-1.427 1.529-2.33 2.779-1.643l11.54 6.347c1.295.712 1.295 2.573 0 3.286L7.28 19.99c-1.25.687-2.779-.217-2.779-1.643V5.653Z", - clipRule: "evenodd" - })); -} -export function CljsCodeBlock({ - children -}) { - const [output, setOutput] = React.useState(""); - function handleRun() { - setOutput("Running..."); - let [evalOutput, printOutput] = evaluate(children); - setOutput(printOutput.concat(evalOutput).join("\n")); - } - return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SyntaxHighlighter, { - language: "clojure", - style: style, - showLineNumbers: false - }, children), /*#__PURE__*/React.createElement("button", { - onClick: () => handleRun(), - style: { - minWidth: 30, - minHeight: 30 - } - }, /*#__PURE__*/React.createElement(PlayIcon, null)), output && /*#__PURE__*/React.createElement(SyntaxHighlighter, { - language: "clojure", - style: style, - showLineNumbers: false - }, output)); -} diff --git a/index.jsx b/index.jsx index cf7de08..881040f 100644 --- a/index.jsx +++ b/index.jsx @@ -1,5 +1,5 @@ import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; -import { darcula as style } from 'react-syntax-highlighter/dist/esm/styles/prism'; +import { darcula } from 'react-syntax-highlighter/dist/esm/styles/prism'; import React from 'react'; import { evaluate } from 'eval-cljs'; @@ -9,7 +9,7 @@ function PlayIcon() { ); } -export function CljsCodeBlock({ children }) { +export function CodeBlock({ children, language = "clojure", allowEval = true, style = darcula, ...props }) { const [output, setOutput] = React.useState("") function handleRun() { @@ -21,18 +21,21 @@ export function CljsCodeBlock({ children }) { return ( <> + {...props} + > {children} - - {output && ( - {output} - )} + {allowEval && language === "clojure" && (<> + + {output && ( + {output} + )} + )} ); } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 0768f6b..0c84a6b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,12 +10,14 @@ "license": "MIT", "dependencies": { "eval-cljs": "^0.0.2", - "react": "^18.0.0", "react-syntax-highlighter": "^15.5.0" }, "devDependencies": { "@babel/cli": "^7.24.8", "@babel/preset-react": "^7.24.7" + }, + "peerDependencies": { + "react": "^18.0.0" } }, "node_modules/@ampproject/remapping": { @@ -1082,6 +1084,7 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", + "peer": true, "dependencies": { "js-tokens": "^3.0.0 || ^4.0.0" }, @@ -1256,6 +1259,7 @@ "version": "18.3.1", "resolved": "https://registry.npmjs.org/react/-/react-18.3.1.tgz", "integrity": "sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==", + "peer": true, "dependencies": { "loose-envify": "^1.1.0" }, diff --git a/package.json b/package.json index 4c3c0ff..65591ee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-interactive-cljs", - "version": "0.0.6", + "version": "0.1.0", "description": "Interactive cljs code blocks", "main": "index.js", "scripts": {