recma JavaScript Stringifier Plugin

1.0.0 · active · verified Sun Apr 19

The `recma-stringify` package is a `unified` plugin designed to serialize JavaScript Abstract Syntax Trees (ASTs) back into JavaScript code. It operates within the `recma` ecosystem, which focuses on transforming JavaScript ASTs, similar to how `remark` handles Markdown and `rehype` handles HTML. Currently at version `1.0.0`, it is part of an active project with a stable release cadence that generally aligns with Node.js LTS versions, with major releases dropping support for unmaintained Node.js environments. Its primary differentiator is its seamless integration into the `unified` processing pipeline, abstracting the underlying `estree-util-to-js` utility. This allows developers to easily incorporate JavaScript code generation into broader content processing workflows, such as compiling MDX. It does not parse JavaScript itself; for that, it's typically paired with `recma-parse` or used via the overarching `recma` core package.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates processing an HTML fragment through rehype, converting it to a recma (JS) AST, applying JSX transformations, and then stringifying it to output JSX code. This shows the typical `unified` pipeline usage.

import recmaJsx from 'recma-jsx'
import recmaStringify from 'recma-stringify'
import rehypeParse from 'rehype-parse'
import rehypeRecma from 'rehype-recma'
import {unified} from 'unified'

async function processHtmlToJsx() {
  const file = await unified()
    .use(rehypeParse, {fragment: true}) // Parse HTML fragment
    .use(rehypeRecma) // Convert HTML AST (hast) to JS AST (esast)
    .use(recmaJsx) // Transform JS AST for JSX elements
    .use(recmaStringify) // Serialize the JS AST back to code
    .process('<p>Hi!<h1>Hello!') // Process the input string

  console.log(String(file))
}

processHtmlToJsx().catch(console.error)

view raw JSON →