Skip to content

Commit 0f6f06a

Browse files
committed
Website docs refactoring (Lua, mostly)
1 parent 1402245 commit 0f6f06a

File tree

13 files changed

+406
-140
lines changed

13 files changed

+406
-140
lines changed

STYLE.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
A few notes on coding conventions used in this project.
2+
3+
# Tooling
4+
5+
Run these commands to perform type and style checks and automatically reformat code based on conventions:
6+
7+
```bash
8+
make check # Type check + lint frontend and backend
9+
make fmt # Format all code
10+
```
11+
12+
# Code Style Guidelines
13+
14+
## TypeScript (client and plugs)
15+
16+
### Import Organization
17+
18+
```typescript
19+
// Use relative imports by default (include .ts extension)
20+
import { Space } from "./space.ts";
21+
// Use `type` for type-only imports (part of the lint rules)
22+
import type { Command } from "./types/command.ts";
23+
24+
// Use package-prefixed absolute when available (defined in deno.json)
25+
import { sleep } from "@silverbulletmd/silverbullet/lib/async";
26+
```
27+
28+
### TypeScript Conventions
29+
* Use `type` over `interface` for object shapes
30+
* Use `type` for unions and complex types
31+
* Always type function parameters explicitly
32+
* Type return values for public APIs
33+
* Let TypeScript infer obvious variable types
34+
* Use `any` for error objects in catch blocks
35+
36+
37+
### Naming Conventions
38+
- **Variables & functions:** `camelCase`
39+
- **Classes:** `PascalCase`
40+
- **Files:** `snake_case.ts` (e.g., `http_space_primitives.ts`)
41+
- **Test files:** `*.test.ts` alongside source files
42+
- **Types:** `PascalCase` (e.g., `PageMeta`, `SpacePrimitives`)
43+
- **Constants:** `camelCase`
44+
45+
46+
### Testing Patterns
47+
TypeScript tests run with Deno.
48+
49+
**Test structure:**
50+
```typescript
51+
Deno.test("Test description in plain English", async () => {
52+
// Setup
53+
const kv = new MemoryKvPrimitives();
54+
const space = new Space(new DataStoreSpacePrimitives(kv), eventHook);
55+
56+
// Execute
57+
await space.writePage("test", testPage);
58+
59+
// Assert
60+
assertEquals(await space.readPage("test"), testPage);
61+
});
62+
```
63+
64+
**Assertions:**
65+
```typescript
66+
import { assertEquals, assertNotEquals } from "@std/assert";
67+
68+
assertEquals(actual, expected);
69+
assertNotEquals(actual, notExpected);
70+
```
71+
72+
**Test configuration:**
73+
```typescript
74+
Deno.test("Test with options", {
75+
sanitizeResources: false,
76+
sanitizeOps: false,
77+
}, async () => {
78+
// test code
79+
});
80+
```
81+
82+
### Comments & Documentation
83+
84+
Use JSDoc for public APIs:
85+
```typescript
86+
/**
87+
* Lists all pages (files ending in .md) in the space.
88+
* @param unfiltered - Whether to include filtered pages
89+
* @returns A list of all pages represented as PageMeta objects
90+
*/
91+
export function listPages(unfiltered?: boolean): Promise<PageMeta[]> {
92+
return syscall("space.listPages", unfiltered);
93+
}
94+
```
95+
96+
Inline comments:
97+
* In case of doubt: add comments around the _why_ of the code (not what)
98+
* Add TODO comments for known issues
99+
100+
```typescript
101+
// Note: these events are dispatched asynchronously (not waiting for results)
102+
this.eventHook.dispatchEvent(...);
103+
104+
// TODO: Clean this up, this has become a god class
105+
export class Client {
106+
```
107+
108+
### Code Patterns
109+
110+
**Definite assignment for late initialization:**
111+
```typescript
112+
class Client {
113+
space!: Space; // Initialized in init()
114+
editorView!: EditorView;
115+
}
116+
```
117+
118+
**Destructuring:**
119+
```typescript
120+
const { name, def } = command;
121+
const { text, meta } = await this.space.readPage(name);
122+
```
123+
124+
## Go (server)
125+
Follow common Go conventions. `make fmt` also reformats Go code.
126+
127+
## Lua (libraries)
128+
See `website/Space Lua/Conventions`

website/API/dom.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
An API to easily build DOM objects through the magic of Lua meta tables.
2+
3+
# Usage
4+
5+
```lua
6+
-- any HTML tag can be used here
7+
dom.span {
8+
-- tag attributes can be set like this:
9+
class = "my-class",
10+
id = "my-id",
11+
-- Plain text body elements can be added like this
12+
"Span content",
13+
-- And elements can be nested
14+
dom.strong { "I am strong" },
15+
-- Widgets can also be embedded
16+
widget.html "<b>Bold</b>",
17+
widget.html(dom.marquee { "nested widget" })
18+
}
19+
```
20+
21+
Example:
22+
${widget.html(dom.marquee {
23+
"I'm in a ",
24+
dom.span {
25+
style="color:red;",
26+
"marquee"
27+
}
28+
})}
29+
30+
# API
31+
## dom.* {attribute1=value, attribute2=value, childElement1, childElement2}
32+
Renders a HTML DOM.
33+
34+
* `attribute=value` key/value mappings are translated to HTML DOM attribtutes.
35+
* Plain text elements such as `"Hello **world**"` are parsed and rendered as markdown translated to HTML.
36+
* DOM elements (for instance those resulting from additional `dom.*` calls) are injected in place.
37+
* [[API/widget|widgets]] are rendered in place.
38+
39+
For instance:
40+
41+
```lua
42+
dom.span {
43+
class = "class-attribute",
44+
dom.span {
45+
"A first nested span"
46+
},
47+
dom.span {
48+
"A second nested span"
49+
},
50+
}
51+
```
52+
53+
Would be roughly equivalent to the following HTML:
54+
55+
```html
56+
<span class="class-attribute">
57+
<span>A first nested span</span>
58+
<span>A second nested span</span>
59+
</span>
60+
```
61+
62+
This API is implemented using Lua metatables, its implementation lives here: [[^Library/Std/APIs/DOM]]

website/API/tag.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Enables customization of [[Tag|tags]] and its [[Object|objects]] in a few ways:
66
* Change how objects are indexed in the [[Object Index]]
77
* Tweak styling of tags in the editor using [[Space Style]]
88

9+
> **success** Success
10+
> Put all your `tag.define` calls in your [[CONFIG]] page so that they will be picked up during the index process.
11+
912
# API
1013
## tag.define(spec)
1114
Defines a tag explicitly.

website/API/widget.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
APIs to define widgets in SilverBullet, often used through [[Space Lua#Expressions]].
2+
3+
# Widget types
4+
## Markdown widgets
5+
When setting a `markdown` key, or using the `widget.markdown` API, a markdown-based widget can be created.
6+
7+
Example:
8+
9+
```space-lua
10+
function helloWorld(name)
11+
return widget.markdown("Hello world, *" .. name .. "*!")
12+
end
13+
```
14+
15+
Can be used as follows:
16+
17+
${helloWorld("Pete")}
18+
19+
## DOM widgets
20+
To render a custom HTML-based widget, use the [[API/dom]] elements passed as an argument to `widget.html`:
21+
22+
```space-lua
23+
function marquee(text)
24+
return widget.html(dom.marquee {
25+
class = "my-marquee",
26+
onclick = function()
27+
editor.flashNotification "You clicked me"
28+
end,
29+
text
30+
})
31+
end
32+
```
33+
34+
We can combine this with some [[Space Style]] to style it:
35+
36+
```space-style
37+
.my-marquee {
38+
color: purple;
39+
}
40+
```
41+
42+
This can be used as follows:
43+
${marquee "Finally, marqeeeeeee!"}
44+
# API
45+
## widget.new(spec)
46+
To render a widget, call `widget.new` with a `spec` table setting any of the following keys:
47+
48+
* `markdown`: Renders the value as markdown
49+
* `html`: Renders a HTML DOM as a widget. It is usually used in conjunction with the [[API/dom]] API.
50+
* `display`: Render the value either `inline` or as a `block` (defaults to `inline`).
51+
52+
## widget.markdown(text)
53+
Shortcut for `widget.new { markdown = text }`
54+
55+
## widget.html(htmlOrDOM)
56+
Shortcut for `widget.new { html = htmlOrDOM }`
57+
58+
Usually used in conjunction with [[API/dom]].

website/CHANGELOG.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,23 @@ Whenever a commit is pushed to the `main` branch, within ~10 minutes, it will be
2222
* `Page: Rename` keyboard shortcut removed
2323
* `Page: Rename Linked Page` keyboard shortcut removed
2424
* `Sync: Space` keyboard shortcut removed
25+
* Sync reliability work:
26+
* Better indication whether your page is synced to the server: “Dirty state” (slightly tinted color of page name) is now aligned with actual synced-to-server state _unless_ the editor clearly indicates it is in offline mode (yellow top bar).
27+
* Sync snapshots are now persisted after every file sync, reducing (and hopefully eliminating) edge cases where the sync engine is killed mid-sync (for whatever reason) and the snapshot becomes of sync with “reality”.
28+
* The index status progress indicator (blue circle) should now be more reliably reflect the actual indexing status.
29+
* Tag schema updates:
30+
* `pos` (present in link, item and some other tags) is now _deprecated_, use `range` instead
31+
* `range` is a tuple of two numbers: _from_ and _to_ (e.g. `{0, 10}`) identify where the object appears in the page
2532
* Lua engine improvements (courtesy of [Matouš Jan Fialka](https://github.com/mjf)):
2633
* [Support for `<close>` attribute and __close metamethod](https://github.com/silverbulletmd/silverbullet/commit/9419cdcd9be61908330e1dce68a9156dbb911d23)
2734
* [Better arithmetic error messages](https://github.com/silverbulletmd/silverbullet/commit/5a20a5f8f476a98172609e80c799cd1d83765585)
2835
* [Refactor of control flow (performance)](https://github.com/silverbulletmd/silverbullet/commit/e5b4c8feb22a44cb4b22b3a77f9f2ed21dd09297)
2936
* [Improved numeric type semantics](https://github.com/silverbulletmd/silverbullet/pull/1803)
3037
* Lua APIs:
3138
* [[API/table#table.select(table, keys...)]] (non-standard in Lua) API, convenient to use in [[Space Lua/Lua Integrated Query]] `select` clauses, see example in docs.
32-
* Sync reliability work:
33-
* Better indication whether your page is synced to the server: “Dirty state” (slightly tinted color of page name) is now aligned with actual synced-to-server state _unless_ the editor clearly indicates it is in offline mode (yellow top bar).
34-
* Sync snapshots are now persisted after every file sync, reducing (and hopefully eliminating) edge cases where the sync engine is killed mid-sync (for whatever reason) and the snapshot becomes of sync with “reality”.
35-
* The index status progress indicator (blue circle) should now be more reliably reflect the actual indexing status.
3639
* Library manager: SilverBullet now navigates to library page after installing one.
40+
* Styling changes:
41+
* Attribute names and values ([key: value] notation) now get different CSS classes in the editor: `sb-attribute-name` for names and `sb-attribute-value` for values.
3742
* New `Page: Create Under Cursor` command, useful to pre-create an aspiring page link. Put your cursor in a wiki link to a non-existing page, and hit `Cmd-Shift-Enter` (`Ctrl-Shift-Enter`) to create it (empty) without navigating there.
3843
* Authentication:
3944
* How long “remember me” works is now configurable (by [Metin Yazici](https://github.com/silverbulletmd/silverbullet/pull/1796)) via [[Install/Configuration]] and more reliably persisted.

website/Object Index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ You interact with it in a few ways:
77

88
# Indexing
99
## Initial indexing process
10-
When you launch a fresh client for the first time, the object index will be built from scratch. Depending on the size of your space this can take anything between a few seconds and minutes. If the process takes longer than a few seconds, you will see progress with a blue status circle. Until this initial indexing process finishes, you will notice that things like [[Space Lua/Widget]] and [[Space Lua/Lua Integrated Query]] are not yet rendered, this is to avoid errors and invalid data.
10+
When you launch a fresh client for the first time, the object index will be built from scratch. Depending on the size of your space this can take anything between a few seconds and minutes. If the process takes longer than a few seconds, you will see progress with a blue status circle. Until this initial indexing process finishes, you will notice that things like [[API/widget|Widgets]] and [[Space Lua/Lua Integrated Query]] are not yet rendered, this is to avoid errors and invalid data.
1111

1212
After the initial index process, the index will be kept up-to-date incrementally.
1313

website/Object/space-lua.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Every `space-lua` block across your space is indexed with the `space-lua` tag. Upon client boot (or after using `System: Reload`), all space lua scripts are executed in sequence.

website/SilverBullet.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ In SilverBullet you keep your content as a collection of [[Markdown]] [[Page|Pag
99

1010
If you are the **writer** type, you’ll appreciate SilverBullet as a clean [[Markdown]] editor with [[Live Preview]]. If you have more of an **outliner** personality, SilverBullet has [[Outlines|Outlining]] tools for you. Productivity freak? Have a look at [[Task|Tasks]]. More of a **database** person? You will appreciate [[Object|Objects]] and [[Space Lua/Lua Integrated Query|Queries]].
1111

12-
And if you are comfortable **programming** a little bit — now we’re really talking. You will love _dynamically generating content_ with [[Space Lua]] (SilverBullet’s [[Lua]] dialect), or to use it to create custom [[Command|Commands]], [[Page Template|Page Templates]] or [[Space Lua/Widget|Widgets]].
12+
And if you are comfortable **programming** a little bit — now we’re really talking. You will love _dynamically generating content_ with [[Space Lua]] (SilverBullet’s [[Lua]] dialect), or to use it to create custom [[Command|Commands]], [[Page Template|Page Templates]] or [[API/widget|Widgets]].
1313

1414
# Programmable notes
1515
Dynamically generating content, _programmable notes_... why would you want that, and how does it work?

0 commit comments

Comments
 (0)