Pandoc Lua-Filter to replace `=this.meta_var` by its value

What I’m trying to do

I’m using pandoc to export some of my notes to docx, but want to use metadata variables stored in the yaml of the note and have their value rendered in the final document. Ideally this should be compatible with Obsidians =this.keyword to render metadata in the document.

Things I have tried

I found a Lua-filter on stackoverflow (declaring arbitrary variables for pandoc conversion using YAML metadata block - Stack Overflow) that replaces metadata variables between a double pair of curly brackets:

local vars = {}

function get_vars (meta)
  for k, v in pairs(meta) do
    if pandoc.utils.type(v) == 'Inlines' then
      vars["{{" .. k .. "}}"] = {table.unpack(v)}
    end
  end
end

function replace (el)
  for k, v in pairs(vars) do

    local startIndex, endIndex = string.find(el.text, k) 
    if startIndex then

      local preceding = string.sub(el.text, 1, startIndex - 1)
      if string.len(preceding) > 0 then
        table.insert(v, 1, preceding)
      end

      local remaining = string.sub(el.text, endIndex+1)
      if string.len(remaining) > 0 then
        table.insert(v, remaining)
      end

      return v
    end
  end
  return nil
end


return {{Meta = get_vars}, {Str = replace}}

Unfortunately I’m not able to change the get_vars function to work with =this.meta_var instead of {{meta_var}} do to my limited knowledge of the Lua language. Can anyone help?

I’m winging this totally as I’ve not done a lot of lua coding, nor do I have a lua environment available, and I’m just guessing on what happens in this code segments. All those disclaimers aside, what happens if you change the table.unpack line to:

vars["`= this." .. k .. "`"] =  { table.unpack(v)}

You might also want to try with "\`= this." … k … "\`" if the first doesn’t work. Not sure how this would deal with extra spaces here and there, and if those needs to be added into the strings as well…

Hopefully this shot in the blind provides you with some help

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.