pte-interpolation-react 1.0.1 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/pte-interpolation-react.svg?style=flat-square)](https://www.npmjs.com/package/pte-interpolation-react)
4
4
 
5
+ ![Demo](https://raw.githubusercontent.com/jordanl17/sanity-pte-interpolation/main/.github/assets/demo.png)
6
+
5
7
  React rendering library that resolves dynamic variable inline blocks in [Portable Text](https://portabletext.org/) to actual values. Wraps [`@portabletext/react`](https://github.com/portabletext/react-portabletext) with automatic variable substitution.
6
8
 
7
9
  Part of [sanity-pte-interpolation](https://github.com/jordanl17/sanity-pte-interpolation). For adding variable picker inline blocks to Sanity Studio, see [`sanity-plugin-pte-interpolation`](https://www.npmjs.com/package/sanity-plugin-pte-interpolation).
@@ -154,7 +156,7 @@ Returns a `PortableTextComponents` object with a `types.pteInterpolationVariable
154
156
 
155
157
  ### `VARIABLE_TYPE_PREFIX`
156
158
 
157
- The constant `'pteInterpolationVariable'` the `_type` string used for variable inline blocks. Exported for advanced use cases.
159
+ The constant `'pteInterpolationVariable'` - the `_type` string used for variable inline blocks. Exported for advanced use cases.
158
160
 
159
161
  ## License
160
162
 
package/dist/index.cjs CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var jsxRuntime = require("react/jsx-runtime"), react$1 = require("@portabletext/react"), react = require("react");
4
- const VARIABLE_TYPE_PREFIX = "pteInterpolationVariable";
3
+ var jsxRuntime = require("react/jsx-runtime"), react$1 = require("@portabletext/react"), react = require("react"), pteInterpolationCore = require("pte-interpolation-core");
5
4
  function defaultFallback(variableKey) {
6
5
  return `{${variableKey}}`;
7
6
  }
@@ -12,7 +11,7 @@ function createInterpolationComponents(values, fallback = defaultFallback) {
12
11
  }
13
12
  return {
14
13
  types: {
15
- [VARIABLE_TYPE_PREFIX]: VariableComponent
14
+ [pteInterpolationCore.VARIABLE_TYPE_PREFIX]: VariableComponent
16
15
  }
17
16
  };
18
17
  }
@@ -34,7 +33,24 @@ function InterpolatedPortableText({
34
33
  }, [interpolationValues, fallback, userComponents]);
35
34
  return /* @__PURE__ */ jsxRuntime.jsx(react$1.PortableText, { ...rest, components: mergedComponents });
36
35
  }
36
+ Object.defineProperty(exports, "VARIABLE_TYPE_PREFIX", {
37
+ enumerable: !0,
38
+ get: function() {
39
+ return pteInterpolationCore.VARIABLE_TYPE_PREFIX;
40
+ }
41
+ });
42
+ Object.defineProperty(exports, "extractVariableKeys", {
43
+ enumerable: !0,
44
+ get: function() {
45
+ return pteInterpolationCore.extractVariableKeys;
46
+ }
47
+ });
48
+ Object.defineProperty(exports, "interpolateToString", {
49
+ enumerable: !0,
50
+ get: function() {
51
+ return pteInterpolationCore.interpolateToString;
52
+ }
53
+ });
37
54
  exports.InterpolatedPortableText = InterpolatedPortableText;
38
- exports.VARIABLE_TYPE_PREFIX = VARIABLE_TYPE_PREFIX;
39
55
  exports.createInterpolationComponents = createInterpolationComponents;
40
56
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/constants.ts","../src/createInterpolationComponents.tsx","../src/InterpolatedPortableText.tsx"],"sourcesContent":["/** @public */\nexport const VARIABLE_TYPE_PREFIX = 'pteInterpolationVariable'\n","import type {PortableTextComponents, PortableTextTypeComponentProps} from '@portabletext/react'\n\nimport {VARIABLE_TYPE_PREFIX} from './constants'\nimport type {\n InterpolationFallback,\n InterpolationValues,\n PteInterpolationVariableBlock,\n} from './types'\n\nfunction defaultFallback(variableKey: string): string {\n return `{${variableKey}}`\n}\n\n/** @public */\nexport function createInterpolationComponents(\n values: InterpolationValues,\n fallback: InterpolationFallback = defaultFallback,\n): PortableTextComponents {\n function VariableComponent(props: PortableTextTypeComponentProps<PteInterpolationVariableBlock>) {\n const {variableKey} = props.value\n const resolvedValue =\n values[variableKey] !== undefined ? values[variableKey] : fallback(variableKey)\n\n return <span data-variable-key={variableKey}>{resolvedValue}</span>\n }\n\n return {\n types: {\n [VARIABLE_TYPE_PREFIX]: VariableComponent,\n },\n }\n}\n","import {PortableText} from '@portabletext/react'\nimport {useMemo} from 'react'\n\nimport {createInterpolationComponents} from './createInterpolationComponents'\nimport type {InterpolatedPortableTextProps} from './types'\n\n/** @public */\nexport function InterpolatedPortableText({\n interpolationValues,\n components: userComponents,\n fallback,\n ...rest\n}: InterpolatedPortableTextProps) {\n const mergedComponents = useMemo(() => {\n const interpolationComponents = createInterpolationComponents(interpolationValues, fallback)\n\n if (!userComponents) {\n return interpolationComponents\n }\n\n return {\n ...userComponents,\n types: {\n ...userComponents.types,\n ...interpolationComponents.types,\n },\n }\n }, [interpolationValues, fallback, userComponents])\n\n return <PortableText {...rest} components={mergedComponents} />\n}\n"],"names":["jsx","useMemo","PortableText"],"mappings":";;;AACO,MAAM,uBAAuB;ACQpC,SAAS,gBAAgB,aAA6B;AACpD,SAAO,IAAI,WAAW;AACxB;AAGO,SAAS,8BACd,QACA,WAAkC,iBACV;AACxB,WAAS,kBAAkB,OAAsE;AAC/F,UAAM,EAAC,YAAA,IAAe,MAAM,OACtB,gBACJ,OAAO,WAAW,MAAM,SAAY,OAAO,WAAW,IAAI,SAAS,WAAW;AAEhF,WAAOA,2BAAAA,IAAC,QAAA,EAAK,qBAAmB,aAAc,UAAA,eAAc;AAAA,EAC9D;AAEA,SAAO;AAAA,IACL,OAAO;AAAA,MACL,CAAC,oBAAoB,GAAG;AAAA,IAAA;AAAA,EAC1B;AAEJ;ACxBO,SAAS,yBAAyB;AAAA,EACvC;AAAA,EACA,YAAY;AAAA,EACZ;AAAA,EACA,GAAG;AACL,GAAkC;AAChC,QAAM,mBAAmBC,MAAAA,QAAQ,MAAM;AACrC,UAAM,0BAA0B,8BAA8B,qBAAqB,QAAQ;AAE3F,WAAK,iBAIE;AAAA,MACL,GAAG;AAAA,MACH,OAAO;AAAA,QACL,GAAG,eAAe;AAAA,QAClB,GAAG,wBAAwB;AAAA,MAAA;AAAA,IAC7B,IARO;AAAA,EAUX,GAAG,CAAC,qBAAqB,UAAU,cAAc,CAAC;AAElD,SAAOD,2BAAAA,IAACE,QAAAA,cAAA,EAAc,GAAG,MAAM,YAAY,kBAAkB;AAC/D;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/createInterpolationComponents.tsx","../src/InterpolatedPortableText.tsx"],"sourcesContent":["import type {PortableTextComponents, PortableTextTypeComponentProps} from '@portabletext/react'\n\nimport {VARIABLE_TYPE_PREFIX} from './constants'\nimport type {\n InterpolationFallback,\n InterpolationValues,\n PteInterpolationVariableBlock,\n} from './types'\n\nfunction defaultFallback(variableKey: string): string {\n return `{${variableKey}}`\n}\n\n/** @public */\nexport function createInterpolationComponents(\n values: InterpolationValues,\n fallback: InterpolationFallback = defaultFallback,\n): PortableTextComponents {\n function VariableComponent(props: PortableTextTypeComponentProps<PteInterpolationVariableBlock>) {\n const {variableKey} = props.value\n const resolvedValue =\n values[variableKey] !== undefined ? values[variableKey] : fallback(variableKey)\n\n return <span data-variable-key={variableKey}>{resolvedValue}</span>\n }\n\n return {\n types: {\n [VARIABLE_TYPE_PREFIX]: VariableComponent,\n },\n }\n}\n","import {PortableText} from '@portabletext/react'\nimport {useMemo} from 'react'\n\nimport {createInterpolationComponents} from './createInterpolationComponents'\nimport type {InterpolatedPortableTextProps} from './types'\n\n/** @public */\nexport function InterpolatedPortableText({\n interpolationValues,\n components: userComponents,\n fallback,\n ...rest\n}: InterpolatedPortableTextProps) {\n const mergedComponents = useMemo(() => {\n const interpolationComponents = createInterpolationComponents(interpolationValues, fallback)\n\n if (!userComponents) {\n return interpolationComponents\n }\n\n return {\n ...userComponents,\n types: {\n ...userComponents.types,\n ...interpolationComponents.types,\n },\n }\n }, [interpolationValues, fallback, userComponents])\n\n return <PortableText {...rest} components={mergedComponents} />\n}\n"],"names":["jsx","VARIABLE_TYPE_PREFIX","useMemo","PortableText"],"mappings":";;;AASA,SAAS,gBAAgB,aAA6B;AACpD,SAAO,IAAI,WAAW;AACxB;AAGO,SAAS,8BACd,QACA,WAAkC,iBACV;AACxB,WAAS,kBAAkB,OAAsE;AAC/F,UAAM,EAAC,YAAA,IAAe,MAAM,OACtB,gBACJ,OAAO,WAAW,MAAM,SAAY,OAAO,WAAW,IAAI,SAAS,WAAW;AAEhF,WAAOA,2BAAAA,IAAC,QAAA,EAAK,qBAAmB,aAAc,UAAA,eAAc;AAAA,EAC9D;AAEA,SAAO;AAAA,IACL,OAAO;AAAA,MACL,CAACC,yCAAoB,GAAG;AAAA,IAAA;AAAA,EAC1B;AAEJ;ACxBO,SAAS,yBAAyB;AAAA,EACvC;AAAA,EACA,YAAY;AAAA,EACZ;AAAA,EACA,GAAG;AACL,GAAkC;AAChC,QAAM,mBAAmBC,MAAAA,QAAQ,MAAM;AACrC,UAAM,0BAA0B,8BAA8B,qBAAqB,QAAQ;AAE3F,WAAK,iBAIE;AAAA,MACL,GAAG;AAAA,MACH,OAAO;AAAA,QACL,GAAG,eAAe;AAAA,QAClB,GAAG,wBAAwB;AAAA,MAAA;AAAA,IAC7B,IARO;AAAA,EAUX,GAAG,CAAC,qBAAqB,UAAU,cAAc,CAAC;AAElD,SAAOF,2BAAAA,IAACG,QAAAA,cAAA,EAAc,GAAG,MAAM,YAAY,kBAAkB;AAC/D;;;;;;;;;;;;;;;;;;;;;"}
package/dist/index.d.cts CHANGED
@@ -1,6 +1,13 @@
1
+ import {extractVariableKeys} from 'pte-interpolation-core'
2
+ import {interpolateToString} from 'pte-interpolation-core'
3
+ import {InterpolationFallback} from 'pte-interpolation-core'
4
+ import {InterpolationValues} from 'pte-interpolation-core'
1
5
  import {JSX} from 'react/jsx-runtime'
6
+ import {PortableTextBlockLike} from 'pte-interpolation-core'
7
+ import {PortableTextChild} from 'pte-interpolation-core'
2
8
  import type {PortableTextComponents} from '@portabletext/react'
3
9
  import type {PortableTextProps} from '@portabletext/react'
10
+ import {VARIABLE_TYPE_PREFIX} from 'pte-interpolation-core'
4
11
 
5
12
  /** @public */
6
13
  export declare function createInterpolationComponents(
@@ -8,6 +15,8 @@ export declare function createInterpolationComponents(
8
15
  fallback?: InterpolationFallback,
9
16
  ): PortableTextComponents
10
17
 
18
+ export {extractVariableKeys}
19
+
11
20
  /** @public */
12
21
  export declare function InterpolatedPortableText({
13
22
  interpolationValues,
@@ -26,11 +35,15 @@ export declare interface InterpolatedPortableTextProps extends Omit<
26
35
  fallback?: InterpolationFallback
27
36
  }
28
37
 
29
- /** @public */
30
- export declare type InterpolationFallback = (variableKey: string) => string
38
+ export {interpolateToString}
31
39
 
32
- /** @public */
33
- export declare type InterpolationValues = Record<string, string>
40
+ export {InterpolationFallback}
41
+
42
+ export {InterpolationValues}
43
+
44
+ export {PortableTextBlockLike}
45
+
46
+ export {PortableTextChild}
34
47
 
35
48
  /** @public */
36
49
  export declare interface PteInterpolationVariableBlock {
@@ -39,7 +52,6 @@ export declare interface PteInterpolationVariableBlock {
39
52
  variableKey: string
40
53
  }
41
54
 
42
- /** @public */
43
- export declare const VARIABLE_TYPE_PREFIX = 'pteInterpolationVariable'
55
+ export {VARIABLE_TYPE_PREFIX}
44
56
 
45
57
  export {}
package/dist/index.d.ts CHANGED
@@ -1,6 +1,13 @@
1
+ import {extractVariableKeys} from 'pte-interpolation-core'
2
+ import {interpolateToString} from 'pte-interpolation-core'
3
+ import {InterpolationFallback} from 'pte-interpolation-core'
4
+ import {InterpolationValues} from 'pte-interpolation-core'
1
5
  import {JSX} from 'react/jsx-runtime'
6
+ import {PortableTextBlockLike} from 'pte-interpolation-core'
7
+ import {PortableTextChild} from 'pte-interpolation-core'
2
8
  import type {PortableTextComponents} from '@portabletext/react'
3
9
  import type {PortableTextProps} from '@portabletext/react'
10
+ import {VARIABLE_TYPE_PREFIX} from 'pte-interpolation-core'
4
11
 
5
12
  /** @public */
6
13
  export declare function createInterpolationComponents(
@@ -8,6 +15,8 @@ export declare function createInterpolationComponents(
8
15
  fallback?: InterpolationFallback,
9
16
  ): PortableTextComponents
10
17
 
18
+ export {extractVariableKeys}
19
+
11
20
  /** @public */
12
21
  export declare function InterpolatedPortableText({
13
22
  interpolationValues,
@@ -26,11 +35,15 @@ export declare interface InterpolatedPortableTextProps extends Omit<
26
35
  fallback?: InterpolationFallback
27
36
  }
28
37
 
29
- /** @public */
30
- export declare type InterpolationFallback = (variableKey: string) => string
38
+ export {interpolateToString}
31
39
 
32
- /** @public */
33
- export declare type InterpolationValues = Record<string, string>
40
+ export {InterpolationFallback}
41
+
42
+ export {InterpolationValues}
43
+
44
+ export {PortableTextBlockLike}
45
+
46
+ export {PortableTextChild}
34
47
 
35
48
  /** @public */
36
49
  export declare interface PteInterpolationVariableBlock {
@@ -39,7 +52,6 @@ export declare interface PteInterpolationVariableBlock {
39
52
  variableKey: string
40
53
  }
41
54
 
42
- /** @public */
43
- export declare const VARIABLE_TYPE_PREFIX = 'pteInterpolationVariable'
55
+ export {VARIABLE_TYPE_PREFIX}
44
56
 
45
57
  export {}
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
2
  import { PortableText } from "@portabletext/react";
3
3
  import { useMemo } from "react";
4
- const VARIABLE_TYPE_PREFIX = "pteInterpolationVariable";
4
+ import { VARIABLE_TYPE_PREFIX } from "pte-interpolation-core";
5
+ import { VARIABLE_TYPE_PREFIX as VARIABLE_TYPE_PREFIX2, extractVariableKeys, interpolateToString } from "pte-interpolation-core";
5
6
  function defaultFallback(variableKey) {
6
7
  return `{${variableKey}}`;
7
8
  }
@@ -36,7 +37,9 @@ function InterpolatedPortableText({
36
37
  }
37
38
  export {
38
39
  InterpolatedPortableText,
39
- VARIABLE_TYPE_PREFIX,
40
- createInterpolationComponents
40
+ VARIABLE_TYPE_PREFIX2 as VARIABLE_TYPE_PREFIX,
41
+ createInterpolationComponents,
42
+ extractVariableKeys,
43
+ interpolateToString
41
44
  };
42
45
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/constants.ts","../src/createInterpolationComponents.tsx","../src/InterpolatedPortableText.tsx"],"sourcesContent":["/** @public */\nexport const VARIABLE_TYPE_PREFIX = 'pteInterpolationVariable'\n","import type {PortableTextComponents, PortableTextTypeComponentProps} from '@portabletext/react'\n\nimport {VARIABLE_TYPE_PREFIX} from './constants'\nimport type {\n InterpolationFallback,\n InterpolationValues,\n PteInterpolationVariableBlock,\n} from './types'\n\nfunction defaultFallback(variableKey: string): string {\n return `{${variableKey}}`\n}\n\n/** @public */\nexport function createInterpolationComponents(\n values: InterpolationValues,\n fallback: InterpolationFallback = defaultFallback,\n): PortableTextComponents {\n function VariableComponent(props: PortableTextTypeComponentProps<PteInterpolationVariableBlock>) {\n const {variableKey} = props.value\n const resolvedValue =\n values[variableKey] !== undefined ? values[variableKey] : fallback(variableKey)\n\n return <span data-variable-key={variableKey}>{resolvedValue}</span>\n }\n\n return {\n types: {\n [VARIABLE_TYPE_PREFIX]: VariableComponent,\n },\n }\n}\n","import {PortableText} from '@portabletext/react'\nimport {useMemo} from 'react'\n\nimport {createInterpolationComponents} from './createInterpolationComponents'\nimport type {InterpolatedPortableTextProps} from './types'\n\n/** @public */\nexport function InterpolatedPortableText({\n interpolationValues,\n components: userComponents,\n fallback,\n ...rest\n}: InterpolatedPortableTextProps) {\n const mergedComponents = useMemo(() => {\n const interpolationComponents = createInterpolationComponents(interpolationValues, fallback)\n\n if (!userComponents) {\n return interpolationComponents\n }\n\n return {\n ...userComponents,\n types: {\n ...userComponents.types,\n ...interpolationComponents.types,\n },\n }\n }, [interpolationValues, fallback, userComponents])\n\n return <PortableText {...rest} components={mergedComponents} />\n}\n"],"names":[],"mappings":";;;AACO,MAAM,uBAAuB;ACQpC,SAAS,gBAAgB,aAA6B;AACpD,SAAO,IAAI,WAAW;AACxB;AAGO,SAAS,8BACd,QACA,WAAkC,iBACV;AACxB,WAAS,kBAAkB,OAAsE;AAC/F,UAAM,EAAC,YAAA,IAAe,MAAM,OACtB,gBACJ,OAAO,WAAW,MAAM,SAAY,OAAO,WAAW,IAAI,SAAS,WAAW;AAEhF,WAAO,oBAAC,QAAA,EAAK,qBAAmB,aAAc,UAAA,eAAc;AAAA,EAC9D;AAEA,SAAO;AAAA,IACL,OAAO;AAAA,MACL,CAAC,oBAAoB,GAAG;AAAA,IAAA;AAAA,EAC1B;AAEJ;ACxBO,SAAS,yBAAyB;AAAA,EACvC;AAAA,EACA,YAAY;AAAA,EACZ;AAAA,EACA,GAAG;AACL,GAAkC;AAChC,QAAM,mBAAmB,QAAQ,MAAM;AACrC,UAAM,0BAA0B,8BAA8B,qBAAqB,QAAQ;AAE3F,WAAK,iBAIE;AAAA,MACL,GAAG;AAAA,MACH,OAAO;AAAA,QACL,GAAG,eAAe;AAAA,QAClB,GAAG,wBAAwB;AAAA,MAAA;AAAA,IAC7B,IARO;AAAA,EAUX,GAAG,CAAC,qBAAqB,UAAU,cAAc,CAAC;AAElD,SAAO,oBAAC,cAAA,EAAc,GAAG,MAAM,YAAY,kBAAkB;AAC/D;"}
1
+ {"version":3,"file":"index.js","sources":["../src/createInterpolationComponents.tsx","../src/InterpolatedPortableText.tsx"],"sourcesContent":["import type {PortableTextComponents, PortableTextTypeComponentProps} from '@portabletext/react'\n\nimport {VARIABLE_TYPE_PREFIX} from './constants'\nimport type {\n InterpolationFallback,\n InterpolationValues,\n PteInterpolationVariableBlock,\n} from './types'\n\nfunction defaultFallback(variableKey: string): string {\n return `{${variableKey}}`\n}\n\n/** @public */\nexport function createInterpolationComponents(\n values: InterpolationValues,\n fallback: InterpolationFallback = defaultFallback,\n): PortableTextComponents {\n function VariableComponent(props: PortableTextTypeComponentProps<PteInterpolationVariableBlock>) {\n const {variableKey} = props.value\n const resolvedValue =\n values[variableKey] !== undefined ? values[variableKey] : fallback(variableKey)\n\n return <span data-variable-key={variableKey}>{resolvedValue}</span>\n }\n\n return {\n types: {\n [VARIABLE_TYPE_PREFIX]: VariableComponent,\n },\n }\n}\n","import {PortableText} from '@portabletext/react'\nimport {useMemo} from 'react'\n\nimport {createInterpolationComponents} from './createInterpolationComponents'\nimport type {InterpolatedPortableTextProps} from './types'\n\n/** @public */\nexport function InterpolatedPortableText({\n interpolationValues,\n components: userComponents,\n fallback,\n ...rest\n}: InterpolatedPortableTextProps) {\n const mergedComponents = useMemo(() => {\n const interpolationComponents = createInterpolationComponents(interpolationValues, fallback)\n\n if (!userComponents) {\n return interpolationComponents\n }\n\n return {\n ...userComponents,\n types: {\n ...userComponents.types,\n ...interpolationComponents.types,\n },\n }\n }, [interpolationValues, fallback, userComponents])\n\n return <PortableText {...rest} components={mergedComponents} />\n}\n"],"names":[],"mappings":";;;;;AASA,SAAS,gBAAgB,aAA6B;AACpD,SAAO,IAAI,WAAW;AACxB;AAGO,SAAS,8BACd,QACA,WAAkC,iBACV;AACxB,WAAS,kBAAkB,OAAsE;AAC/F,UAAM,EAAC,YAAA,IAAe,MAAM,OACtB,gBACJ,OAAO,WAAW,MAAM,SAAY,OAAO,WAAW,IAAI,SAAS,WAAW;AAEhF,WAAO,oBAAC,QAAA,EAAK,qBAAmB,aAAc,UAAA,eAAc;AAAA,EAC9D;AAEA,SAAO;AAAA,IACL,OAAO;AAAA,MACL,CAAC,oBAAoB,GAAG;AAAA,IAAA;AAAA,EAC1B;AAEJ;ACxBO,SAAS,yBAAyB;AAAA,EACvC;AAAA,EACA,YAAY;AAAA,EACZ;AAAA,EACA,GAAG;AACL,GAAkC;AAChC,QAAM,mBAAmB,QAAQ,MAAM;AACrC,UAAM,0BAA0B,8BAA8B,qBAAqB,QAAQ;AAE3F,WAAK,iBAIE;AAAA,MACL,GAAG;AAAA,MACH,OAAO;AAAA,QACL,GAAG,eAAe;AAAA,QAClB,GAAG,wBAAwB;AAAA,MAAA;AAAA,IAC7B,IARO;AAAA,EAUX,GAAG,CAAC,qBAAqB,UAAU,cAAc,CAAC;AAElD,SAAO,oBAAC,cAAA,EAAc,GAAG,MAAM,YAAY,kBAAkB;AAC/D;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pte-interpolation-react",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "React components for rendering Portable Text with interpolated values",
@@ -40,7 +40,8 @@
40
40
  "access": "public"
41
41
  },
42
42
  "dependencies": {
43
- "@portabletext/react": "^6.0.2"
43
+ "@portabletext/react": "^6.0.2",
44
+ "pte-interpolation-core": "1.1.1"
44
45
  },
45
46
  "peerDependencies": {
46
47
  "react": "^18.0.0 || ^19.0.0"
@@ -0,0 +1,87 @@
1
+ import {describe, expect, it} from 'vitest'
2
+ import {
3
+ extractVariableKeys,
4
+ interpolateToString,
5
+ VARIABLE_TYPE_PREFIX,
6
+ } from 'pte-interpolation-react'
7
+ import type {
8
+ InterpolationFallback,
9
+ InterpolationValues,
10
+ PortableTextBlockLike,
11
+ PortableTextChild,
12
+ PteInterpolationVariableBlock,
13
+ } from 'pte-interpolation-react'
14
+ import {singleVariableBlock, multipleVariablesBlock, plainTextBlock} from './fixtures'
15
+
16
+ describe('re-exports from pte-interpolation-core', () => {
17
+ describe('VARIABLE_TYPE_PREFIX', () => {
18
+ it('equals pteInterpolationVariable', () => {
19
+ expect(VARIABLE_TYPE_PREFIX).toBe('pteInterpolationVariable')
20
+ })
21
+ })
22
+
23
+ describe('extractVariableKeys', () => {
24
+ it('extracts variable keys from blocks', () => {
25
+ const keys = extractVariableKeys(singleVariableBlock)
26
+ expect(keys).toEqual(['firstName'])
27
+ })
28
+
29
+ it('extracts multiple variable keys', () => {
30
+ const keys = extractVariableKeys(multipleVariablesBlock)
31
+ expect(keys).toEqual(['firstName', 'lastName', 'email'])
32
+ })
33
+
34
+ it('returns empty array for plain text', () => {
35
+ const keys = extractVariableKeys(plainTextBlock)
36
+ expect(keys).toEqual([])
37
+ })
38
+ })
39
+
40
+ describe('interpolateToString', () => {
41
+ it('interpolates variable values into text', () => {
42
+ const result = interpolateToString(singleVariableBlock, {firstName: 'Alice'})
43
+ expect(result).toBe('Hello, Alice!')
44
+ })
45
+
46
+ it('uses default fallback for missing values', () => {
47
+ const result = interpolateToString(singleVariableBlock, {})
48
+ expect(result).toBe('Hello, {firstName}!')
49
+ })
50
+
51
+ it('uses custom fallback for missing values', () => {
52
+ const customFallback: InterpolationFallback = (variableKey) => `[${variableKey}]`
53
+ const result = interpolateToString(singleVariableBlock, {}, customFallback)
54
+ expect(result).toBe('Hello, [firstName]!')
55
+ })
56
+ })
57
+
58
+ describe('type exports', () => {
59
+ it('InterpolationValues type works as a record of strings', () => {
60
+ const values: InterpolationValues = {firstName: 'Alice', lastName: 'Smith'}
61
+ expect(values).toEqual({firstName: 'Alice', lastName: 'Smith'})
62
+ })
63
+
64
+ it('PortableTextBlockLike type is compatible with fixture blocks', () => {
65
+ const blocks: PortableTextBlockLike[] = singleVariableBlock
66
+ expect(blocks).toHaveLength(1)
67
+ })
68
+
69
+ it('PortableTextChild type is compatible with block children', () => {
70
+ const child: PortableTextChild = {
71
+ _type: 'span',
72
+ _key: 'span-1',
73
+ text: 'Hello',
74
+ }
75
+ expect(child._type).toBe('span')
76
+ })
77
+
78
+ it('PteInterpolationVariableBlock type is compatible with variable blocks', () => {
79
+ const variableBlock: PteInterpolationVariableBlock = {
80
+ _type: 'pteInterpolationVariable',
81
+ _key: 'var-1',
82
+ variableKey: 'firstName',
83
+ }
84
+ expect(variableBlock.variableKey).toBe('firstName')
85
+ })
86
+ })
87
+ })
package/src/constants.ts CHANGED
@@ -1,2 +1 @@
1
- /** @public */
2
- export const VARIABLE_TYPE_PREFIX = 'pteInterpolationVariable'
1
+ export {VARIABLE_TYPE_PREFIX} from 'pte-interpolation-core'
package/src/index.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  export {InterpolatedPortableText} from './InterpolatedPortableText'
2
2
  export {createInterpolationComponents} from './createInterpolationComponents'
3
3
  export {VARIABLE_TYPE_PREFIX} from './constants'
4
+ export {extractVariableKeys, interpolateToString} from 'pte-interpolation-core'
4
5
  export type {
5
6
  InterpolatedPortableTextProps,
6
7
  InterpolationFallback,
7
8
  InterpolationValues,
8
9
  PteInterpolationVariableBlock,
9
10
  } from './types'
11
+ export type {PortableTextBlockLike, PortableTextChild} from 'pte-interpolation-core'
package/src/types.ts CHANGED
@@ -1,10 +1,7 @@
1
1
  import type {PortableTextComponents, PortableTextProps} from '@portabletext/react'
2
+ import type {InterpolationFallback, InterpolationValues} from 'pte-interpolation-core'
2
3
 
3
- /** @public */
4
- export type InterpolationValues = Record<string, string>
5
-
6
- /** @public */
7
- export type InterpolationFallback = (variableKey: string) => string
4
+ export type {InterpolationFallback, InterpolationValues} from 'pte-interpolation-core'
8
5
 
9
6
  /** @public */
10
7
  export interface PteInterpolationVariableBlock {