UNPKG

text-annotator-v2

Version:

A JavaScript library for locating and annotating plain text in HTML

1 lines 3.14 kB
var TextAnnotator;(()=>{"use strict";var t={590:(t,e,n)=>{Object.defineProperty(e,"__esModule",{value:!0});const s=n(137);t.exports=s.default},137:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.default=class{constructor(t){this.html="",this.text="",this.tags=[],this.annotations=[],this.html=t;const{text:e,tags:n}=this._stripHTMLTags(t);this.text=e,this.tags=n}search(t,{prefix:e="",postfix:n="",trim:s=!0,caseSensitive:i=!1,offset:a=0}={}){const{text:l,annotations:o}=this;let h=e+t+n;h=s?h.trim():h,h=i?h:h.toLowerCase();const r=(i?l:l.toLowerCase()).indexOf(h,a),g=s?e.replace(/^\s+/,"").length:e.length,c=s?n.replace(/\s+$/,"").length:n.length;return-1===r?-1:o.push({index:r+g,length:h.substring(g,h.length-c).length})-1}searchAll(t,e){let n=0;const s=[];let i=-1;const a=Object.assign({},e);do{i=this.search(t,a),-1!==i&&(n=this.annotations[i].index+1,a.offset=n,s.push(i))}while(-1!==i);return s}annotate(t,{tagName:e="span",baseClassName:n="annotation",classPattern:s="annotation-"}={}){const{tags:i,annotations:a,_insert:l,_binaryInsert:o}=this,h=a[t],r=[h.index,h.index+h.length,0],g=[[...r]];for(let t=0;t<i.length;t++){const{index:e,length:n}=i[t];if(e<=r[0])g[0][2]+=n;else{if(!(e<r[1]))break;{const s=0===t?0:i[t-1].index,a=g[g.length-1];if(e===s)a[2]+=n;else{const t=a[1];a[1]=e,g.push([a[1],t,a[2]+n])}}}}const c=`<${e} class="${n} ${s}${t}">`,x=`</${e}>`,d=c.length,f=x.length;let u=0;for(let e=0;e<g.length;e++){const n=g[e];o(i,{index:n[0],length:d,isCloseTag:!1,annotationIndex:t},((t,e)=>t.index<=e.index?-1:1)),o(i,{index:n[1],length:f,isCloseTag:!0,annotationIndex:t},((t,e)=>t.index-e.index)),this.html=l(this.html,c,n[0]+n[2]+u),this.html=l(this.html,x,n[1]+n[2]+u+d),u+=d+f}return this.html}annotateAll(t,e){return t.forEach((t=>{this.annotate(t,e)})),this.html}unannotate(t){const e=[],n=this.tags.filter(((n,s)=>(n.annotationIndex===t&&e.push(s),n.annotationIndex===t))),s=this.tags.filter((e=>e.annotationIndex!==t));for(let t=0;t<n.length;t++){const i=n[t];let a=0;for(let t=0;t<s.length;t++){const e=s[t];if(i.index<e.index)break;i.index>e.index?a+=e.length:void 0===e.annotationIndex?i.isCloseTag||(a+=e.length):i.annotationIndex<e.annotationIndex?e.isCloseTag&&(a+=e.length):i.isCloseTag||(a+=e.length)}this.html=this.html.slice(0,i.index+a)+this.html.slice(i.index+a+i.length),this.tags.splice(e[t]-t,1)}return this.html}unannotateAll(t){return t.forEach((t=>{this.unannotate(t)})),this.html}_stripHTMLTags(t){let e=t;const n=[];let s;const i=/<[^>]+>/;for(;s=e.match(i);)e=e.replace(s[0],""),n.push({index:s.index,length:s[0].length,isCloseTag:s[0].startsWith("</")});return{text:e,tags:n}}_insert(t,e,n){return t.slice(0,n)+e+t.slice(n)}_binaryInsert(t,e,n){if(0===t.length||n(t[0],e)>=0)return t.splice(0,0,e),t;if(t.length>0&&n(t[t.length-1],e)<=0)return t.splice(t.length,0,e),t;let s=0,i=t.length,a=0,l=i;for(;s<i;){const o=Math.floor((i+s)/2),h=n(t[o],e);if(h<0?s=o:h>0?i=o:(i=o,s=o),a===s&&l===i)break;a=s,l=i}return t.splice(i,0,e),t}}}},e={},n=function n(s){var i=e[s];if(void 0!==i)return i.exports;var a=e[s]={exports:{}};return t[s](a,a.exports,n),a.exports}(590);TextAnnotator=n})();