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 comes. 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:
- scriptStart - the block ID of the beginning of the script.
- blocks - the blocks, serialized in sb3 format
- locale - the locale code (check demo for the list)
- opts - the options for generator
opts is an object, and can have these properties:
- tab - characters used for indenting C blocks. defaults to four spaces. Note, scratchblocks will not render C blocks unless the tab is space/tab.
- variableStyle - whether to add ::variables or not. defaults to "none" (never add), and can be set to "always" (always add) or "as-needed" (add if there's a name conflict).
Building
If you want to build, clone the GitHub repository and run:
npm i
npm run build
Note that Node.js 16+ is required for building.
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: