This document demonstrates how scripts work in Lively4 markdown files, based on the working mermaid example.
In Lively4 markdown, scripts execute within the context of the markdown component:
// Get the lively-markdown component that contains this script
const markdownComponent = lively.query(this, "lively-markdown")
// Access the shadow root for DOM manipulation
const root = markdownComponent.shadowRoot
Scripts can use ES6 import syntax to load external libraries:
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10.9.1/dist/mermaid.esm.min.mjs';
Query and manipulate elements within the component’s shadow DOM:
// Find elements in the shadow root, not global document
const elements = root.querySelectorAll('code');
// Create and append elements normally
const container = document.createElement('div');
targetElement.parentNode.replaceChild(container, targetElement);
Based on the mermaid integration, here’s the pattern that works:
This is an example-trigger code block that should be transformed by the script above.
this
refers to the script element, use lively.query(this, "lively-markdown")
to get the containermarkdownComponent.shadowRoot
for proper DOM scopingThis pattern enables powerful interactive markdown documents in Lively4!