From 3e23ba90835222188b8436d2491866d320502127 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 11 Oct 2023 15:26:51 +0200 Subject: [PATCH] Add more options to playground --- docs/_asset/editor.jsx | 195 +++++++++++++++++++++++++++++++---------- docs/_asset/index.css | 4 + 2 files changed, 152 insertions(+), 47 deletions(-) diff --git a/docs/_asset/editor.jsx b/docs/_asset/editor.jsx index 960de7f10..2c772dd92 100644 --- a/docs/_asset/editor.jsx +++ b/docs/_asset/editor.jsx @@ -17,7 +17,7 @@ * @property {false} ok * @property {Error} value * - * @typedef {EvalOk | EvalNok} EvalResult + * @typedef {EvalNok | EvalOk} EvalResult */ import {compile, nodeTypes, run} from '@mdx-js/mdx' @@ -105,18 +105,22 @@ function init(main) { } function Playground() { - const [value, setValue] = useState(sample) const [directive, setDirective] = useState(false) + const [evalResult, setEvalResult] = useState( + /** @type {unknown} */ (undefined) + ) + const [development, setDevelopment] = useState(false) const [frontmatter, setFrontmatter] = useState(false) const [gfm, setGfm] = useState(false) - const [markdown, setMarkdown] = useState(false) + const [formatMarkdown, setFormatMarkdown] = useState(false) + const [jsx, setJsx] = useState(false) const [math, setMath] = useState(false) - const [raw, setRaw] = useState(false) + const [outputFormatFunctionBody, setOutputFormatFunctionBody] = + useState(false) const [positions, setPositions] = useState(false) - const [output, setOutput] = useState('result') - const [evalResult, setEvalResult] = useState( - /** @type {unknown} */ (undefined) - ) + const [raw, setRaw] = useState(false) + const [show, setShow] = useState('result') + const [value, setValue] = useState(sample) useEffect( function () { @@ -147,25 +151,30 @@ function Playground() { if (raw) rehypePlugins.unshift([rehypeRaw, {passThrough: nodeTypes}]) const file = new VFile({ - basename: markdown ? 'example.md' : 'example.mdx', + basename: formatMarkdown ? 'example.md' : 'example.mdx', value }) - if (output === 'mdast') remarkPlugins.push([capture, {name: 'mdast'}]) - if (output === 'hast') rehypePlugins.push([capture, {name: 'hast'}]) - if (output === 'esast') recmaPlugins.push([capture, {name: 'esast'}]) + if (show === 'esast') recmaPlugins.push([capture, {name: 'esast'}]) + if (show === 'hast') rehypePlugins.push([capture, {name: 'hast'}]) + if (show === 'mdast') remarkPlugins.push([capture, {name: 'mdast'}]) /** @type {UnistNode | undefined} */ let tree await compile(file, { - outputFormat: output === 'result' ? 'function-body' : 'program', + development: show === 'result' ? false : development, + jsx: show === 'code' || show === 'esast' ? jsx : false, + outputFormat: + show === 'result' || outputFormatFunctionBody + ? 'function-body' + : 'program', recmaPlugins, rehypePlugins, remarkPlugins, useDynamicImport: true }) - if (output === 'result') { + if (show === 'result') { /** @type {MDXModule} */ const mod = await run(String(file), runtime) @@ -192,7 +201,7 @@ function Playground() { ) } - // `output === 'code'` + // `show === 'code'` return (
             
@@ -227,10 +236,23 @@ function Playground() {
         }
       }
     },
-    [directive, frontmatter, gfm, markdown, math, output, positions, raw, value]
+    [
+      development,
+      directive,
+      frontmatter,
+      gfm,
+      jsx,
+      formatMarkdown,
+      math,
+      outputFormatFunctionBody,
+      positions,
+      raw,
+      show,
+      value
+    ]
   )
 
-  const scope = markdown ? 'text.md' : 'source.mdx'
+  const scope = formatMarkdown ? 'text.md' : 'source.mdx'
   const compiledResult = /** @type {EvalResult | undefined} */ (evalResult)
   /** @type {JSX.Element | undefined} */
   let display
@@ -273,46 +295,20 @@ function Playground() {
         
         
- Output + Show -
-
- Input - -
@@ -389,6 +385,111 @@ function Playground() {
+ Input format + + +
+ +
+ Output format + + +
+ +
+ Development + + +
+ +
+ JSX + + +
+ +
Tree