Executing
Table of Contents
Bugs 🐜 and To do ✅
-
executed cell gets replaced with its output. - more robust outputs
- replace "connected" output on exec
- Extract transpilation as Transfomers
Examples
console.log(document.referrer)
return new Object({foo: "bar"})
Async and Promises
return fetch('https://example.com')
.then( res => res.status )
return (async fn => {
const size = await lit.fs.du('/')
console.log("Local filesystem size:")
return size
})()
Transpiling
The meta syntax for transformers/transpiling is not yet stable, for now use the experimental babel
attribute, combined with specifying the source lang in the filename extention. (Currently requires explicitly setting repl
to js
to execute)
React jsx
Using the experimental react
attribute, a Cell can define its own content as a component, warning due to needing to use the same React instance as the parent application, the below with dynamic import won't work.
Instead, get React
from the utils in the lit
global.
Outputs
See Custom Viewers for information on built in viewers and how to create custom ones.
Raw svg
string
Markdown
console.log("A __*result*__: **`" + 42/13 + "`**")
return 42
A result: 3.230769230769231
42
return { foo: 'bar'}
Meta: lit
global and AST
When executing a Cell this
refers to the hast
node for that cell.
return this
To get the mdast
for the document, you need to refer to the global lit
object.
return lit.ast
To get info about the location of the current document, see lit.location
.
return lit.location
return lit.utils
Finding nodes in the ast can be done either manually or using some of the unist
utils available
const select = lit.utils.unist.select.select
return select('heading', lit.ast)
return lit.utils
.unist.select
.selectAll('heading', lit.ast)
.map( h => `${h.depth} ${h.data.id }`)
// get the title of the section containing the currently selected cell
// some utilities still need exposing to achieve this.
const currentPos = {}
const atPosTree = {}
const section =
return "TBD..."
Mutating Self
Code cell execution which modifies the content of a .lit
document outwith of its own (attached) output cell, initially itself, but then try an external doc.
// Mutated at Sun May 02 2021 12:59:07 GMT+0100 (BST)
const src = lit.file.contents
const unist = lit.utils.unist
const select = unist.select.select
const source = unist.source
const patchSource = unist.patchSource.default
const selector = 'code[meta=#mutateme]'
const node = select(selector, lit.ast)
const pos = node.position
const original = source(pos, src)
const newSrc = "```js #mutateme\n// Mutated at " + new Date() + "\n```"
return patchSource(src, pos, newSrc)
⚠️ This uses the still experimental selfmutate
attribute, and affects content outside of it's own attached output cell.
Dynamic imports
return (async args => {
const confetti = await
import('https://cdn.skypack.dev/canvas-confetti')
confetti.default({
origin: {y: 0},
spread: 55,
particleCount: 100,
ticks: 1000,
})
return "Wait for it... 🥳"
})()