Skip to content

A quick exploration in generating Gutenberg blocks from string templates

Notifications You must be signed in to change notification settings

mcsf/prototype-block-generator

Repository files navigation

Background

Try it

npm i
npm test

Architecture

  • parser uses HTM to parse JSX-like input into a workable element tree. However, since HTM operates on template strings and we need parse to work on plain strings, we also dangerously use eval to "hydrate" these templates.

  • generator operates on parser's element tree to generate Gutenberg code as a plain string.

  • evaluator turns string-based generated code into a usable Preact component. This isn't strictly needed for a simple JSON-to-JS code generator, but is useful for testing the generator. This piece also relies on eval. It assumes that the only environmental requirements to evaluate generated block code are Preact and WP primitives, so it requires those modules before calling eval.

Caveats

  • eval is recklessly used. Consider switching to a sandboxed alternative (e.g. safeEval).
  • Poorly tested, naively approached.
  • feel free to expand

About

A quick exploration in generating Gutenberg blocks from string templates

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published