Skip to content

k.site.styles ​

Create, read, update, delete, resolve route URLs, and inspect version logs for site CSS stylesheet resources

Execution ContextPrerequisitesTypical Use
KScript API / Server scriptsCurrent 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():

js
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 / PropertyDescriptionReturns
add(style)Creates a Style asset and registers its route (defaults to /{name})void
all()Returns all Style resources in the siteStyle[]
get(nameOrId)Retrieves a Style by name or IDStyle | null
getByUrl(url)Retrieves a Style by route URL (such as /main.css)Style | null
update(style)Persists updates on a full Style objectvoid
updateBody(nameOrId, body)Replaces only the CSS body stringvoid
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 Stylestring | null
getLogs(nameOrId) / getByLog(logId)Lists version logs or restores a snapshot by log IDChangeLog[] | 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.

ParameterTypeRequiredDescription
style.namestringyesStyle name (unique)
style.bodystringyesCSS content
style.urlstringnoAccess path, such as /assets/theme.css

Returns: void

js
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[]

js
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.

ParameterTypeRequiredDescription
nameOrIdstringyesStyle name or ID

Returns: Style | null

js
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).

ParameterTypeRequiredDescription
urlstringyesStyle route URL

Returns: Style | null

js
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).

ParameterTypeRequiredDescription
styleStyleyesModified full Style object

Returns: void

js
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.

ParameterTypeRequiredDescription
nameOrIdstringyesStyle name or ID
bodystringyesNew CSS content

Returns: void

js
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.

ParameterTypeRequiredDescription
nameOrIdstringyesStyle name or ID

Returns: void

js
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.

MethodParametersReturnsDescription
getUrl(id)id: stringstring | nullGets the relative URL
getAbsUrl(id)id: stringstring | nullGets the absolute URL

Returns: string | null

js
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.

MethodParametersReturnsDescription
getLogs(nameOrId)nameOrId: stringChangeLog[] | nullGets version logs
getByLog(logId)logId: numberStyle | nullGets a historical version by log ID

Returns: ChangeLog[] | null or Style | null

js
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 ​

FieldTypeDescription
idstringStyle ID
namestringStyle name
bodystringCSS content
extensionstringFile extension, usually css
mediastringMedia query (e.g., screen)
onlinebooleanWhether it is online
versionnumberVersion number

TypeScript Definitions ​

ts
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;
}