Skip to content

Instantly share code, notes, and snippets.

@DinoscapeProgramming
Last active February 18, 2026 22:04
Show Gist options
  • Select an option

  • Save DinoscapeProgramming/6928ce7a05cad354277fd409f1a69261 to your computer and use it in GitHub Desktop.

Select an option

Save DinoscapeProgramming/6928ce7a05cad354277fd409f1a69261 to your computer and use it in GitHub Desktop.
Dug through random, undocumented properties to get code generation with Blockly working in a headless Node.js environment
const { JSDOM } = require("jsdom");
const Blockly = require("blockly/core");
const { javascriptGenerator: generator } = require("blockly/javascript");
require("blockly/blocks");
const dom = new JSDOM("<!DOCTYPE html><html><body></body></html>");
global.window = dom.window;
global.document = dom.window.document;
global.DOMParser = dom.window.DOMParser;
const xmlContent = `
<xml xmlns="https://developers.google.com/blockly/xml">
<block type="math_arithmetic" inline="false" x="10" y="10">
<field name="OP">MULTIPLY</field>
<value name="A">
<shadow type="math_number">
<field name="NUM">6</field>
</shadow>
</value>
<value name="B">
<shadow type="math_number">
<field name="NUM">9</field>
</shadow>
</value>
</block>
</xml>
`;
const parser = new DOMParser();
const xmlDom = parser.parseFromString(xmlContent, "text/xml");
const workspace = new Blockly.Workspace();
generator.init(workspace);
Blockly.Xml.domToWorkspace(xmlDom.documentElement, workspace);
const code = generator.workspaceToCode(workspace);
console.log(code);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment