# Lua: \[Productivity\] Token replacement

**URL:** <https://community.silverbullet.md/t/lua-productivity-token-replacement/2096>\
**Category:** Tricks & Techniques\
**Created:** [April 1, 2025, 1:21pm UTC](https://community.silverbullet.md/t/lua-productivity-token-replacement/2096 "2025-04-01T13:21:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![malys](https://community.silverbullet.md/user_avatar/community.silverbullet.md/malys/32/840_2.png) [@malys](https://community.silverbullet.md/u/malys)\
**Post date:** [April 1, 2025, 1:21pm UTC](https://community.silverbullet.md/t/lua-productivity-token-replacement/2096/1 "2025-04-01T13:21:54Z")

</div>

````auto
---
# Define key-value to replace
replace:
  t: text 
  d: demonstration
---

```space-lua
function replaceValue(content, tags)
  local c= content
  for k, v in pairs(tags) do
    local key="_"..k.."_"
    c=c:gsub(key, v)
  end
  return c
end

function getParent(path)
    local parent = path:match("^(.*)/[^/]+$")
    return parent
end

function getMeta(name)
  local tagsToReplace= index.extractFrontmatter(space.readPage(name)).frontmatter--space.getPageMeta(name)
  local parent = getParent(name)
  if (tagsToReplace == nil or (tagsToReplace ~=nil and tagsToReplace["replace"] ==nil)) and parent ~=nil then 
    tagsToReplace = getMeta(parent)
  end
  if tagsToReplace ~=nil and tagsToReplace["replace"] ~=nil then 
    return tagsToReplace
  end
end

command.define {
  name = "replace ",
  run = function()
    local pageName=editor.getCurrentPage()
    local tagsToReplace=getMeta(pageName)
    if tagsToReplace ~=nil then
      local content= space.readPage(pageName)
      local result=replaceValue(content, tagsToReplace["replace"])  
      space.writePage(pageName, result)
      editor.navigate(pageName)
    else
      print("Nothing to replace")
    end
  end
}
```

````

_d_ with this _t_ will be transformed to `demonstration with this text`

---

<div class="post-metadata">

**Author:** ![malys](https://community.silverbullet.md/user_avatar/community.silverbullet.md/malys/32/840_2.png) [@malys](https://community.silverbullet.md/u/malys)\
**Post date:** [April 4, 2025, 9:49am UTC](https://community.silverbullet.md/t/lua-productivity-token-replacement/2096/2 "2025-04-04T09:49:04Z")

</div>

add Inheritance from parent pages
