You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{visit}from'@jote/utils'import{Text,Root,Node,P,R}from'ooxast'import{convertElement,isElement}from'xast-util-is-element'import{select}from'xast-util-select'import{removePosition}from'unist-util-remove-position'import{selectasunistSelect}from'unist-util-select'import{remove}from'unist-util-remove'// Check to see if node is a paragraph, because we want to merge elements in a paragraphconstisP=convertElement<P>('w:p')constisR=convertElement<R>('w:r')exportinterfaceOptions{rPrRemoveList?: string[]}exportfunctionooxastUtilRemoveRsid(tree: Root,options?: Options): RootexportfunctionooxastUtilRemoveRsid(tree: Node,options?: Options): NodeexportfunctionooxastUtilRemoveRsid(tree: Root|Node,options?: Options): Root|Node{visit(tree,isP,(node: P)=>{// Clean rsid props from Pconst{'w:rsidR': rr,'w:rsidRPr': rpr,'w:rsidRDefault': rd,'w:rsidP': rp,
...rest}=node.attributesnode.attributes=restasanyconstkids=node.childrenconstruns: typeofkids=[]for(constkidofkids){letlastEl=runs[runs.length-1]if(!isR(kid)){runs.push(kid)continue}letel=removeRsid(kid)el=maybeRemoveRunProperties(el,options?.rPrRemoveList)if(lastEl?.name!=='w:r'){runs.push(el)continue}constlastT=select('w\\:t',lastEl)constt=select('w\\:t',el)// If either one of them don't have text, don't merge them ya dummyif(!lastT||!t){runs.push(el)continue}constpr=select('w\\:rPr',el)constlastRPr=select('w\\:rPr',lastEl)//null caseif(!lastRPr&&!pr){lastEl=merge(lastEl,el)continue}// one of them has props while the other doesn't, don't merge// TODO: remove unnecssary properties first, that way this will equal more oftenif((!lastRPr&&pr)||(lastRPr&&!pr)){runs.push(el)continue}// they both have properties but they differ, e.g. one is italic, the other bold. Should not merge// string check otherwise === don't workif(JSON.stringify(removePosition(lastRPr!,true))!==JSON.stringify(removePosition(pr!,true))){runs.push(el)continue}lastEl=merge(lastEl,el)}node.children=runs})returntree}functionmaybeRemoveRunProperties(r: R,options: string[]|undefined): R{if(!options){returnr}remove(r,(element)=>isElement(element)&&options.includes(element.name))returnr}/** * Merge two runs into an old one by concatenating the text properties. * If they don't have text, don't merge them. */functionmerge(prev: R,curr: R): R{constlastText=unistSelect('text',prev)asTextconsttext=unistSelect('text',curr)asTextif(!lastText){returncurr}if(!text){returnprev}lastText.value+=text.valuereturnprev}functionremoveRsid(node: R): R{const{'w:rsidRPr': rp,'w:rsidR': r, ...rest}=node.attributesnode.attributes=restasanyreturnnode}
cf0a6ee32e0f85798ed5ff674890033d2ad76cfd
The text was updated successfully, but these errors were encountered:
remove unnecssary properties first, that way this will equal more often
string check otherwise === don't work
https://github.com/JournalOfTrialAndError/JOTE/blob/59c8623885f0330e9c945306e09214b5fb378d5b/libs/ooxast/ooxast-util-remove-rsid/src/lib/ooxast-util-remove-rsid.ts#L71
cf0a6ee32e0f85798ed5ff674890033d2ad76cfd
The text was updated successfully, but these errors were encountered: