@edemaine/codemirror-spell-checker
Version:
Tweaked spell checker for CodeMirror
7 lines • 10.6 kB
JavaScript
/**
* @edemaine/codemirror-spell-checker v0.0.1
* Copyright
* @link https://github.com/edemaine/codemirror-spell-checker
* @license MIT
*/
!function(e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).CodeMirrorSpellChecker=e()}(function(){return function o(i,r,s){function a(t,e){if(!r[t]){if(!i[t]){var n="function"==typeof require&&require;if(!e&&n)return n(t,!0);if(l)return l(t,!0);throw(e=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",e}n=r[t]={exports:{}},i[t][0].call(n.exports,function(e){return a(i[t][1][e]||e)},n,n.exports,o,i,r,s)}return r[t].exports}for(var l="function"==typeof require&&require,e=0;e<s.length;e++)a(s[e]);return a}({1:[function(e,t,n){},{}],2:[function(r,t,e){!function(h){!function(){var e;!function(){"use strict";(e=function(e,i,r,s){s=s||{},this.dictionary=null,this.rules={},this.dictionaryTable={},this.compoundRules=[],this.compoundRuleCodes={},this.replacementTable=[],this.flags=s.flags||{},this.memoized={},this.loaded=!1;var t,a,l,u,c,f=this;function n(e,t){e=f._readFile(e,null,s.asyncLoad);s.asyncLoad?e.then(function(e){t(e)}):t(e)}function o(e){i=e,r&&p()}function d(e){r=e,i&&p()}function p(){for(f.rules=f._parseAFF(i),f.compoundRuleCodes={},a=0,u=f.compoundRules.length;a<u;a++){var e=f.compoundRules[a];for(l=0,c=e.length;l<c;l++)f.compoundRuleCodes[e[l]]=[]}for(a in"ONLYINCOMPOUND"in f.flags&&(f.compoundRuleCodes[f.flags.ONLYINCOMPOUND]=[]),f.dictionaryTable=f._parseDIC(r),f.compoundRuleCodes)0===f.compoundRuleCodes[a].length&&delete f.compoundRuleCodes[a];for(a=0,u=f.compoundRules.length;a<u;a++){var t=f.compoundRules[a],n="";for(l=0,c=t.length;l<c;l++){var o=t[l];o in f.compoundRuleCodes?n+="("+f.compoundRuleCodes[o].join("|")+")":n+=o}f.compoundRules[a]=new RegExp(n,"i")}f.loaded=!0,s.asyncLoad&&s.loadedCallback&&s.loadedCallback(f)}return e&&(f.dictionary=e,i&&r?p():"undefined"!=typeof window&&"chrome"in window&&"extension"in window.chrome&&"getURL"in window.chrome.extension?(t=s.dictionaryPath||"typo/dictionaries",i||n(chrome.extension.getURL(t+"/"+e+"/"+e+".aff"),o),r||n(chrome.extension.getURL(t+"/"+e+"/"+e+".dic"),d)):(t=s.dictionaryPath||(void 0!==h?h+"/dictionaries":"./dictionaries"),i||n(t+"/"+e+"/"+e+".aff",o),r||n(t+"/"+e+"/"+e+".dic",d))),this}).prototype={load:function(e){for(var t in e)e.hasOwnProperty(t)&&(this[t]=e[t]);return this},_readFile:function(e,t,n){var o,i;if(t=t||"utf8","undefined"!=typeof XMLHttpRequest)return(i=new XMLHttpRequest).open("GET",e,n),n&&(o=new Promise(function(e,t){i.onload=function(){200===i.status?e(i.responseText):t(i.statusText)},i.onerror=function(){t(i.statusText)}})),i.overrideMimeType&&i.overrideMimeType("text/plain; charset="+t),i.send(null),n?o:i.responseText;if(void 0!==r){n=r("fs");try{if(n.existsSync(e))return n.readFileSync(e,t);console.log("Path "+e+" does not exist.")}catch(e){return console.log(e),""}}},_parseAFF:function(e){for(var t,n={},o=e.split(/\r?\n/),i=0,r=o.length;i<r;i++)if(t=(t=this._removeAffixComments(o[i])).trim()){var s=t.split(/\s+/),a=s[0];if("PFX"==a||"SFX"==a){for(var l,u=s[1],c=s[2],f=[],d=i+1,p=i+1+(l=parseInt(s[3],10));d<p;d++){var h,g=(h=o[d].split(/\s+/))[2],m=h[3].split("/"),v=m[0],m=("0"===v&&(v=""),this.parseRuleCodes(m[1])),y=h[4],C={};C.add=v,0<m.length&&(C.continuationClasses=m),"."!==y&&(C.match="SFX"===a?new RegExp(y+"$"):new RegExp("^"+y)),"0"!=g&&(C.remove="SFX"===a?new RegExp(g+"$"):g),f.push(C)}n[u]={type:a,combineable:"Y"==c,entries:f},i+=l}else if("COMPOUNDRULE"===a){for(p=(d=i+1)+(l=parseInt(s[1],10));d<p;d++)h=(t=o[d]).split(/\s+/),this.compoundRules.push(h[1]);i+=l}else"REP"===a?3===(h=t.split(/\s+/)).length&&this.replacementTable.push([h[1],h[2]]):this.flags[a]=s[1]}return n},_removeAffixComments:function(e){return e.match(/^\s*#/,"")?"":e},_parseDIC:function(e){var t=(e=this._removeDicComments(e)).split(/\r?\n/),n={};function o(e,t){n.hasOwnProperty(e)||(n[e]=null),0<t.length&&(null===n[e]&&(n[e]=[]),n[e].push(t))}for(var i=1,r=t.length;i<r;i++){var s=t[i];if(s){var s=s.split("/",2),a=s[0];if(1<s.length){var l=this.parseRuleCodes(s[1]);"NEEDAFFIX"in this.flags&&-1!=l.indexOf(this.flags.NEEDAFFIX)||o(a,l);for(var u=0,c=l.length;u<c;u++){var f=l[u],d=this.rules[f];if(d)for(var p=this._applyRule(a,d),h=0,g=p.length;h<g;h++){var m=p[h];if(o(m,[]),d.combineable)for(var v=u+1;v<c;v++){var y=l[v],y=this.rules[y];if(y&&y.combineable&&d.type!=y.type)for(var C=this._applyRule(m,y),b=0,R=C.length;b<R;b++)o(C[b],[])}}f in this.compoundRuleCodes&&this.compoundRuleCodes[f].push(a)}}else o(a.trim(),[])}}return n},_removeDicComments:function(e){return e=e.replace(/^\t.*$/gm,"")},parseRuleCodes:function(e){if(e){if("FLAG"in this.flags){if("long"!==this.flags.FLAG)return"num"===this.flags.FLAG?e.split(","):"UTF-8"===this.flags.FLAG?Array.from(e):e.split("");for(var t=[],n=0,o=e.length;n<o;n+=2)t.push(e.substr(n,2));return t}return e.split("")}return[]},_applyRule:function(e,t){for(var n=t.entries,o=[],i=0,r=n.length;i<r;i++){var s=n[i];if(!s.match||e.match(s.match)){var a=e;if(s.remove&&(a=a.replace(s.remove,"")),"SFX"===t.type?a+=s.add:a=s.add+a,o.push(a),"continuationClasses"in s)for(var l=0,u=s.continuationClasses.length;l<u;l++){var c=this.rules[s.continuationClasses[l]];c&&(o=o.concat(this._applyRule(a,c)))}}}return o},check:function(e){if(!this.loaded)throw"Dictionary not loaded.";e=e.replace(/^\s\s*/,"").replace(/\s\s*$/,"");if(this.checkExact(e))return!0;if(e.toUpperCase()===e){var t=e[0]+e.substring(1).toLowerCase();if(this.hasFlag(t,"KEEPCASE"))return!1;if(this.checkExact(t))return!0;if(this.checkExact(e.toLowerCase()))return!0}t=e[0].toLowerCase()+e.substring(1);if(t!==e){if(this.hasFlag(t,"KEEPCASE"))return!1;if(this.checkExact(t))return!0}return!1},checkExact:function(e){if(!this.loaded)throw"Dictionary not loaded.";var t,n,o=this.dictionaryTable[e];if(void 0===o){if("COMPOUNDMIN"in this.flags&&e.length>=this.flags.COMPOUNDMIN)for(t=0,n=this.compoundRules.length;t<n;t++)if(e.match(this.compoundRules[t]))return!0}else{if(null===o)return!0;if("object"==typeof o)for(t=0,n=o.length;t<n;t++)if(!this.hasFlag(e,"ONLYINCOMPOUND",o[t]))return!0}return!1},hasFlag:function(e,t,n){if(this.loaded)return!!(t in this.flags&&(n=void 0===n?Array.prototype.concat.apply([],this.dictionaryTable[e]):n)&&-1!==n.indexOf(this.flags[t]));throw"Dictionary not loaded."},alphabet:"",suggest:function(e,u){if(!this.loaded)throw"Dictionary not loaded.";if(u=u||5,this.memoized.hasOwnProperty(e)){var t=this.memoized[e].limit;if(u<=t||this.memoized[e].suggestions.length<t)return this.memoized[e].suggestions.slice(0,u)}if(this.check(e))return[];for(var n=0,o=this.replacementTable.length;n<o;n++){var i=this.replacementTable[n];if(-1!==e.indexOf(i[0])){i=e.replace(i[0],i[1]);if(this.check(i))return[i]}}if(!this.alphabet){this.alphabet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ","TRY"in this.flags&&(this.alphabet+=this.flags.TRY),"WORDCHARS"in this.flags&&(this.alphabet+=this.flags.WORDCHARS);for(var r=this.alphabet.split(""),s=(r.sort(),{}),n=0;n<r.length;n++)s[r[n]]=!0;for(n in this.alphabet="",s)this.alphabet+=n}var d=this;function c(e,t){var n,o,i,r,s={},a=d.alphabet.length;for(r in"string"==typeof e&&(r=e,(e={})[r]=!0),e)for(n=0,o=r.length+1;n<o;n++){var l=[r.substring(0,n),r.substring(n)];if(l[1]&&(i=l[0]+l[1].substring(1),t&&!d.check(i)||(i in s?s[i]+=1:s[i]=1)),1<l[1].length&&l[1][1]!==l[1][0]&&(i=l[0]+l[1][1]+l[1][0]+l[1].substring(2),t&&!d.check(i)||(i in s?s[i]+=1:s[i]=1)),l[1])for(var u=l[1].substring(0,1).toUpperCase()===l[1].substring(0,1)?"uppercase":"lowercase",c=0;c<a;c++){var f=d.alphabet[c];(f="uppercase"===u?f.toUpperCase():f)!=l[1].substring(0,1)&&(i=l[0]+f+l[1].substring(1),t&&!d.check(i)||(i in s?s[i]+=1:s[i]=1))}if(l[1])for(c=0;c<a;c++){u=l[0].substring(-1).toUpperCase()===l[0].substring(-1)&&l[1].substring(0,1).toUpperCase()===l[1].substring(0,1)?"uppercase":"lowercase",f=d.alphabet[c];"uppercase"===u&&(f=f.toUpperCase()),i=l[0]+f+l[1],t&&!d.check(i)||(i in s?s[i]+=1:s[i]=1)}}return s}return this.memoized[e]={suggestions:function(e){var t,n=c(e),o=c(n,!0);for(t in n)d.check(t)&&(t in o?o[t]+=n[t]:o[t]=n[t]);var i=[];for(l in o)o.hasOwnProperty(l)&&i.push([l,o[l]]);i.sort(function(e,t){var n=e[1],o=t[1];return n<o?-1:o<n?1:t[0].localeCompare(e[0])}).reverse();for(var r=[],s="lowercase",a=(e.toUpperCase()===e?s="uppercase":e.substr(0,1).toUpperCase()+e.substr(1).toLowerCase()===e&&(s="capitalized"),u),l=0;l<Math.min(a,i.length);l++)"uppercase"===s?i[l][0]=i[l][0].toUpperCase():"capitalized"===s&&(i[l][0]=i[l][0].substr(0,1).toUpperCase()+i[l][0].substr(1)),d.hasFlag(i[l][0],"NOSUGGEST")||-1!=r.indexOf(i[l][0])?a++:r.push(i[l][0]);return r}(e),limit:u},this.memoized[e].suggestions}}}(),void 0!==t&&(t.exports=e)}.call(this)}.call(this,"/node_modules/typo-js")},{fs:1}],3:[function(e,t,n){"use strict";var l=e("typo-js");function u(a){"function"!=typeof(a=a||{}).codeMirrorInstance||"function"!=typeof a.codeMirrorInstance.defineMode?console.log("CodeMirror Spell Checker: You must provide an instance of CodeMirror via the option `codeMirrorInstance`"):(String.prototype.includes||(String.prototype.includes=function(){return-1!==String.prototype.indexOf.apply(this,arguments)}),a.codeMirrorInstance.defineMode("spell-checker",function(e){u.aff_loading||(u.aff_loading=!0,(t=new XMLHttpRequest).open("GET","https://cdn.jsdelivr.net/npm/hunspell-dict-en-us@0.1.0/en-us.aff",!0),t.onload=function(){4===t.readyState&&200===t.status&&(u.aff_data=t.responseText,u.num_loaded++,2==u.num_loaded)&&(u.typo=new l("en_US",u.aff_data,u.dic_data,{platform:"any"}))},t.send(null)),u.dic_loading||(u.dic_loading=!0,(n=new XMLHttpRequest).open("GET","https://cdn.jsdelivr.net/npm/hunspell-dict-en-us@0.1.0/en-us.dic",!0),n.onload=function(){4===n.readyState&&200===n.status&&(u.dic_data=n.responseText,u.num_loaded++,2==u.num_loaded)&&(u.typo=new l("en_US",u.aff_data,u.dic_data,{platform:"any"}))},n.send(null));var t,n,o=/^[^!"#$%&()*+,\-./:;<=>?@[\\\]^_`{|}~\s\u00a0-\uffff]+/,i=/^[0-9]+D?$/,r=[],s=(a.customWords&&a.customWords instanceof Array&&(r=a.customWords),{token:function(e){var t=e.match(o,!0);if(console.log(t),t){if(!(t=t[0]).match(i)&&u.typo&&!u.typo.check(t)&&!~r.indexOf(t))return"spell-error"}else e.next();return null}}),e=a.codeMirrorInstance.getMode(e,e.backdrop||"text/plain");return a.codeMirrorInstance.overlayMode(e,s,!0)}))}u.num_loaded=0,u.aff_loading=!1,u.dic_loading=!1,u.aff_data="",u.dic_data="",t.exports=u},{"typo-js":2}]},{},[3])(3)});