silverbullet/website/API/space.md

151 lines
2.8 KiB
Markdown
Raw Normal View History

2025-01-17 03:41:52 +08:00
The Space API provides functions for interacting with pages, attachments, and files in the space.
2025-01-17 17:41:02 +08:00
# Page Operations
2025-01-17 03:41:52 +08:00
2025-01-17 17:41:02 +08:00
## space.list_pages()
2025-01-17 03:41:52 +08:00
Returns a list of all pages in the space.
Example:
```lua
local pages = space.list_pages()
for page in each(pages) do
print(page.name)
end
```
2025-01-17 17:41:02 +08:00
## space.read_page(name)
2025-01-17 03:41:52 +08:00
Reads the content of a page.
Example:
```lua
local content = space.read_page("welcome")
print(content) -- prints the content of the "welcome" page
```
2025-01-17 17:41:02 +08:00
## space.get_page_meta(name)
2025-01-17 03:41:52 +08:00
Gets metadata for a specific page.
Example:
```lua
local meta = space.get_page_meta("welcome")
print(meta.name, meta.lastModified) -- prints page name and last modified date
```
2025-01-17 17:41:02 +08:00
## space.write_page(name, text)
2025-01-17 03:41:52 +08:00
Writes content to a page.
Example:
```lua
local meta = space.write_page("notes", "My new note content")
print("Page updated at: " .. meta.lastModified)
```
2025-01-17 17:41:02 +08:00
## space.delete_page(name)
2025-01-17 03:41:52 +08:00
Deletes a page from the space.
Example:
```lua
space.delete_page("old-notes")
```
2025-01-17 17:41:02 +08:00
# Attachment Operations
2025-01-17 03:41:52 +08:00
2025-01-17 17:41:02 +08:00
## space.list_attachments()
2025-01-17 03:41:52 +08:00
Returns a list of all attachments in the space.
Example:
```lua
local attachments = space.list_attachments()
for att in each(attachments) do
print(att.name, att.size)
end
```
2025-01-17 17:41:02 +08:00
## space.read_attachment(name)
2025-01-17 03:41:52 +08:00
Reads the content of an attachment.
Example:
```lua
local data = space.read_attachment("image.png")
print("Attachment size: " .. #data .. " bytes")
```
2025-01-17 17:41:02 +08:00
## space.write_attachment(name, data)
2025-01-17 03:41:52 +08:00
Writes binary data to an attachment.
Example:
```lua
local binary_data = string.char(72, 69, 76, 76, 79) -- "HELLO" in binary
local meta = space.write_attachment("test.bin", binary_data)
print("Attachment saved with size: " .. meta.size)
```
2025-01-17 17:41:02 +08:00
## space.delete_attachment(name)
2025-01-17 03:41:52 +08:00
Deletes an attachment from the space.
Example:
```lua
space.delete_attachment("old-image.png")
```
2025-01-17 17:41:02 +08:00
# File Operations
2025-01-17 03:41:52 +08:00
2025-01-17 17:41:02 +08:00
## space.list_files()
2025-01-17 03:41:52 +08:00
Returns a list of all files in the space.
Example:
```lua
local files = space.list_files()
for _, file in ipairs(files) do
print(file.name, file.size)
end
```
2025-01-17 17:41:02 +08:00
## space.get_file_meta(name)
2025-01-17 03:41:52 +08:00
Gets metadata for a specific file.
Example:
```lua
local meta = space.get_file_meta("document.txt")
print(meta.name, meta.modified, meta.size)
```
2025-01-17 17:41:02 +08:00
## space.read_file(name)
2025-01-17 03:41:52 +08:00
Reads the content of a file.
Example:
```lua
local content = space.read_file("document.txt")
print("File size: " .. #content .. " bytes")
```
2025-01-17 17:41:02 +08:00
## space.write_file(name, data)
2025-01-17 03:41:52 +08:00
Writes binary data to a file.
Example:
```lua
local text = "Hello, World!"
local meta = space.write_file("greeting.txt", text)
print("File written with size: " .. meta.size)
```
2025-01-17 17:41:02 +08:00
## space.delete_file(name)
2025-01-17 03:41:52 +08:00
Deletes a file from the space.
Example:
```lua
space.delete_file("old-document.txt")
```
2025-01-17 17:41:02 +08:00
## space.file_exists(name)
2025-01-17 03:41:52 +08:00
Checks if a file exists in the space.
Example:
```lua
if space.file_exists("config.json") then
print("Config file exists!")
else
print("Config file not found")
end
```