Public
Edited
May 16, 2021
1 star
Insert cell
Insert cell
{
// extend any library class:
wtf.extend((models)=>{
// add a method to the main document object
models.Doc.prototype.countLinks = function(){
return this.links().length
}
// add a method to all images
models.Image.prototype.isPNG = function(){
let reg= /\.png/i
return reg.test(this.file())
}
})
//use our new methods:
let doc = wtf('[[File:Westminstpalace.jpg|A building in [[London]] England.]]')
return {
links: doc.countLinks(),
png: doc.images()[0].isPNG()
}

}
Insert cell
Insert cell
{
// change or add any new template
wtf.extend((models, templates)=>{
//(template name lowercased)
templates.pingponggame = function(tmpl, list){
let arr = tmpl.split('|')
// add data to .templates() response
list.push({template:'game', score:arr.slice(1)})
// replace it with '13 to 2'
return arr[1] + ' to '+ arr[2]
}
})
// library now parses our ping-pong template:
let doc = wtf('he won the third match {{pingPongGame|13|2}}')
return {
text: doc.text(),
templates: doc.templates()
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more