Directives
Directives can be added manually by the user, or via a plugin.
Calling a directive will alter the document in some form
Example: A directive that uppercases all the strings of a document
| KyoML | JSON |
|---|---|
@uppercase
object = "world"
introduction = "hello"
sentence = "${object} ${introduction}"
|
{
"object": "WORLD",
"introduction": "HELLO",
"sentence": "HELLO WORLD"
}
|
Loading the directive
kyoml.compile(
`
@uppercase
key = 'value'
`, {
async: false,
interpolation: true,
directives: {
uppercase: (node) => {
//
// Write your own directive
//
if (typeof node.value === "string") {
return node.set(node.value.toUpperCase())
}
Object.keys(node.value).forEach(key => {
const str = node.value[key];
if (typeof str === "string") {
node.value[key] = str.toUpperCase();
}
})
}
}
}
)
The ‘node’ object
Each directive is called with a node object, which is a reference to the place in the document the directive was called at. The node object contains the following properties:
rootThe root of the entire documentpathThe path to the field as a string (e.gdocument.users[3].name)baseThe object that contains the prop referenced by the nodekeyThe key of the property on the base objectvalue(computed) The value referenced by the nodeget(path)A helper method to retrieve any element on the document thanks to a string pathset(value)A helper method used to replace the value referenced by the node
Calling the directive
At the root of a block
Calling a directive at the root of a block will pass the whole block to the directive
e.g
myBlock {
@uppercase
object = "world"
introduction = "hello"
}
On a single value
Single values can be piped into a directive using the left or right handed pipe symbols (<| and |>)
e.g
myBlock {
object = "world" |> @uppercase
introduction = "hello"
}