@portabletext/toolkit
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -1,6 +0,6 @@ | ||
"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(` | ||
"use strict";var w=Object.defineProperty,A=Object.defineProperties;var B=Object.getOwnPropertyDescriptors;var x=Object.getOwnPropertySymbols;var K=Object.prototype.hasOwnProperty,C=Object.prototype.propertyIsEnumerable;var _=(e,t,n)=>t in e?w(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,g=(e,t)=>{for(var n in t||(t={}))K.call(t,n)&&_(e,n,t[n]);if(x)for(var n of x(t))C.call(t,n)&&_(e,n,t[n]);return e},I=(e,t)=>A(e,B(t));Object.defineProperty(exports,"__esModule",{value:!0});exports[Symbol.toStringTag]="Module";function h(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 P(e){return k(e)&&"listItem"in e&&typeof e.listItem=="string"&&(typeof e.level=="undefined"||typeof e.level=="number")}function L(e){return e._type==="@list"}function S(e){return e._type==="@span"}function b(e){return e._type==="@text"}const v=["strong","em","code","underline","strike-through"];function M(e,t,n){if(!h(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(c&&h(c)&&Array.isArray(c.marks)&&c.marks.indexOf(i)!==-1)l[i]++;else break}}),r.sort((i,s)=>N(l,i,s))}function N(e,t,n){const r=e[t],l=e[n];if(r!==l)return l-r;const i=v.indexOf(t),s=v.indexOf(n);return i!==s?i-s:t.localeCompare(n)}function j(e){var s;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 c=0;c<t.length;c++){const f=t[c];if(!f)continue;const T=r[c]||[];let p=1;if(i.length>1)for(p;p<i.length;p++){const a=((s=i[p])==null?void 0:s.markKey)||"",o=T.indexOf(a);if(o===-1)break;T.splice(o,1)}i=i.slice(0,p);let u=i[i.length-1];if(!!u){for(const a of T){const o=n.find(D=>D._key===a),O=o?o._type:a,d={_type:"@span",_key:f._key,children:[],markDef:o,markType:O,markKey:a};u.children.push(d),i.push(d),u=d}if(h(f)){const a=f.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+=` | ||
`);u.children=u.children.concat(a.map(o=>({_type:"@text",text:o})))}else u.children=u.children.concat(f)}}return l.children}function H(e,t){const n=[];let r;for(let l=0;l<e.length;l++){const i=e[l];if(!!i){if(!P(i)){n.push(i),r=void 0;continue}if(!r){r=y(i,l,t),n.push(r);continue}if(R(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],f=I(g({},c),{children:[...c.children,s]});r.children[r.children.length-1]=f}else r.children.push(s);r=s;continue}if((i.level||1)<r.level){const s=n[n.length-1],c=s&&m(s,i);if(c){r=c,r.children.push(i);continue}r=y(i,l,t),n.push(r);continue}if(i.listItem!==r.listItem){const s=n[n.length-1],c=s&&m(s,{level:i.level||1});if(c&&c.listItem===i.listItem){r=c,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 R(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(L(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&&!h(i)?m(i,t):void 0}const $=/^\s/,F=/^\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=>{h(s)?(n+=i&&n&&!F.test(n)&&!$.test(s.text)?" ":"",n+=s.text,i=!1):i=!0}),l!==t.length-1&&(n+=` | ||
`)}),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; | ||
`)}),n}function E(e){let t="";return e.children.forEach(n=>{b(n)?t+=n.text:S(n)&&(t+=E(n))}),t}const q="html",z="direct";exports.LIST_NEST_MODE_DIRECT=z;exports.LIST_NEST_MODE_HTML=q;exports.buildMarksTree=j;exports.isPortableTextBlock=k;exports.isPortableTextListItemBlock=P;exports.isPortableTextSpan=h;exports.isPortableTextToolkitList=L;exports.isPortableTextToolkitSpan=S;exports.isPortableTextToolkitTextNode=b;exports.nestLists=H;exports.sortMarksByOccurences=M;exports.spanToPlainText=E;exports.toPlainText=U; | ||
//# sourceMappingURL=portable-text-toolkit.cjs.js.map |
@@ -52,3 +52,3 @@ var __defProp = Object.defineProperty; | ||
const sibling = blockChildren[siblingIndex]; | ||
if (isPortableTextSpan(sibling) && Array.isArray(sibling.marks) && sibling.marks.indexOf(mark) !== -1) { | ||
if (sibling && isPortableTextSpan(sibling) && Array.isArray(sibling.marks) && sibling.marks.indexOf(mark) !== -1) { | ||
occurences[mark]++; | ||
@@ -76,2 +76,3 @@ } else { | ||
function buildMarksTree(block) { | ||
var _a; | ||
const { children, markDefs = [] } = block; | ||
@@ -90,7 +91,10 @@ if (!children || !children.length) { | ||
const span = children[i]; | ||
const marksNeeded = sortedMarks[i]; | ||
if (!span) { | ||
continue; | ||
} | ||
const marksNeeded = sortedMarks[i] || []; | ||
let pos = 1; | ||
if (nodeStack.length > 1) { | ||
for (pos; pos < nodeStack.length; pos++) { | ||
const mark = nodeStack[pos].markKey || ""; | ||
const mark = ((_a = nodeStack[pos]) == null ? void 0 : _a.markKey) || ""; | ||
const index = marksNeeded.indexOf(mark); | ||
@@ -105,2 +109,5 @@ if (index === -1) { | ||
let currentNode = nodeStack[nodeStack.length - 1]; | ||
if (!currentNode) { | ||
continue; | ||
} | ||
for (const markKey of marksNeeded) { | ||
@@ -138,2 +145,5 @@ const markDef = markDefs.find((def) => def._key === markKey); | ||
const block = blocks[i]; | ||
if (!block) { | ||
continue; | ||
} | ||
if (!isPortableTextListItemBlock(block)) { | ||
@@ -168,3 +178,4 @@ tree.push(block); | ||
if ((block.level || 1) < currentList.level) { | ||
const match = findListMatching(tree[tree.length - 1], block); | ||
const matchingBranch = tree[tree.length - 1]; | ||
const match = matchingBranch && findListMatching(matchingBranch, block); | ||
if (match) { | ||
@@ -180,3 +191,4 @@ currentList = match; | ||
if (block.listItem !== currentList.listItem) { | ||
const match = findListMatching(tree[tree.length - 1], { level: block.level || 1 }); | ||
const matchingBranch = tree[tree.length - 1]; | ||
const match = matchingBranch && findListMatching(matchingBranch, { level: block.level || 1 }); | ||
if (match && match.listItem === block.listItem) { | ||
@@ -183,0 +195,0 @@ currentList = match; |
{ | ||
"name": "@portabletext/toolkit", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Toolkit of handy utility functions for dealing with Portable Text", | ||
@@ -43,8 +43,8 @@ "main": "./dist/portable-text-toolkit.cjs.js", | ||
"dependencies": { | ||
"@portabletext/types": "^1.0.0" | ||
"@portabletext/types": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"@types/tap": "^15.0.5", | ||
"@typescript-eslint/eslint-plugin": "^5.10.2", | ||
"@typescript-eslint/parser": "^5.10.2", | ||
"@typescript-eslint/eslint-plugin": "^5.11.0", | ||
"@typescript-eslint/parser": "^5.11.0", | ||
"esbuild-register": "^3.2.1", | ||
@@ -58,4 +58,4 @@ "eslint": "^8.8.0", | ||
"typedoc": "^0.22.11", | ||
"typescript": "^4.5.4", | ||
"vite": "^2.7.4", | ||
"typescript": "^4.5.5", | ||
"vite": "^2.8.1", | ||
"vite-dts": "^1.0.4" | ||
@@ -62,0 +62,0 @@ }, |
@@ -61,4 +61,7 @@ import type { | ||
const span = children[i] | ||
if (!span) { | ||
continue | ||
} | ||
const marksNeeded = sortedMarks[i] | ||
const marksNeeded = sortedMarks[i] || [] | ||
let pos = 1 | ||
@@ -69,3 +72,3 @@ | ||
for (pos; pos < nodeStack.length; pos++) { | ||
const mark = nodeStack[pos].markKey || '' | ||
const mark = nodeStack[pos]?.markKey || '' | ||
const index = marksNeeded.indexOf(mark) | ||
@@ -86,2 +89,6 @@ | ||
let currentNode = nodeStack[nodeStack.length - 1] | ||
if (!currentNode) { | ||
continue | ||
} | ||
for (const markKey of marksNeeded) { | ||
@@ -88,0 +95,0 @@ const markDef = markDefs.find((def) => def._key === markKey) |
@@ -63,2 +63,6 @@ import type {PortableTextBlock, PortableTextListItemBlock, TypedObject} from '@portabletext/types' | ||
const block = blocks[i] | ||
if (!block) { | ||
continue | ||
} | ||
if (!isPortableTextListItemBlock(block)) { | ||
@@ -120,3 +124,4 @@ tree.push(block) | ||
// Current list has ended, and we need to hook up with a parent of the same level and type | ||
const match = findListMatching(tree[tree.length - 1], block) | ||
const matchingBranch = tree[tree.length - 1] | ||
const match = matchingBranch && findListMatching(matchingBranch, block) | ||
if (match) { | ||
@@ -136,3 +141,4 @@ currentList = match | ||
if (block.listItem !== currentList.listItem) { | ||
const match = findListMatching(tree[tree.length - 1], {level: block.level || 1}) | ||
const matchingBranch = tree[tree.length - 1] | ||
const match = matchingBranch && findListMatching(matchingBranch, {level: block.level || 1}) | ||
if (match && match.listItem === block.listItem) { | ||
@@ -139,0 +145,0 @@ currentList = match |
@@ -66,2 +66,3 @@ import type {PortableTextSpan, TypedObject} from '@portabletext/types' | ||
if ( | ||
sibling && | ||
isPortableTextSpan(sibling) && | ||
@@ -81,3 +82,7 @@ Array.isArray(sibling.marks) && | ||
function sortMarks(occurences: Record<string, number>, markA: string, markB: string): number { | ||
function sortMarks<U extends string, T extends Record<U, number>>( | ||
occurences: T, | ||
markA: U, | ||
markB: U | ||
): number { | ||
const aOccurences = occurences[markA] | ||
@@ -84,0 +89,0 @@ const bOccurences = occurences[markB] |
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
51054
995
Updated@portabletext/types@^1.0.3