k.site.styles
Create, read, update, delete, resolve route URLs, and inspect version logs for site CSS stylesheet resources
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API / Server scripts | Current site context; sidebar item "css" maps to this API (inline styles lack routes) | Manage standalone routable .css stylesheet assets, update CSS bodies, and check change logs |
Quick Start
Create a standalone CSS asset with a registered URL route and verify it via getByUrl() and getUrl():
k.api.post(() => {
const stamp = Date.now().toString()
const name = "ai-style-" + stamp
const url = "/ai-style-" + stamp + ".css"
k.site.styles.add({
name,
url,
body: `.ai-style-${stamp} { color: #333; }`
})
const style = k.site.styles.getByUrl(url)
return {
verified: !!style && style.name === name,
id: style?.id,
route: style ? k.site.styles.getUrl(style.id) : null
}
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
add(style) | Creates a Style asset and registers its route (defaults to /{name}) | void |
all() | Returns all Style resources in the site | Style[] |
get(nameOrId) | Retrieves a Style by name or ID | Style | null |
getByUrl(url) | Retrieves a Style by route URL (such as /main.css) | Style | null |
update(style) | Persists updates on a full Style object | void |
updateBody(nameOrId, body) | Replaces only the CSS body string | void |
delete(nameOrId) | Deletes a Style by name or ID (verify with get) | void |
getUrl(id) / getAbsUrl(id) | Returns relative or absolute URL for a routed Style | string | null |
getLogs(nameOrId) / getByLog(logId) | Lists version logs or restores a snapshot by log ID | ChangeLog[] | null / Style | null |
Differences from layouts and views
k.site.styles supports getByUrl, getUrl, getAbsUrl and other routing-related APIs (similar to k.site.pages); k.site.layouts and k.site.views have no routing API.
Detailed Methods
add()
Create styles and register routes. If url is not passed, it defaults to /{name}, and if the path does not contain an extension, the style extension (usually .css) is appended automatically.
| Parameter | Type | Required | Description |
|---|---|---|---|
style.name | string | yes | Style name (unique) |
style.body | string | yes | CSS content |
style.url | string | no | Access path, such as /assets/theme.css |
Returns: void
k.api.post(() => {
const name = "theme-overrides-" + Date.now()
k.site.styles.add({
name,
url: "/assets/" + name + ".css",
body: "body { line-height: 1.6; }"
})
const created = k.site.styles.get(name)
return { id: created?.id, route: created ? k.site.styles.getUrl(created.id) : null }
})all()
Returns all styles.
None
Returns: Style[]
k.api.get(() => {
const styles = k.site.styles.all()
return { count: styles.length, names: styles.slice(0, 10).map((s) => s.name) }
})get()
Get a style by name or ID.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | string | yes | Style name or ID |
Returns: Style | null
k.api.get(() => {
const style = k.site.styles.get("site")
return style ? { id: style.id, name: style.name, extension: style.extension } : null
})getByUrl()
Get the style by routing URL (e.g. /main.css).
| Parameter | Type | Required | Description |
|---|---|---|---|
url | string | yes | Style route URL |
Returns: Style | null
k.api.get(() => {
const style = k.site.styles.getByUrl("/main.css")
return style ? { id: style.id, name: style.name } : null
})update()
Update the complete style object (usually get / getByUrl first and then body).
| Parameter | Type | Required | Description |
|---|---|---|---|
style | Style | yes | Modified full Style object |
Returns: void
k.api.post(() => {
const stamp = Date.now().toString()
const name = "ai-style-upd-" + stamp
const url = "/ai-style-upd-" + stamp + ".css"
k.site.styles.add({ name, url, body: ".v1 {}" })
const style = k.site.styles.getByUrl(url)
style.body = ".v2 {}"
k.site.styles.update(style)
const after = k.site.styles.getByUrl(url)
return {
verified: after?.body?.indexOf("v2") >= 0,
hasV1: after?.body?.indexOf("v1") >= 0
}
})updateBody()
Only updates body.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | string | yes | Style name or ID |
body | string | yes | New CSS content |
Returns: void
k.api.post(() => {
const stamp = Date.now().toString()
const name = "ai-style-body-" + stamp
const url = "/ai-style-body-" + stamp + ".css"
k.site.styles.add({ name, url, body: ".before {}" })
k.site.styles.updateBody(name, ".after-body {}")
const after = k.site.styles.getByUrl(url)
return { verified: after?.body?.indexOf("after-body") >= 0 }
})delete()
Delete by Name or ID. Use get(name) for verification after deletion. Do not rely on getByUrl after deletion.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | string | yes | Style name or ID |
Returns: void
k.api.post(() => {
const stamp = Date.now().toString()
const name = "ai-style-del-" + stamp
const url = "/ai-style-del-" + stamp + ".css"
k.site.styles.add({ name, url, body: ".del {}" })
const before = k.site.styles.getByUrl(url)
k.site.styles.delete(name)
const after = k.site.styles.get(name)
return {
verified: !!before && !after,
hadBefore: !!before
}
})getUrl() / getAbsUrl()
Only styles with registered routes can return URLs; styles embedded in a page may not have independent routes.
| Method | Parameters | Returns | Description |
|---|---|---|---|
getUrl(id) | id: string | string | null | Gets the relative URL |
getAbsUrl(id) | id: string | string | null | Gets the absolute URL |
Returns: string | null
k.api.get(() => {
const styles = k.site.styles.all()
for (const style of styles) {
const rel = k.site.styles.getUrl(style.id)
if (!rel) continue
const abs = k.site.styles.getAbsUrl(style.id)
return { relative: rel, absolute: abs }
}
return null
})getLogs() / getByLog()
Inspect stylesheet change logs or restore a snapshot by log entry ID.
| Method | Parameters | Returns | Description |
|---|---|---|---|
getLogs(nameOrId) | nameOrId: string | ChangeLog[] | null | Gets version logs |
getByLog(logId) | logId: number | Style | null | Gets a historical version by log ID |
Returns: ChangeLog[] | null or Style | null
k.api.get(() => {
const styles = k.site.styles.all()
if (!styles.length) return { error: "no styles" }
const logs = k.site.styles.getLogs(styles[0].id)
return {
verified: logs === null || Array.isArray(logs),
logCount: logs ? logs.length : 0
}
})Common Style Fields
| Field | Type | Description |
|---|---|---|
id | string | Style ID |
name | string | Style name |
body | string | CSS content |
extension | string | File extension, usually css |
media | string | Media query (e.g., screen) |
online | boolean | Whether it is online |
version | number | Version number |
TypeScript Definitions
interface StyleRepository {
add(style: StyleInput): void;
all(): Style[];
get(nameOrId: string): Style | null;
getByUrl(url: string): Style | null;
update(style: Style): void;
updateBody(nameOrId: string, body: string): void;
delete(nameOrId: string): void;
getUrl(id: string): string | null;
getAbsUrl(id: string): string | null;
getLogs(nameOrId: string): ChangeLog[] | null;
getByLog(logId: number): Style | null;
}
interface StyleInput {
name: string;
body: string;
url?: string;
}