hooks:renderTopWidgets

In Silverbullet 0.94 this template:

tags: 
  - template
pageDecoration:
  prefix: "🟩 "
hooks.top:
  where: 'name =~ /^luoghi\//'
  order: 502
---

${widgets.breadcrumb()}

let me put the “breadcrumb” content on the top part of the rendered page.

In Silverbullet V2 I seemed to understand that the same function was possible with the following code, but the widgets.breadcrumb() is not used.
Where am I going wrong?

widgets = widgets or {}

function widgets.breadcrumb()
  local path = editor.getCurrentPage()

  -- Funzione per suddividere la stringa in parti basate su "/"
  local function split(str, sep)
    local result = {}
    for part in string.gmatch(str, "[^" .. sep .. "]+") do
      table.insert(result, part)
    end
    return result
  end

  -- Divide il percorso in segmenti
  local parts = split(path, "/")

  -- Costruisce i breadcrumb escludendo l'ultimo elemento
  local breadcrumbs = {}
  local currentPath = ""

  for i = 1, #parts - 1 do
    if i == 1 then
      currentPath = parts[i]
    else
      currentPath = currentPath .. "/" .. parts[i]
    end
    table.insert(breadcrumbs, "[[" .. currentPath .. "]]")
  end

  -- Restituisce i breadcrumb formattati
  return table.concat(breadcrumbs, " > ")
end

event.listen {
  name = "hooks:renderTopWidgets",
  run = function(e)
    return widgets.breadcrumb()
  end
}
1 Like

Just had this exact issue haha! Try:

event.listen {
  name = "hooks:renderTopWidgets",
  run = function(e)
    return widget.new {markdown = widgets.breadcrumb()}
  end
}

And see if it works!

1 Like

Fine!

Have you already sorted out how to get conditional application?
How to build the part “where: ‘name =~ /^places//’” in v2?

And now I have my breadcrump overlapping with the table of context..

An extremely dirty workaround to that would be something like this:

event.listen {
  name = "hooks:renderTopWidgets",
  run = function(e)
    return widget.new {markdown = "\n\n"..widgets.breadcrumb().."\n\n"}
  end
}

But I do think that that is a bug.

About the conditional application, I think that it should be as easy as:

event.listen {
 name = "hooks:renderTopWidgets",
  run = function(e)
    if not editor.getCurrentPage().startsWith("places/") then
        return
    end
    return widget.new {markdown = "\n\n"..widgets.breadcrumb().."\n\n"}
    end
}

Note: I am using editor.getCurrentPage() because _CTX.currentPage as specified in Space Lua for some reason has not been working for me

1 Like

I wil try your suggestion!

1 Like

Yeah I think you figured it all out by now, this still needs to be documented.