parse-sb3-blocks

Parse sb3 blocks, and generate scratchblocks formatted code.

What is parse-sb3-blocks?

Parsing Scratch 3.0 file was a nightmare. Things are compressed and we needed to handle constants or special cases (variables inserted on input, empty slots, custom blocks...) ourselves - until parse-sb3-blocks came. This library comes with an easy-to-use parser, and as a by-product, a scratchblocks generator.

parse-sb3-blocks parses one script of your choice from the blocks object serialized in sb3 format. A simple mechanism called "inputtable" handles anything that looks like an argument.

It also provides scratchblocks code generator - which supports all languages supported by Scratch. The generator also handles indenting, menu items and variable name conflicts.

Get parse-sb3-blocks

Install via npm

Install via CDN

Or, you can download the file.

Usage

To use toScratchblocks function:

import {toScratchblocks} from 'parse-sb3-blocks';

Or, with CommonJS:

const {toScratchblocks} = require('parse-sb3-blocks');

If you are using CDN/download version:

const {toScratchblocks} = parseSB3Blocks;

After that you can parse serialized blocks and generate scratchblocks. Here is one example:

const stage = projectJSON.targets.filter(t => t.isStage)[0];
const whenGreenflag = Object.keys(stage.blocks).filter(key => stage.blocks[key].opcode === 'event_whenflagclicked')[0];
console.log(toScratchblocks(whenGreenflag, stage.blocks, "en", {tab: "  "}));

toScratchblocks takes three to four arguments:

opts is a object, and can have these properties:

Building

If you want to build, clone the GitHub repository and run:

npm i
npm run build

Note that Node.js 14+ will be required.

Credits

Huge thanks to Tim Radvan ( tjvr ) for making scratchblocks and the original version of the generator. Some CSS styles created by tjvr and the contributors from the website is used under MIT License.

This website uses some libraries: