@portabletext/toolkit
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,6 +0,6 @@ | ||
"use strict";var A=Object.defineProperty,D=Object.defineProperties;var B=Object.getOwnPropertyDescriptors;var x=Object.getOwnPropertySymbols;var E=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var T=(e,t,n)=>t in e?A(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,g=(e,t)=>{for(var n in t||(t={}))E.call(t,n)&&T(e,n,t[n]);if(x)for(var n of x(t))K.call(t,n)&&T(e,n,t[n]);return e},v=(e,t)=>D(e,B(t));Object.defineProperty(exports,"__esModule",{value:!0});exports[Symbol.toStringTag]="Module";function u(e){return e._type==="span"&&"text"in e&&typeof e.text=="string"&&(typeof e.marks=="undefined"||Array.isArray(e.marks)&&e.marks.every(t=>typeof t=="string"))}function k(e){return typeof e._type=="string"&&e._type[0]!=="@"&&(!("markDefs"in e)||Array.isArray(e.markDefs)&&e.markDefs.every(t=>typeof t._key=="string"))&&"children"in e&&Array.isArray(e.children)&&e.children.every(t=>typeof t=="object"&&"_type"in t)}function _(e){return k(e)&&"listItem"in e&&typeof e.listItem=="string"&&(typeof e.level=="undefined"||typeof e.level=="number")}function P(e){return e._type==="@list"}function I(e){return e._type==="@span"}function b(e){return e._type==="@text"}const L=["strong","em","code","underline","strike-through"];function M(e,t,n){if(!u(e)||!e.marks)return[];if(!e.marks.length)return[];const r=e.marks.slice(),l={};return r.forEach(i=>{l[i]=1;for(let s=t+1;s<n.length;s++){const o=n[s];if(u(o)&&Array.isArray(o.marks)&&o.marks.indexOf(i)!==-1)l[i]++;else break}}),r.sort((i,s)=>j(l,i,s))}function j(e,t,n){const r=e[t],l=e[n];if(r!==l)return l-r;const i=L.indexOf(t),s=L.indexOf(n);return i!==s?i-s:t.localeCompare(n)}function $(e){const{children:t,markDefs:n=[]}=e;if(!t||!t.length)return[];const r=t.map(M),l={_type:"@span",children:[],markType:"<unknown>"};let i=[l];for(let s=0;s<t.length;s++){const o=t[s],h=r[s];let p=1;if(i.length>1)for(p;p<i.length;p++){const a=i[p].markKey||"",c=h.indexOf(a);if(c===-1)break;h.splice(c,1)}i=i.slice(0,p);let f=i[i.length-1];for(const a of h){const c=n.find(w=>w._key===a),S=c?c._type:a,d={_type:"@span",_key:o._key,children:[],markDef:c,markType:S,markKey:a};f.children.push(d),i.push(d),f=d}if(u(o)){const a=o.text.split(` | ||
`);for(let c=a.length;c-- >1;)a.splice(c,0,` | ||
`);f.children=f.children.concat(a.map(c=>({_type:"@text",text:c})))}else f.children=f.children.concat(o)}return l.children}function C(e,t){const n=[];let r;for(let l=0;l<e.length;l++){const i=e[l];if(!_(i)){n.push(i),r=void 0;continue}if(!r){r=y(i,l,t),n.push(r);continue}if(F(i,r)){r.children.push(i);continue}if((i.level||1)>r.level){const s=y(i,l,t);if(t==="html"){const o=r.children[r.children.length-1],h=v(g({},o),{children:[...o.children,s]});r.children[r.children.length-1]=h}else r.children.push(s);r=s;continue}if((i.level||1)<r.level){const s=m(n[n.length-1],i);if(s){r=s,r.children.push(i);continue}r=y(i,l,t),n.push(r);continue}if(i.listItem!==r.listItem){const s=m(n[n.length-1],{level:i.level||1});if(s&&s.listItem===i.listItem){r=s,r.children.push(i);continue}else{r=y(i,l,t),n.push(r);continue}}console.warn("Unknown state encountered for block",i),n.push(i)}return n}function F(e,t){return(e.level||1)===t.level&&e.listItem===t.listItem}function y(e,t,n){return{_type:"@list",_key:`${e._key||`${t}`}-parent`,mode:n,level:e.level||1,listItem:e.listItem,children:[e]}}function m(e,t){const n=t.level||1,r=t.listItem||"normal",l=typeof t.listItem=="string";if(P(e)&&(e.level||1)===n&&l&&(e.listItem||"normal")===r)return e;if(!("children"in e))return;const i=e.children[e.children.length-1];return i&&!u(i)?m(i,t):void 0}const H=/^\s/,N=/^\s/;function U(e){const t=Array.isArray(e)?e:[e];let n="";return t.forEach((r,l)=>{if(!k(r))return;let i=!1;r.children.forEach(s=>{u(s)?(n+=i&&n&&!N.test(n)&&!H.test(s.text)?" ":"",n+=s.text,i=!1):i=!0}),l!==t.length-1&&(n+=` | ||
"use strict";var D=Object.defineProperty,w=Object.defineProperties;var A=Object.getOwnPropertyDescriptors;var m=Object.getOwnPropertySymbols;var B=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var x=(e,t,n)=>t in e?D(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,_=(e,t)=>{for(var n in t||(t={}))B.call(t,n)&&x(e,n,t[n]);if(m)for(var n of m(t))K.call(t,n)&&x(e,n,t[n]);return e},g=(e,t)=>w(e,A(t));Object.defineProperty(exports,"__esModule",{value:!0});exports[Symbol.toStringTag]="Module";function u(e){return e._type==="span"&&"text"in e&&typeof e.text=="string"&&(typeof e.marks=="undefined"||Array.isArray(e.marks)&&e.marks.every(t=>typeof t=="string"))}function d(e){return typeof e._type=="string"&&e._type[0]!=="@"&&(!("markDefs"in e)||Array.isArray(e.markDefs)&&e.markDefs.every(t=>typeof t._key=="string"))&&"children"in e&&Array.isArray(e.children)&&e.children.every(t=>typeof t=="object"&&"_type"in t)}function I(e){return d(e)&&"listItem"in e&&typeof e.listItem=="string"&&(typeof e.level=="undefined"||typeof e.level=="number")}function v(e){return e._type==="@list"}function P(e){return e._type==="@span"}function L(e){return e._type==="@text"}const S=["strong","em","code","underline","strike-through"];function b(e,t,n){if(!u(e)||!e.marks)return[];if(!e.marks.length)return[];const r=e.marks.slice(),l={};return r.forEach(i=>{l[i]=1;for(let s=t+1;s<n.length;s++){const c=n[s];if(u(c)&&Array.isArray(c.marks)&&c.marks.indexOf(i)!==-1)l[i]++;else break}}),r.sort((i,s)=>C(l,i,s))}function C(e,t,n){const r=e[t],l=e[n];if(r!==l)return l-r;const i=S.indexOf(t),s=S.indexOf(n);return i!==s?i-s:t.localeCompare(n)}function N(e){const{children:t,markDefs:n=[]}=e;if(!t||!t.length)return[];const r=t.map(b),l={_type:"@span",children:[],markType:"<unknown>"};let i=[l];for(let s=0;s<t.length;s++){const c=t[s],h=r[s];let p=1;if(i.length>1)for(p;p<i.length;p++){const a=i[p].markKey||"",o=h.indexOf(a);if(o===-1)break;h.splice(o,1)}i=i.slice(0,p);let f=i[i.length-1];for(const a of h){const o=n.find(O=>O._key===a),E=o?o._type:a,T={_type:"@span",_key:c._key,children:[],markDef:o,markType:E,markKey:a};f.children.push(T),i.push(T),f=T}if(u(c)){const a=c.text.split(` | ||
`);for(let o=a.length;o-- >1;)a.splice(o,0,` | ||
`);f.children=f.children.concat(a.map(o=>({_type:"@text",text:o})))}else f.children=f.children.concat(c)}return l.children}function j(e,t){const n=[];let r;for(let l=0;l<e.length;l++){const i=e[l];if(!I(i)){n.push(i),r=void 0;continue}if(!r){r=y(i,l,t),n.push(r);continue}if(H(i,r)){r.children.push(i);continue}if((i.level||1)>r.level){const s=y(i,l,t);if(t==="html"){const c=r.children[r.children.length-1],h=g(_({},c),{children:[...c.children,s]});r.children[r.children.length-1]=h}else r.children.push(s);r=s;continue}if((i.level||1)<r.level){const s=k(n[n.length-1],i);if(s){r=s,r.children.push(i);continue}r=y(i,l,t),n.push(r);continue}if(i.listItem!==r.listItem){const s=k(n[n.length-1],{level:i.level||1});if(s&&s.listItem===i.listItem){r=s,r.children.push(i);continue}else{r=y(i,l,t),n.push(r);continue}}console.warn("Unknown state encountered for block",i),n.push(i)}return n}function H(e,t){return(e.level||1)===t.level&&e.listItem===t.listItem}function y(e,t,n){return{_type:"@list",_key:`${e._key||`${t}`}-parent`,mode:n,level:e.level||1,listItem:e.listItem,children:[e]}}function k(e,t){const n=t.level||1,r=t.listItem||"normal",l=typeof t.listItem=="string";if(v(e)&&(e.level||1)===n&&l&&(e.listItem||"normal")===r)return e;if(!("children"in e))return;const i=e.children[e.children.length-1];return i&&!u(i)?k(i,t):void 0}const R=/^\s/,$=/^\s/;function F(e){const t=Array.isArray(e)?e:[e];let n="";return t.forEach((r,l)=>{if(!d(r))return;let i=!1;r.children.forEach(s=>{u(s)?(n+=i&&n&&!$.test(n)&&!R.test(s.text)?" ":"",n+=s.text,i=!1):i=!0}),l!==t.length-1&&(n+=` | ||
`)}),n}function O(e){let t="";return e.children.forEach(n=>{b(n)?t+=n.text:I(n)&&(t+=O(n))}),t}exports.ToolkitListNestMode=void 0;(function(e){e.Html="html",e.Direct="direct"})(exports.ToolkitListNestMode||(exports.ToolkitListNestMode={}));exports.buildMarksTree=$;exports.isPortableTextBlock=k;exports.isPortableTextListItemBlock=_;exports.isPortableTextSpan=u;exports.isPortableTextToolkitList=P;exports.isPortableTextToolkitSpan=I;exports.isPortableTextToolkitTextNode=b;exports.nestLists=C;exports.sortMarksByOccurences=M;exports.spanToPlainText=O;exports.toPlainText=U; | ||
`)}),n}function M(e){let t="";return e.children.forEach(n=>{L(n)?t+=n.text:P(n)&&(t+=M(n))}),t}const U="html",q="direct";exports.LIST_NEST_MODE_DIRECT=q;exports.LIST_NEST_MODE_HTML=U;exports.buildMarksTree=N;exports.isPortableTextBlock=d;exports.isPortableTextListItemBlock=I;exports.isPortableTextSpan=u;exports.isPortableTextToolkitList=v;exports.isPortableTextToolkitSpan=P;exports.isPortableTextToolkitTextNode=L;exports.nestLists=j;exports.sortMarksByOccurences=b;exports.spanToPlainText=M;exports.toPlainText=F; | ||
//# sourceMappingURL=portable-text-toolkit.cjs.js.map |
@@ -252,8 +252,5 @@ var __defProp = Object.defineProperty; | ||
} | ||
var ToolkitListNestMode; | ||
(function(ToolkitListNestMode2) { | ||
ToolkitListNestMode2["Html"] = "html"; | ||
ToolkitListNestMode2["Direct"] = "direct"; | ||
})(ToolkitListNestMode || (ToolkitListNestMode = {})); | ||
export { ToolkitListNestMode, buildMarksTree, isPortableTextBlock, isPortableTextListItemBlock, isPortableTextSpan, isPortableTextToolkitList, isPortableTextToolkitSpan, isPortableTextToolkitTextNode, nestLists, sortMarksByOccurences, spanToPlainText, toPlainText }; | ||
const LIST_NEST_MODE_HTML = "html"; | ||
const LIST_NEST_MODE_DIRECT = "direct"; | ||
export { LIST_NEST_MODE_DIRECT, LIST_NEST_MODE_HTML, buildMarksTree, isPortableTextBlock, isPortableTextListItemBlock, isPortableTextSpan, isPortableTextToolkitList, isPortableTextToolkitSpan, isPortableTextToolkitTextNode, nestLists, sortMarksByOccurences, spanToPlainText, toPlainText }; | ||
//# sourceMappingURL=portable-text-toolkit.esm.js.map |
{ | ||
"name": "@portabletext/toolkit", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Toolkit of handy utility functions for dealing with Portable Text", | ||
@@ -5,0 +5,0 @@ "main": "./dist/portable-text-toolkit.cjs.js", |
@@ -36,3 +36,3 @@ import type {PortableTextBlock, PortableTextListItemBlock, TypedObject} from '@portabletext/types' | ||
* | ||
* These modes are defined in the {@link ToolkitListNestMode} enum. | ||
* These modes are available as {@link LIST_NEST_MODE_HTML} and {@link LIST_NEST_MODE_DIRECT}. | ||
* | ||
@@ -45,7 +45,7 @@ * @param blocks - Array of Portable Text blocks and other arbitrary types | ||
blocks: T[], | ||
mode: ToolkitListNestMode.Direct | ||
mode: 'direct' | ||
): (T | ToolkitPortableTextDirectList)[] | ||
export function nestLists<T extends TypedObject = PortableTextBlock | TypedObject>( | ||
blocks: T[], | ||
mode: ToolkitListNestMode.Html | ||
mode: 'html' | ||
): (T | ToolkitPortableTextHtmlList)[] | ||
@@ -52,0 +52,0 @@ export function nestLists<T extends TypedObject = PortableTextBlock | TypedObject>( |
@@ -8,11 +8,16 @@ import type { | ||
/* eslint-disable no-shadow, no-unused-vars */ | ||
/** | ||
* List nesting mode for HTML, see the {@link nestLists | `nestLists()` function} | ||
*/ | ||
export const LIST_NEST_MODE_HTML = 'html' | ||
/** | ||
* List nesting mode for direct, nested lists, see the {@link nestLists | `nestLists()` function} | ||
*/ | ||
export const LIST_NEST_MODE_DIRECT = 'direct' | ||
/** | ||
* List nesting mode, see the {@link nestLists | `nestLists()` function} | ||
*/ | ||
export enum ToolkitListNestMode { | ||
Html = 'html', | ||
Direct = 'direct', | ||
} | ||
/* eslint-enable no-shadow, no-unused-vars */ | ||
export type ToolkitListNestMode = 'html' | 'direct' | ||
@@ -19,0 +24,0 @@ /** |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
963
49973