🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@braid-design-system/source.macro

Package Overview
Dependencies
Maintainers
3
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@braid-design-system/source.macro

A [Babel macro][babel-plugin-macros] for authoring code snippets as code.

1.0.0
latest
npm
Version published
Weekly downloads
298
-71.35%
Maintainers
3
Weekly downloads
 
Created
Source

@braid-design-system/source.macro

A Babel macro for authoring code snippets as code.

Example and use cases

In Braid, documentation is authored as code. Rather than writing snippets and code examples as strings, the source macro enables them to be written as code instead. The first argument passed to the macro will be returned both as a string and as the original value inside an object:

import source from '@braid-design-system/source.macro';

const result = source(<div>Hello world</div>);
// Returns { code: '<div>Hello world</div>', value: <div>Hello world</div> }

This macro enables several capabilities that are not easily achievable with snippets:

  • Snippets can be type-checked and linted
  • Snippets can be rendered directly in documentation
  • The code string representing the snippet can still be displayed in documentation, copied to the clipboard, or in the case of Braid, used to generate a Playroom link

Setup

Install @braid-design-system/source.macro:

npm install --save-dev @braid-design-system/source.macro

Install and configure babel-plugin-macros:

npm install --save-dev babel-plugin-macros
// babel.config.js
module.exports = {
  plugins: ['babel-plugin-macros'],
};

Configuration

The source macro supports the following configuration options:

// babel.config.js
module.exports = {
  plugins: [
    [
      'babel-plugin-macros',
      {
        source: {
          // Whether to return a code string and the original value in an object, or just the code string. Defaults to `false`.
          codeOnly: true,
          // Whether to format the code string using Prettier. Defaults to `true`.
          formatWithPretteir: false,
        },
      },
    ],
  ],
};

Alternatives

react-element-to-jsx-string is a library that converts JSX elements to strings, but it cannot serialize functions, and the formatting of the resulting string is subpar relative to Prettier.

FAQs

Package last updated on 21 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts