mcpField
D
directives

directives

github.com/vercel-labs开发18.8k8.0k 次安装5 天前更新
代码仓库

关于 directives

@json-render/directives 为 @json-render/core 提供可添加到目录和渲染器的预置指令,适用于构建 JSON UI 的开发者。它涵盖格式化、数学运算、字符串处理和国际化,并支持组合解析及自定义指令。

主要功能

  1. 01按区域设置格式化日期与数值
  2. 02执行算术运算
  3. 03拼接及截断文本
  4. 04统计并连接数组或字符串
  5. 05处理单复数形式
  6. 06通过工厂配置国际化指令

适用场景

  1. 01显示本地化日期和货币
  2. 02在渲染数据中计算价格与税额
  3. 03缩短文章文本并展示项目数
  4. 04为界面文案配置翻译

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/vercel-labs/json-render --skill directives

安装后在 Claude Code 中输入/directives确认已加载。

详细介绍

@json-render/directives

为 @json-render/core 预先构建的自定义指令。将它们添加到目录和渲染器中,即可支持格式化、数学运算、字符串操作和国际化。

快速开始

import { standardDirectives } from '@json-render/directives';

// Wire into prompt generation
const prompt = catalog.prompt({ directives: standardDirectives });

// Wire into the renderer (React example)
import { JSONUIProvider, Renderer } from '@json-render/react';

<JSONUIProvider registry={registry} directives={standardDirectives}>
  <Renderer spec={spec} registry={registry} />
</JSONUIProvider>

要添加 createI18nDirective 这类工厂指令,请展开该数组:

import { standardDirectives, createI18nDirective } from '@json-render/directives';

const directives = [...standardDirectives, createI18nDirective(config)];

定义自定义指令

使用 @json-render/core 中的 defineDirective:

import { defineDirective, resolvePropValue } from '@json-render/core';
import { z } from 'zod';

const doubleDirective = defineDirective({
  name: '$double',
  description: 'Double a numeric value.',
  schema: z.object({
    $double: z.unknown(),
  }),
  resolve(value, ctx) {
    const resolved = resolvePropValue(value.$double, ctx);
    return (resolved as number) * 2;
  },
});

规则:

  • 名称必须以 $ 开头
  • 名称不能与内置键冲突($state、$cond、$computed、$template、$item、$index、$bindState、$bindItem)
  • 解析器应对内部值调用 resolvePropValue,以支持组合

内置指令

$format — 区域设置感知的值格式化

使用 Intl 格式化器格式化值。支持 date、currency、number 和 percent。

{ "$format": "currency", "value": { "$state": "/cart/total" }, "currency": "USD" }
{ "$format": "date", "value": { "$state": "/user/createdAt" } }
{ "$format": "number", "value": 1234567, "notation": "compact" }
{ "$format": "percent", "value": 0.75 }
{ "$format": "date", "value": { "$state": "/post/createdAt" }, "style": "relative" }

字段:$format(date | currency | number | percent)、value(任意表达式)、locale?(字符串)、currency?(字符串,默认值为 "USD")、notation?(字符串)、style?("relative" 表示相对日期)、options?(额外的 Intl 选项)。

$math — 算术运算
{ "$math": "add", "a": { "$state": "/subtotal" }, "b": { "$state": "/tax" } }
{ "$math": "round", "a": 3.7 }

运算:add、subtract、multiply、divide、mod、min、max、round、floor、ceil、abs。一元运算(round、floor、ceil、abs)只使用 a。除以零时返回 0。

字段:$math(运算枚举)、a?(第一个操作数,默认值为 0)、b?(第二个操作数,默认值为 0)。

$concat — 字符串拼接
{ "$concat": [{ "$state": "/user/firstName" }, " ", { "$state": "/user/lastName" }] }

字段:$concat(要解析并拼接为字符串的值数组)。

$count — 数组/字符串长度
{ "$count": { "$state": "/cart/items" } }

返回数组或字符串的 .length,其他类型返回 0。

字段:$count(要计数的值)。

$truncate — 文本截断
{ "$truncate": { "$state": "/post/body" }, "length": 140, "suffix": "..." }

字段:$truncate(要截断的值)、length?(最大字符数,默认值为 100)、suffix?(字符串,默认值为 "...")。

$pluralize — 单数/复数形式
{ "$pluralize": { "$state": "/cart/itemCount" }, "one": "item", "other": "items", "zero": "no items" }

输出:"3 items"、"1 item" 或 "no items"。

字段:$pluralize(计数值)、one(单数标签)、other(复数标签)、zero?(零值标签)。

$join — 拼接数组元素
{ "$join": { "$state": "/tags" }, "separator": ", " }

字段:$join(要拼接的数组)、separator?(字符串,默认值为 ", ")。

createI18nDirective — 国际化工厂
import { createI18nDirective } from '@json-render/directives';

const tDirective = createI18nDirective({
  locale: 'en',
  messages: {
    en: { "greeting": "Hello, {{name}}!", "checkout.submit": "Place Order" },
    es: { "greeting": "Hola, {{name}}!", "checkout.submit": "Realizar Pedido" },
  },
  fallbackLocale: 'en',
});

在规格中使用:

{ "$t": "checkout.submit" }
{ "$t": "greeting", "params": { "name": { "$state": "/user/name" } } }

字段:$t(翻译键)、params?(插值参数,值可以接受表达式)。

配置:locale(当前区域设置)、messages(Record<locale, Record<key, string>>)、fallbackLocale?(键缺失时使用的回退区域设置)。

组合

指令可以自然组合——每个解析器都会对其输入调用 resolvePropValue,因此指令可以包装其他指令或内置表达式:

{
  "$format": "currency",
  "value": { "$math": "multiply", "a": { "$state": "/price" }, "b": { "$state": "/qty" } },
  "currency": "USD"
}

按由内向外的顺序解析:$state 从状态中读取值,$math 执行乘法,$format 将结果格式化为货币。

接入渲染器

所有四种渲染器(React、Vue、Svelte、Solid)都支持在其 provider 和 createRenderer 输出中传入 directives:

// Provider pattern
<JSONUIProvider registry={registry} directives={directives}>
  <Renderer spec={spec} registry={registry} />
</JSONUIProvider>

// createRenderer pattern
const MyRenderer = createRenderer(catalog, components);
<MyRenderer spec={spec} directives={directives} />

生成提示词时,传入相同的数组:

const prompt = catalog.prompt({ directives });

主要导出项

导出项用途
formatDirective$format 指令定义
mathDirective$math 指令定义
concatDirective$concat 指令定义
countDirective$count 指令定义
truncateDirective$truncate 指令定义
pluralizeDirective$pluralize 指令定义
joinDirective$join 指令定义
createI18nDirective$t 国际化指令的工厂
standardDirectives包含全部 7 个非工厂指令的数组
I18nConfig国际化配置类型

评论

支持 Markdown