#api/space-lua #maturity/experimental
Provides APIs to define and configure custom Tag|Tags.
Enables you to customize tags in a few ways:
Defines a custom tag. spec is a table that can contain:
* name (required) the name of the tag
* mustValidate a boolean defining whether or not schema validation must pass for the object to be indexed
* schema Schema to validate against
* transform callback function invoked when an object with tag name has been indexed. Allows you to make changes to it, skip indexing altogether or generate additional objects.
To define a JSON schema for validating an object (e.g. a page) tagged with #person ensuring that the age attribute is always a number, you can do the following:
tag.define {
name = "person",
-- mustValidate = true,
schema = {
type = "object",
properties = {
age = schema.number()
}
}
}
The result of this is that when editing a page tagged with #person where this schema does not validate, you will see this error being highlighted.
If you set mustValidate to true, schema validation will happen during the index phase (in addition to linting in the editor) and non-validating objects will not be indexed (with errors being reported in the JavaScript console). Use this to ensure all your objects conform to your schema (it does come at a slight performance penalty at the indexing phase).
Based on your page’s markdown, an indexer produces a list of objects to be indexed. If you define a transform callback function for a custom tag, this function will be invoked for each object with that tag, when the indexer encounters one. transform can inspect the object and do a few things:
{}): in this case the object will not be indexed at all.nil in which case indexing proceeds as usual.ref attribute as thenote Note
transformwill only be invoked when a page is indexed. This generally happens after making a change. To apply newly definedtransformfunctionality to all pages in your space, you have to reindex the entire space usingSpace: Reindex.
The following dynamically adds a 🧑 prefix Page Decorations|page decoration to all pages tagged with #person, such as Person/John and Person/Zef.
tag.define {
name = "person",
transform = function(o)
o.pageDecoration = { prefix = "🧑 " }
return o
end
}
Tags get assigned a data-tag-name attribute in the DOM, which you can use to do custom styling with Space Style.
Example: #my-red-tag
a[data-tag-name="my-red-tag"] {
background-color: red;
}