ObjectDecorators in v2

I use Object Decorators in v1. Specifically:

objectDecorators:
- where: 'tag = "page" and name = "index"'
  attributes:
    pageDecoration.disableTOC: true
- where: 'tag = "page" and tags = "house"'
  attributes:
    pageDecoration.prefix: "'🏠'"
- where: 'tag = "page" and (tags = "music" or tags = "make-music")'
  attributes:
    pageDecoration.prefix: "'🎵'"
- where: 'tag = "page" and name =~ "^People/"'
  attributes:
    pageDecoration.prefix: "'👤'"

This seems like a good candidate for Lua-fication, replacing the query language. However, this does affect the index (because it’s rendered onto links into the page), so perhaps not that straightforward?

Yes, this is on my list to add back in a more Lua native way. What I planned was to have a way to configure Lua metatables to use for tags.

Conceptually:

index.defineTagMetaTable("page", {
    __index = function(self, attr)
        if attr == "pageDecoration" and self.name == "index" then
           return {disableTOC=true}
        end
    end
})

Then, whenever you query with index.tag "page" the objects it return would have the right metatables set, and then when requesting the pageDecoration attribute, it would calculate this value on the fly.

For those not familiar with metatables in Lua: Programming in Lua : 13

2 Likes

Thanks, so is there currently no way of disabling the TOC on say the home page?
Nick

Not right now.

1 Like

I just added support back for explicitly adding the disableTOC to frontmatter, so that should work again.

2 Likes

I still do not understand why the TOC cannot be generated by Lua function and hooked to the top of the page (or called like ${toc(3)} for example)? There is support to get the AST of the page so it’s just about traversing it and fetching headings leafs… which seems fairly easy to implement in Lua and add to the libraries with some pre-defined hook for the top to use it…

Is that not how it already works exactly (minus use of the AST because I haven’t decided how to best expose that to Lua yet)?

There used to be a function to parse MD into AST in v1… can’t remember the name, parseMardown? Can’t lookup atm.

In Lua, what about to return a table with methods…

p = space.getPage('/foo')
f = p.getFrontMatter()
m = p.getContent()
a = m.getAST()
t = m.getTOC(6)

No real idea…