Skip to content

/api/snippet

getSnippet

  • 🛠 获取代码片段
  • /api/snippet/getSnippet

请求

ts
/**
 * Get code snippet list
 */
export interface IPayload {
    /**
     * Get code snippets of a enabled state
     * 0: disabled
     * 1: enabled
     * 2: all
     */
    readonly enabled: number;
    /**
     * Get code snippets of a specified type
     */
    readonly type: TSnippetType;
}

/**
 * Get code snippets of a specified type
 */
export type TSnippetType = "all" | "css" | "js";
json5
/**
 * schemas/kernel/api/snippet/getSnippet/payload.schema.json5
 * 获取代码片段列表
 * REF: https://github.com/siyuan-note/siyuan/blob/v2.9.7/kernel/api/snippet.go#L58-L94
 * @pathname: /api/snippet/getSnippet
 * @version: 2.9.7
 */
{
    $schema: 'https://json-schema.org/draft/2020-12/schema',
    $id: 'https://github.com/siyuan-community/siyuan-sdk/raw/main/schemas/kernel/api/snippet/getSnippet/payload.schema.json5',
    $comment: 'v2.9.7',
    $ref: '#/$defs/root',
    $defs: {
        root: {
            title: 'payload body',
            description: 'Get code snippet list',

            type: 'object',
            additionalProperties: false,
            required: [
                'type',
                'enabled',
            ],
            properties: {
                type: {
                    // 获取指定类型的代码片段
                    title: 'TSnippetType',
                    type: 'string',
                    description: 'Get code snippets of a specified type',
                    enum: [
                        'js', // 获取 js 代码片段
                        'css', // 获取 css 代码片段
                        'all', // 获取所有类型的代码判断
                    ],
                },
                enabled: {
                    // 获取指定启用状态的代码片段
                    type: 'integer',
                    description: 'Get code snippets of a enabled state\n0: disabled\n1: enabled\n2: all',
                    enum: [
                        0, // 禁用的代码片段
                        1, // 启用的代码片段
                        2, // 所有状态的代码片段
                    ],
                },
            },
        },
    },
}
json
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://github.com/siyuan-community/siyuan-sdk/raw/main/schemas/kernel/api/snippet/getSnippet/payload.schema.json",
    "$comment": "v2.9.7",
    "$ref": "#/$defs/root",
    "$defs": {
        "root": {
            "title": "payload body",
            "description": "Get code snippet list",
            "type": "object",
            "additionalProperties": false,
            "required": [
                "type",
                "enabled"
            ],
            "properties": {
                "type": {
                    "title": "TSnippetType",
                    "type": "string",
                    "description": "Get code snippets of a specified type",
                    "enum": [
                        "js",
                        "css",
                        "all"
                    ]
                },
                "enabled": {
                    "type": "integer",
                    "description": "Get code snippets of a enabled state\n0: disabled\n1: enabled\n2: all",
                    "enum": [
                        0,
                        1,
                        2
                    ]
                }
            }
        }
    }
}

响应

ts
/**
 * Get code snippet list
 */
export interface IResponse {
    /**
     * status code
     */
    readonly code: number;
    readonly data: IData;
    /**
     * status message
     */
    readonly msg: string;
}

/**
 * response data
 */
export interface IData {
    readonly snippets: ISnippet[];
}

/**
 * code snippet
 */
export interface ISnippet {
    /**
     * snippet content
     */
    readonly content: string;
    /**
     * snippet enable status
     */
    readonly enabled: boolean;
    /**
     * snippet ID
     */
    readonly id: string;
    /**
     * snippet memo
     */
    readonly memo?: string;
    /**
     * snippet name
     */
    readonly name: string;
    /**
     * snippet type
     */
    readonly type: TSnippetType;
}

/**
 * snippet type
 */
export type TSnippetType = "css" | "js";
json5
/**
 * schemas/kernel/api/snippet/getSnippet/response.schema.json5
 * 获取代码片段列表
 * REF: https://github.com/siyuan-note/siyuan/blob/v2.10.15/kernel/api/snippet.go#L58-L94
 * @pathname: /api/snippet/getSnippet
 * @version: 2.10.15
 */
{
    $schema: 'https://json-schema.org/draft/2020-12/schema',
    $id: 'https://github.com/siyuan-community/siyuan-sdk/raw/main/schemas/kernel/api/snippet/getSnippet/response.schema.json5',
    $comment: 'v2.10.15',
    $ref: '#/$defs/root',
    $defs: {
        root: {
            title: 'response body',
            description: 'Get code snippet list',

            type: 'object',
            additionalProperties: false,
            required: [
                'code',
                'msg',
                'data',
            ],
            properties: {
                code: {
                    type: 'integer',
                    description: 'status code',
                },
                msg: {
                    type: 'string',
                    description: 'status message',
                },
                data: {
                    $ref: '#/$defs/data',
                },
            },
        },
        data: {
            title: 'IData',
            description: 'response data',

            type: 'object',
            additionalProperties: false,
            required: [
                'snippets',
            ],
            properties: {
                snippets: {
                    type: 'array',
                    items: {
                        $ref: '#/$defs/snippet',
                    },
                },
            },
        },
        snippet: {
            // 一个代码片段
            title: 'ISnippet',
            description: 'code snippet',

            type: 'object',
            additionalProperties: false,
            required: [
                'id',
                'name',
                'type',
                'enabled',
                'content',
            ],
            properties: {
                id: {
                    // 代码片段 ID
                    type: 'string',
                    description: 'snippet ID',
                    pattern: '^\\d{14}-[0-9a-z]{7}$',
                },
                name: {
                    // 代码片段名称
                    type: 'string',
                    description: 'snippet name',
                },
                memo: {
                    // 代码片段备注
                    type: 'string',
                    description: 'snippet memo',
                },
                type: {
                    // 代码片段类型
                    title: 'TSnippetType',
                    type: 'string',
                    description: 'snippet type',
                    enum: [
                        'js', // JavaScript 片段
                        'css', // CSS 片段
                    ],
                },
                enabled: {
                    // 代码片段是否启用
                    type: 'boolean',
                    description: 'snippet enable status',
                },
                content: {
                    // 代码片段内容
                    type: 'string',
                    description: 'snippet content',
                },
            },
        },
    },
}
json
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://github.com/siyuan-community/siyuan-sdk/raw/main/schemas/kernel/api/snippet/getSnippet/response.schema.json",
    "$comment": "v2.10.15",
    "$ref": "#/$defs/root",
    "$defs": {
        "root": {
            "title": "response body",
            "description": "Get code snippet list",
            "type": "object",
            "additionalProperties": false,
            "required": [
                "code",
                "msg",
                "data"
            ],
            "properties": {
                "code": {
                    "type": "integer",
                    "description": "status code"
                },
                "msg": {
                    "type": "string",
                    "description": "status message"
                },
                "data": {
                    "$ref": "#/$defs/data"
                }
            }
        },
        "data": {
            "title": "IData",
            "description": "response data",
            "type": "object",
            "additionalProperties": false,
            "required": [
                "snippets"
            ],
            "properties": {
                "snippets": {
                    "type": "array",
                    "items": {
                        "$ref": "#/$defs/snippet"
                    }
                }
            }
        },
        "snippet": {
            "title": "ISnippet",
            "description": "code snippet",
            "type": "object",
            "additionalProperties": false,
            "required": [
                "id",
                "name",
                "type",
                "enabled",
                "content"
            ],
            "properties": {
                "id": {
                    "type": "string",
                    "description": "snippet ID",
                    "pattern": "^\\d{14}-[0-9a-z]{7}$"
                },
                "name": {
                    "type": "string",
                    "description": "snippet name"
                },
                "memo": {
                    "type": "string",
                    "description": "snippet memo"
                },
                "type": {
                    "title": "TSnippetType",
                    "type": "string",
                    "description": "snippet type",
                    "enum": [
                        "js",
                        "css"
                    ]
                },
                "enabled": {
                    "type": "boolean",
                    "description": "snippet enable status"
                },
                "content": {
                    "type": "string",
                    "description": "snippet content"
                }
            }
        }
    }
}

setSnippet

  • 🛠 设置代码片段
  • /api/snippet/setSnippet

请求

ts
/**
 * Set code snippet list
 */
export interface IPayload {
    /**
     * snippet list
     */
    readonly snippets: ISnippet[];
}

/**
 * code snippet
 */
export interface ISnippet {
    /**
     * snippet content
     */
    readonly content: string;
    /**
     * snippet enable status
     */
    readonly enabled: boolean;
    /**
     * snippet ID
     */
    readonly id: string;
    /**
     * snippet name
     */
    readonly name: string;
    /**
     * snippet type
     */
    readonly type: TSnippetType;
}

/**
 * snippet type
 */
export type TSnippetType = "css" | "js";
json5
/**
 * schemas/kernel/api/snippet/setSnippet/payload.schema.json5
 * 设置代码片段列表
 * REF: https://github.com/siyuan-note/siyuan/blob/v2.9.7/kernel/api/snippet.go#L96-L128
 * @pathname: /api/snippet/setSnippet
 * @version: 2.9.7
 */
{
    $schema: 'https://json-schema.org/draft/2020-12/schema',
    $id: 'https://github.com/siyuan-community/siyuan-sdk/raw/main/schemas/kernel/api/snippet/setSnippet/payload.schema.json5',
    $comment: 'v2.9.7',
    $ref: '#/$defs/root',
    $defs: {
        root: {
            title: 'payload body',
            description: 'Set code snippet list',

            type: 'object',
            additionalProperties: false,
            required: [
                'snippets',
            ],
            properties: {
                snippets: {
                    // 代码片段列表
                    type: 'array',
                    description: 'snippet list',
                    items: {
                        $ref: '#/$defs/snippet',
                    },
                },
            },
        },
        snippet: {
            // 一个代码片段
            title: 'ISnippet',
            description: 'code snippet',

            type: 'object',
            additionalProperties: false,
            required: [
                'id',
                'name',
                'type',
                'enabled',
                'content',
            ],
            properties: {
                id: {
                    // 代码片段 ID
                    type: 'string',
                    description: 'snippet ID',
                    oneOf: [
                        {
                            const: '', // 新的代码片段, ID 应设置为空 (内核自动生成)
                        },
                        {
                            pattern: '^\\d{14}-[0-9a-z]{7}$',
                        },
                    ],
                },
                name: {
                    // 代码片段名称
                    type: 'string',
                    description: 'snippet name',
                },
                type: {
                    // 代码片段类型
                    title: 'TSnippetType',
                    type: 'string',
                    description: 'snippet type',
                    enum: [
                        'js', // JavaScript 片段
                        'css', // CSS 片段
                    ],
                },
                enabled: {
                    // 代码片段是否启用
                    type: 'boolean',
                    description: 'snippet enable status',
                },
                content: {
                    // 代码片段内容
                    type: 'string',
                    description: 'snippet content',
                },
            },
        },
    },
}
json
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://github.com/siyuan-community/siyuan-sdk/raw/main/schemas/kernel/api/snippet/setSnippet/payload.schema.json",
    "$comment": "v2.9.7",
    "$ref": "#/$defs/root",
    "$defs": {
        "root": {
            "title": "payload body",
            "description": "Set code snippet list",
            "type": "object",
            "additionalProperties": false,
            "required": [
                "snippets"
            ],
            "properties": {
                "snippets": {
                    "type": "array",
                    "description": "snippet list",
                    "items": {
                        "$ref": "#/$defs/snippet"
                    }
                }
            }
        },
        "snippet": {
            "title": "ISnippet",
            "description": "code snippet",
            "type": "object",
            "additionalProperties": false,
            "required": [
                "id",
                "name",
                "type",
                "enabled",
                "content"
            ],
            "properties": {
                "id": {
                    "type": "string",
                    "description": "snippet ID",
                    "oneOf": [
                        {
                            "const": ""
                        },
                        {
                            "pattern": "^\\d{14}-[0-9a-z]{7}$"
                        }
                    ]
                },
                "name": {
                    "type": "string",
                    "description": "snippet name"
                },
                "type": {
                    "title": "TSnippetType",
                    "type": "string",
                    "description": "snippet type",
                    "enum": [
                        "js",
                        "css"
                    ]
                },
                "enabled": {
                    "type": "boolean",
                    "description": "snippet enable status"
                },
                "content": {
                    "type": "string",
                    "description": "snippet content"
                }
            }
        }
    }
}

响应

ts
export interface IResponse {
    /**
     * status code
     */
    readonly code: number;
    readonly data: null;
    /**
     * status message
     */
    readonly msg: string;
}
json5
/**
 * schemas/kernel/api/snippet/setSnippet/response.schema.json5
 * 设置代码片段列表
 * REF: https://github.com/siyuan-note/siyuan/blob/v2.9.7/kernel/api/snippet.go#L96-L128
 * @pathname: /api/snippet/setSnippet
 * @version: 2.9.7
 */
{
    $schema: 'https://json-schema.org/draft/2020-12/schema',
    $id: 'https://github.com/siyuan-community/siyuan-sdk/raw/main/schemas/kernel/api/snippet/setSnippet/response.schema.json5',
    $comment: 'v2.9.7',
    $ref: '#/$defs/root',
    $defs: {
        root: {
            title: 'response body',
            description: '',

            type: 'object',
            additionalProperties: false,
            required: [
                'code',
                'msg',
                'data',
            ],
            properties: {
                code: {
                    type: 'integer',
                    description: 'status code',
                },
                msg: {
                    type: 'string',
                    description: 'status message',
                },
                data: {
                    type: 'null',
                },
            },
        },
    },
}
json
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://github.com/siyuan-community/siyuan-sdk/raw/main/schemas/kernel/api/snippet/setSnippet/response.schema.json",
    "$comment": "v2.9.7",
    "$ref": "#/$defs/root",
    "$defs": {
        "root": {
            "title": "response body",
            "description": "",
            "type": "object",
            "additionalProperties": false,
            "required": [
                "code",
                "msg",
                "data"
            ],
            "properties": {
                "code": {
                    "type": "integer",
                    "description": "status code"
                },
                "msg": {
                    "type": "string",
                    "description": "status message"
                },
                "data": {
                    "type": "null"
                }
            }
        }
    }
}