安装code-input插件后Sanity CMS无法添加代码块,求配置位置指引
code-input插件配置位置指引
- 核心修改文件为
sanityblog/blockContent.js,无需修改schema.js文件 - 逻辑说明:你的博客正文使用自定义
blockContent类型承载富文本内容,该类型为数组结构,of属性定义了富文本编辑器支持插入的所有内容模块(包含普通文字块、图片等现有类型),将代码块配置加入这个数组即可实现代码插入能力。
具体修改操作
- 打开
blockContent.js,定位到of数组的代码位置 - 将code-input插件要求的类型配置,作为数组元素和现有
block文字块、image图片配置平级放置即可。修改完成后的完整文件代码如下:
/** * This is the schema definition for the rich text fields used for * for this blog studio. When you import it in schemas.js it can be * reused in other parts of the studio with: * { * name: 'someName', * title: 'Some title', * type: 'blockContent' * } */ export default { title: "Block Content", name: "blockContent", type: "array", of: [ { title: "Block", type: "block", // Styles let you set what your user can mark up blocks with. These // correspond with HTML tags, but you can set any title or value // you want and decide how you want to deal with it where you want to // use your content. styles: [ { title: "Normal", value: "normal" }, { title: "H1", value: "h1" }, { title: "H2", value: "h2" }, { title: "H3", value: "h3" }, { title: "H4", value: "h4" }, { title: "Quote", value: "blockquote" }, ], lists: [{ title: "Bullet", value: "bullet" }], // Marks let you mark up inline text in the block editor. marks: { // Decorators usually describe a single property – e.g. a typographic // preference or highlighting by editors. decorators: [ { title: "Strong", value: "strong" }, { title: "Emphasis", value: "em" }, ], // Annotations can be any object structure – e.g. a link or a footnote. annotations: [ { title: "URL", name: "link", type: "object", fields: [ { title: "URL", name: "href", type: "url", }, ], }, ], }, }, // You can add additional types here. Note that you can't use // primitive types such as 'string' and 'number' in the same array // as a block type. { type: "image", options: { hotspot: true }, }, // 新增代码块配置,和图片、文字块平级 { title: "Code", name: "code", type: "code", options: { theme: "monokai", withFilename: true, }, }, ], };
注意事项
- 配置写入完成后,需要重启本地运行的Sanity Studio服务,富文本编辑器才会加载代码块插入选项
- 无需在
schema.js中额外注册code类型:安装code-input插件时已自动将基础code类型加入全局schema类型池,只要在blockContent的可插入列表中引用,整个配置链路即可生效。
内容的提问来源于stack exchange,提问作者HKS
相关产品推荐
相关产品推荐

