Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

slate-hyperprint

GitbookIO2.1kApache-2.02.3.0

A Slate plugin to print Slate models to their slate-hyperscript representation.

slate, hyperscript, print, jsx

readme

slate-hyperprint

NPM version

A library to convert Slate models to their slate-hyperscript representation.

You can use slate-hyperprint as a library to:

  • Run a script to easily convert Slate tests written in Yaml/JSON to hyperscript.
  • Improve the output of unit tests by comparing hyperscript strings instead of JSON values.
  • Facilitate debugging and console logging of Slate values.

See the online demo, that converts a Slate JSON representation to a Slate hyperscript representation.

Setup

yarn add slate-hyperprint [--dev]

Usage

import Slate from 'slate';
import hyperprint from 'slate-hyperprint';

console.log(
    hyperprint(
        Slate.Value.create({
            document: Slate.Document.create({
                nodes: [
                    Slate.Block.create({
                        type: 'paragraph',
                        data: { a: 1 },
                        nodes: [
                            Slate.Text.create('Hello')
                        ]
                    }
                )]
            })
        })
    )
);
// <value>
//   <document>
//     <paragraph a={1}>
//       Hello
//     </paragraph>
//   </document>
// </value>

hyperprint.log(...)
// Equivalent to console.log(hyperprint(...))

Options

slate-hyperprint accepts an option object:

hyperprint(value, options);
  • preserveKeys: boolean = false True to print node keys
  • strict: boolean = false True to preserve empty texts and other things that the formatting would otherwise omit. Useful when using hyperprint compare values in tests, because the output is stricter.
  • prettier: Object = { semi: false, singleQuote: true, tabWidth: 4 } Prettier config to use when formatting the output JSX.

Test

yarn run test

Build

yarn run build

CLI

slate-hyperprint also export a command line interface tool that converts yaml files to jsx. When installed globally (npm install slate-hyperprint --global) it can be used like so:

$ slate-hyperprint document.yaml

It will load the file, create a Slate document and print it to the console in jsx. Note: it will look for a value.document, state.document or document property. It will consider the whole content as the document if none are found.

You can write the output to a file like so slate-hyperprint input.yaml > output.js

Here is a command to convert a whole bunch of yaml files in a test folder:

$ for file in tests/**/*.yaml; do basename=$(echo $file | sed 's/\.yaml//'); slate-hyperprint $basename.yaml > $basename.js; done;

Thanks

The React equivalent react-element-to-jsx-string is and will remain a great source of inspiration.

changelog

Release notes

All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

Unreleased

2.3.0 - 28/11/2018

  • Make slate-hyperscript CLI accept JSON and JS files in addition to YAML files.

2.2.6 - 17/09/2018

  • User standalone version of prettier@1.13.0 to use hyperprint in browser environment

2.2.5 - 06/09/2018

  • Fix representation of data on document
  • Fix issues with data keys that are not a valid JSX attributes

2.2.4 - 19/04/2018

  • Escape newlines for texts that contain no other special characters too

2.2.3 - 17/04/2018

  • Escape newlines for text in tags

2.2.2 - 07/04/2018

  • Robust string escaping and value printing

2.2.1 - 26/03/2018

  • Support slate 0.33.x (renamed kind to object)

2.2.0 - 13/02/2018

  • Add strict option to preserve empty texts and other things that the formatting would otherwise omit

2.1.3 - 13/02/2018

  • Use older version of prettier, that still works in the browser

2.1.2 - 09/02/2018

  • Sort tag attributes, for deterministic output

2.1.1 - 08/02/2018

  • Add hyperprint.log util

2.1.0 - 08/02/2018

  • Add option 'preserveKeys'

2.0.1 - 07/02/2018

  • Increase CLI retro-compatibility

2.0.0 - 03/01/2018

  • Add CLI tool
  • Replace indent option with a prettier config option. Prettier is now used to format output.
  • Improve rendering of void nodes

1.1.1

  • Fix rendering of nodes containing whitespaces only

1.1.0

  • Added support for empty nodes to be rendered as <something />

1.0.0

  • Initial release