Userdata API clarification

OK, I did some progress and I think I get at least some part of how to work with the data, at least all objects having text item (paragraphs, items, tasks, etc.). Here is a more complex example and test page. @zef Additional questions are at the bottom of this post.


```space-lua
objects = objects or {}
objects.meta = objects.meta or {}

function objects.meta.tagged(objectName, tagName)
  local ret = {}
  for obj in query[[from tag(objectName)]] do
    if obj.tags != nil then
      for _, tgn in ipairs(obj.tags) do
        if tagName != nil then
          if tgn == tagName then
            table.insert(ret, obj)
          end
        else
          table.insert(ret, obj)
        end
      end
    end
  end
  return ret
end

function objects.meta.render(metaTable, renderFormat)
  local ret = ''
  for _, obj in ipairs(metaTable) do
    if obj.text != nil then
      ret = ret .. obj.text
      ret = ret .. ' (*[Ref.](</' .. obj.ref .. '>)*)\n\n'
    end
  end
  return {[renderFormat or 'markdown'] = ret}
end
```

A testing page with some tagged paragraphs.

# Tagged paragraphs

This is a #TEST paragraph.

Untagged paragraph.

Here is another paragraph with two #TEST-TEST tags. #TEST

Another untagged paragraph.

This is another #TEST-TEST tagged paragraph.

#TEST And yet another paragraph.

A listing page that lists the paragraphs by the tags.

# Listed paragraphs

## Tagged with #TEST

${objects.meta.render(objects.meta.tagged('paragraph', 'TEST'))}

## Tagged with #TEST-TEST

${objects.meta.render(objects.meta.tagged('paragraph', 'TEST-TEST'))}

Looks good and it works! Maybe it could be done better, e.g. using the templates and so on. I just place it here for people trying to dig into their data from the Lua. I hope it can help somebody.


What’s missing?

  1. I still don’t know a way to read page content. Solved - see below.
  2. How can I pass function argument into tag() inside the query[[]] block? I want to have generalized listObjectsByTag(objectName, tagName) function rather than few for all the objects with text field in metadata…
    Updated, see above. Supr easy! Just tag(objectName) inside the query[[]] block! :smiley: