A public SilverBullet space is slow on a first visit. Before SilverBullet can show anything properly, the visitor's browser copies and indexes the whole space, and until then they see a plain, unstyled page. On a big space that can take half a minute. This setup shows a styled copy of the page straight away, with its sidebar and footer, and steps aside once SilverBullet is ready. It was made for SilverBullet 2.12 and needs admin access to the space's dashboard.
The server already sends each page's text as plain HTML before SilverBullet starts. A small script in the space's Head HTML (extra HTML the server adds to every page) takes that text, dresses it with the styles from one theme page and the text of a sidebar page and a footer page, and lays it over the screen. When SilverBullet has finished loading, the copy fades out.
The server can't run queries, so a list made by a query shows up as raw code in the copy. Lists that visitors should see straight away are therefore written into their pages ("baked") and refreshed whenever a page is saved.
Keep the page names below and the sidebar's name garden.nav as they are, because the script looks for them by name.
Library/Garden Sidebar with the links for your sidebar, for example:**Explore**
[⌂ Home](</garden/home>)
[▤ All pages](</garden/All pages>)
**Topics**
[◦ Topic One](<Topic One>)
[◦ Topic Two](<Topic Two>)
Make a page called Library/Garden Footer with your footer line, for example a licence and your name.
Make a page called Library/Garden Theme. Put all your space's styles on this page, because the script only reads styles from here. Add a space-style code block (three backticks followed by space-style) and paste this inside it. The first part is the sidebar, title and footer; change the colours to suit. The second part lays out the styled copy.
/* priority: 10 */
html {
--garden-panel: #292c2e;
--garden-panel-border: #3a3d40;
--garden-panel-muted: #a9aeb2;
--garden-panel-faint: #7d8386;
--garden-panel-active: #f2dedd;
--garden-faint: #8b9398;
}
/* Sidebar: a dark panel, running up into the top bar */
.sb-nav-root[data-slot="lhs"] {
background: var(--garden-panel);
border-right: 1px solid var(--garden-panel-border);
color: var(--garden-panel-muted);
}
#sb-main .sb-nav-root[data-slot="lhs"] {
border-right-color: var(--garden-panel-border);
}
#sb-top > .sb-nav-spacer:has(~ .main),
#garden-layer .garden-layer-top-side {
position: relative;
}
#sb-top > .sb-nav-spacer:has(~ .main)::after,
#garden-layer .garden-layer-top-side::after {
content: "";
position: absolute;
inset: 0 0 -1px 0;
background: var(--garden-panel);
border-right: 1px solid var(--garden-panel-border);
}
.sb-nav-root[data-slot="lhs"] .garden-nav {
padding: 8px 10px;
font-size: 15px;
line-height: 1.5;
}
.sb-nav-root[data-slot="lhs"] .garden-nav a {
color: var(--garden-panel-muted);
text-decoration: none;
background: transparent;
}
.sb-nav-root[data-slot="lhs"] .garden-nav a:hover {
color: var(--garden-panel-active);
}
.sb-nav-root[data-slot="lhs"] .garden-nav strong {
font-size: 11px;
text-transform: uppercase;
letter-spacing: 0.12em;
font-weight: 400;
color: var(--garden-panel-faint);
}
.sb-nav-root[data-slot="lhs"] .garden-nav p {
margin: 0 0 22.5px 0;
}
/* Page title and date */
.garden-title.garden-title {
font-size: 13px;
color: var(--garden-faint);
padding-bottom: 24px;
}
#sb-main .cm-editor .garden-title.garden-title h1,
#garden-layer .garden-title.garden-title h1 {
background: transparent;
padding: 0 !important;
font-size: 51px;
font-weight: 600;
line-height: 1.3;
margin: 12px 0 6px;
color: var(--editor-heading-color);
}
/* Footer */
.garden-footer {
font-size: 13px;
color: var(--garden-faint);
border-top: 1px solid var(--top-border-color);
padding-top: 18px;
margin-top: 40px;
}
/* Baked lists: hide the comment lines that hold their query */
#sb-main .cm-editor .cm-line:has(> .sb-comment:only-child) {
display: none;
}
/* The styled copy shown while SilverBullet loads */
#garden-layer {
display: flex;
flex-direction: column;
color: var(--root-color);
font-family: var(--editor-font);
transition: opacity 0.25s;
}
#garden-layer.garden-layer-done {
opacity: 0;
}
#garden-layer .garden-layer-top {
flex: none;
display: flex;
height: 55px;
background: var(--top-background-color);
border-bottom: 1px solid var(--top-border-color);
}
#garden-layer .sb-nav-root,
#garden-layer .garden-layer-top-side {
width: var(--garden-layer-side);
}
#garden-layer .garden-layer-top-main {
flex: 1;
display: flex;
align-items: center;
overflow: hidden;
scrollbar-gutter: stable;
}
#garden-layer .garden-layer-name {
box-sizing: border-box;
width: 100%;
max-width: var(--editor-width);
margin: 0 auto;
padding: 0 20px;
font-size: 28px;
font-weight: bold;
color: var(--root-color);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
#garden-layer .garden-layer-body {
flex: 1;
display: flex;
min-height: 0;
}
#garden-layer .sb-nav-root {
flex: none;
height: auto;
}
#garden-layer .garden-layer-main {
flex: 1;
overflow-y: auto;
scrollbar-gutter: stable;
}
#garden-layer .garden-layer-page {
box-sizing: border-box;
max-width: var(--editor-width);
margin: 0 auto;
padding: 5px 20px 40px;
}
#garden-layer .garden-layer-page a {
color: var(--link-color);
}
#garden-layer .garden-page > * {
margin-bottom: 0;
}
#garden-layer .garden-page > * + * {
margin-top: 24px;
}
#garden-layer .garden-page > :is(h1, h2, h3, h4, h5, h6) + *,
#garden-layer .garden-page > p + :is(ul, ol) {
margin-top: 0;
}
#garden-layer .garden-page img {
display: block;
max-width: 100%;
height: auto;
}
#garden-layer .garden-layer-date {
visibility: hidden;
margin-bottom: 6px;
}
@media (max-width: 800px) {
#garden-layer .sb-nav-root,
#garden-layer .garden-layer-top-side { display: none; }
}
space-lua code block with the sidebar, the page title and the footer. Change "My Garden" to your site's name. The script reads the sidebar's title and width from here.-- Reads a page's text; empty if it can't be read yet
function gardenPageText(name)
local ok, text = pcall(space.readPage, name)
if ok and type(text) == "string" then
return text
end
return ""
end
view.define {
name = "garden.nav",
title = "My Garden",
dock = "lhs",
openOnStart = true,
content = function()
return widget.new {
markdown = gardenPageText("Library/Garden Sidebar"),
cssClasses = {"garden-nav"},
}
end,
}
config.set("view.defaults", {
["garden.nav"] = { dock = "lhs", open = true, width = 260 },
})
local gardenMonths = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
function gardenDate(iso)
if not iso then return nil end
local s = tostring(iso)
local y, m, d = s:sub(1, 4), tonumber(s:sub(6, 7)), tonumber(s:sub(9, 10))
if not m or not d then return nil end
return d .. " " .. gardenMonths[m] .. " " .. y
end
-- Page title and "Updated" date above every page except home
view.define {
name = "garden.title",
dock = "page-top",
frame = "minimal",
content = function()
local meta = editor.getCurrentPageMeta()
if not meta or meta.name == "home" then
return nil
end
local md = "# " .. (meta.name:match("([^/]+)$") or meta.name)
local updated = gardenDate(meta.lastModified)
if updated then
md = md .. "\n\nUpdated " .. updated
end
return widget.new { markdown = md, cssClasses = {"garden-title"} }
end,
}
view.define {
name = "garden.footer",
dock = "page-bottom",
frame = "minimal",
content = function()
return widget.new {
markdown = gardenPageText("Library/Garden Footer"),
cssClasses = {"garden-footer"},
}
end,
}
space-lua block on the same page. It keeps the baked lists on home and All pages current: a few seconds after any page is saved or deleted, it rewrites them. A page you have open is left alone.-- The pages directly inside a folder, without the pages inside those
function gardenChildPages(folder)
local start = #folder + 2
return query[
from p = index.subPages(folder)
where not string.find(p.name, "/", start, true)
](<
from p = index.subPages(folder)
where not string.find(p.name, "/", start, true)
>)
end
local gardenListPages = {"home", "All pages"}
local gardenRefreshTimer = nil
function gardenRefreshLists()
gardenRefreshTimer = nil
local open = editor.getCurrentPage()
for _, name in ipairs(gardenListPages) do
if name ~= open then
local ok, err = pcall(function()
local text = space.readPage(name)
local fresh = markdown.bakeSections(text, name)
if fresh ~= text then
space.writePage(name, fresh)
end
end)
if not ok then
print("Could not refresh the list on " .. name .. ": " .. tostring(err))
end
end
end
end
local function gardenListsChanged()
if gardenRefreshTimer then
js.window.clearTimeout(gardenRefreshTimer)
end
gardenRefreshTimer = js.window.setTimeout(gardenRefreshLists, 3000)
end
event.listen { name = "page:saved", run = gardenListsChanged }
event.listen { name = "page:deleted", run = gardenListsChanged }
home page, add a baked list for each topic folder. Change "Topic One" to the folder's name:## [Topic One](<Topic One>)
<!--#lua some(query[ from p = gardenChildPages("Topic One") order by p.name select templates.pageItem(p) ](< from p = gardenChildPages("Topic One") order by p.name select templates.pageItem(p) >)) or "_No pages yet_" -->
<!--/lua-->
All pages that lists every page. Start it with a sentence, because a page that begins with the list opens with the cursor inside it:Every page, A to Z.
<!--#lua query[ from p = index.pages() where not p.name:startsWith("Library/") and p.name != "All pages" order by p.name select templates.pageItem(p) ](< from p = index.pages() where not p.name:startsWith("Library/") and p.name != "All pages" order by p.name select templates.pageItem(p) >) -->
<!--/lua-->
Run the command System: Reload, then on home and All pages run Baked Sections: Update once to fill the lists.
Now add the script to the Head HTML. In SilverBullet's dashboard, open your space's page. Open the browser's console (in most browsers, F12 or Cmd+Option+J, then the Console tab), paste this whole block and press Enter. It saves the old Head HTML in the browser first, then replaces it.
(async () => {
// The script SilverBullet will send with every page
const loader = () => {
const root = document.documentElement;
const at = (path) => new URL(path, document.baseURI);
const base = at(".").pathname;
const getText = (path, headers) =>
fetch(at(path), { headers }).then((r) => (r.ok ? r.text() : ""), () => "");
const pageHtml = (path) =>
getText(path).then((t) => {
const c = t && new DOMParser().parseFromString(t, "text/html")
.querySelector("#sb-editor .cm-content");
return c ? c.innerHTML : "";
});
const hide = document.createElement("style");
hide.textContent = "html.garden-wait #sb-root{visibility:hidden}";
document.head.append(hide);
root.classList.add("garden-wait");
const reveal = () => root.classList.remove("garden-wait");
const revealAnyway = setTimeout(reveal, 8000);
function start() {
const served = document.querySelector("#sb-editor > .cm-editor > .cm-content");
if (!served || !served.innerHTML.trim()) { reveal(); return; }
const name = decodeURIComponent(location.pathname.slice(base.length)).replace(/\/$/, "") || "home";
const isHome = name === "home";
if (!root.dataset.theme) {
root.dataset.theme = matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
}
const layer = document.createElement("div");
layer.id = "garden-layer";
layer.style.cssText = "position:fixed;inset:0;z-index:2147483000;background:var(--root-background-color)";
if (isHome) layer.classList.add("garden-home");
layer.innerHTML =
'<div class="garden-layer-top"><div class="garden-layer-top-side"></div>' +
'<div class="garden-layer-top-main"><div class="garden-layer-name"></div></div></div>' +
'<div class="garden-layer-body" style="visibility:hidden">' +
'<div class="sb-nav-root sb-nav-root-lhs sb-nav-root-content" data-slot="lhs">' +
'<div class="sb-nav-header sb-nav-no-filter"><div class="sb-nav-header-row">' +
'<label class="sb-nav-title"></label></div></div>' +
'<div class="sb-nav-body"><div class="garden-nav"></div></div></div>' +
'<div class="garden-layer-main"><div class="garden-layer-page"></div></div></div>';
const body = layer.querySelector(".garden-layer-body");
const page = layer.querySelector(".garden-layer-page");
if (!isHome) {
const title = document.createElement("div"); title.className = "garden-title";
const h1 = document.createElement("h1"); h1.textContent = name.split("/").pop();
const date = document.createElement("p"); date.className = "garden-layer-date"; date.textContent = "Updated";
title.append(h1, date); page.append(title);
}
const text = document.createElement("div"); text.className = "garden-page";
text.innerHTML = served.innerHTML;
for (const a of text.querySelectorAll("a[href]")) {
const href = a.getAttribute("href");
if (!href.startsWith(base)) continue;
const target = decodeURIComponent(href.slice(base.length));
if (a.textContent === target && target.includes("/")) a.textContent = target.split("/").pop();
}
for (const img of text.querySelectorAll("img[src]")) {
const src = at(img.getAttribute("src"));
if (src.origin === location.origin && src.pathname.startsWith(base) && !src.pathname.startsWith(base + ".fs/")) {
img.src = at(".fs/" + src.pathname.slice(base.length)).href;
}
}
const footer = document.createElement("div"); footer.className = "garden-footer";
page.append(text, footer);
document.body.append(layer);
layer.querySelector(".garden-layer-name").textContent = name;
const style = document.createElement("style");
let finished = false;
const finish = () => {
if (finished) return; finished = true;
clearTimeout(revealAnyway); reveal();
layer.classList.add("garden-layer-done");
setTimeout(() => { layer.remove(); style.remove(); hide.remove(); }, 300);
};
const show = () => { body.style.visibility = ""; };
const showAnyway = setTimeout(show, 1500);
Promise.all([
getText(".fs/Library/Garden%20Theme.md", { "X-Sync-Mode": "true" }),
pageHtml("Library/Garden%20Sidebar"),
pageHtml("Library/Garden%20Footer"),
]).then(([theme, sidebar, foot]) => {
const css = [...theme.matchAll(/```space-style[^\n]*\n([\s\S]*?)```/g)].map((m) => m[1]).join("\n");
if (!css) { finish(); return; }
style.textContent = css; document.head.append(style);
const navTitle = theme.match(/name = "garden\.nav",[\s\S]*?title = "([^"]*)"/);
const navWidth = theme.match(/\["garden\.nav"\][^}]*width = (\d+)/);
layer.querySelector(".sb-nav-title").textContent = navTitle ? navTitle[1] : "";
if (navWidth) layer.style.setProperty("--garden-layer-side", navWidth[1] + "px");
layer.querySelector(".garden-nav").innerHTML = sidebar;
footer.innerHTML = foot;
clearTimeout(showAnyway); show();
});
const startedAt = Date.now(); let readyAt = 0;
const check = setInterval(() => {
const now = Date.now();
if (!readyAt && performance.getEntriesByName("sb:widgets-ready").length > 0 &&
document.getElementById("custom-styles")?.innerHTML.trim()) readyAt = now;
const sidebarDrawn = document.querySelector("#sb-root .garden-nav") || innerWidth <= 800;
if ((readyAt && (sidebarDrawn || now - readyAt > 1500)) || now - startedAt > 60000) {
clearInterval(check); finish();
}
}, 200);
}
if (document.getElementById("custom-styles")) { start(); } else {
const watch = new MutationObserver(() => {
if (document.getElementById("custom-styles")) { watch.disconnect(); start(); }
});
watch.observe(root, { childList: true, subtree: true });
}
};
const headHtml = "<script>(" + loader + ")();</" + "script>";
const id = location.pathname.split("/")[2];
if (!id) { console.log("Stopped: open your space's page in the dashboard first."); return; }
const api = "/.dashboard/api/admin/spaces/" + id;
const old = (await (await fetch(api)).json()).headHtml || "";
localStorage.setItem("headHtml-backup", old);
const res = await fetch(api, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ headHtml }),
});
console.log(res.ok ? "Done" : "Failed: " + res.status + " " + await res.text());
})();
(async () => {
const id = location.pathname.split("/")[2];
const old = localStorage.getItem("headHtml-backup") || "";
const res = await fetch("/.dashboard/api/admin/spaces/" + id, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ headHtml: old }),
});
console.log(res.ok ? "Restored" : "Failed: " + res.status);
})();