UNPKG

unified-doc

Version:
3 lines (2 loc) 4.44 kB
import t from"unified-doc-search-micromatch";import r from"vfile";import n from"hast-util-raw";import e from"hast-util-to-html";import i from"hast-util-to-mdast";import o from"hast-util-to-xast";import a from"mdast-util-gfm";import u from"mdast-util-to-markdown";import s from"mime-types";import c from"xast-util-to-xml";import f from"hast-util-to-string";import l from"deepmerge";import m from"hast-util-sanitize";import p from"hast-util-sanitize/lib/github.json";import d from"rehype-parse";import h from"rehype-stringify";import v from"remark-parse";import g from"remark-rehype";import x from"unified";import y from"unified-doc-parse-code-block";import k from"unified-doc-parse-csv";import b from"unified-doc-util-mark";var P,w=O;function O(t){if("string"==typeof t)return function(t){return function(r){return Boolean(r&&r.type===t)}}(t);if(null==t)return z;if("object"==typeof t)return("length"in t?j:C)(t);if("function"==typeof t)return t;throw new Error("Expected function, string, or object as test")}function C(t){return function(r){var n;for(n in t)if(r[n]!==t[n])return!1;return!0}}function j(t){var r=function(t){for(var r=[],n=t.length,e=-1;++e<n;)r[e]=O(t[e]);return r}(t),n=r.length;return function(){for(var t=-1;++t<n;)if(r[t].apply(this,arguments))return!0;return!1}}function z(){return!0}function A(t){return"head"!==t.tagName}function S(t){return f((r=t,n=A,e||(e=n,n={}),o=null==(o=n.cascade)||o,i=w(e),function t(r,n,e){var a,u,s,c;return i(r,n,e)?(a=Object.keys(r).reduce(function(t,n){return"children"!==n&&(t[n]=r[n]),t},{}),r.children&&(a.children=(u=r.children,s=function(n,e){return t(n,e,r)},c=[],Array.isArray(u)?(u.forEach(function(t,r,n){var e=s.call(void 0,t,r,n);Array.isArray(e)?c.push.apply(c,e):null!=e&&c.push(e)}),c):c),o&&0!==r.children.length&&0===a.children.length)?null:a):null}(r,null,null)));var r,n,e,i,o}function E(t){return s.lookup(t)||"text/plain"}function M(){return(M=Object.assign||function(t){for(var r=1;r<arguments.length;r++){var n=arguments[r];for(var e in n)Object.prototype.hasOwnProperty.call(n,e)&&(t[e]=n[e])}return t}).apply(this,arguments)}var N=((P={})["text/csv"]=[[k,{header:!0}]],P["text/html"]=[d],P["text/markdown"]=[v,g],P),R=function(t){return function(){var r=arguments;return function(n){return t.apply(void 0,[n].concat([].slice.call(r)))}}};function B(t,r){r.data.textContent=S(t)}export default function(s){void 0===s&&(s={});var f=s.compiler,d=s.marks,v=void 0===d?[]:d,g=s.parsers,k=void 0===g?{}:g,P=s.postPlugins,w=void 0===P?[]:P,O=s.prePlugins,C=void 0===O?[]:O,j=s.sanitizeSchema,z=void 0===j?{}:j,A=s.searchAlgorithm,L=void 0===A?t:A,Q=s.searchOptions,q=void 0===Q?{}:Q,D=r({contents:s.content,basename:s.filename}),F=function(t){void 0===t&&(t={});var r=t.compiler,n=void 0===r?h:r,e=t.marks,i=void 0===e?[]:e,o=t.parsers,a=t.postPlugins,u=void 0===a?[]:a,s=t.prePlugins,c=void 0===s?[]:s,f=t.sanitizeSchema,d=t.vfile,v=M({},N,void 0===o?N:o),g=[[y,{language:d.extname.slice(1)}]],k=x(),P=E(d.basename);function w(){return k.processSync(d)}return k.use(v[P]||g),k.use(c),k.use(R(b),i),k.use(function(){return B}),k.use(u),f&&k.use(R(m),l(p,f)),k.use(n),{compile:w,parse:function(){return k.runSync(k.parse(d))},textContent:function(){return d.data.textContent||w(),d.data.textContent}}}({compiler:f,marks:v,parsers:k,postPlugins:w,prePlugins:C,sanitizeSchema:z,vfile:D});return{compile:function(){return F.compile()},file:function(t){return function(t){var r,s=t.extension,f=void 0===s?null:s,l=t.hast,m=void 0===l?null:l,p=t.vfile,d=f;switch(f){case".html":r=e(m);break;case".md":r=u(i(m),{extensions:[a.toMarkdown()]});break;case".txt":r=S(m);break;case".xml":var h=n({type:"element",tagName:"html",children:[m]});r=c(o(h));break;default:r=p.contents,d=p.extname}return{content:r,extension:d,name:""+p.stem+d,stem:p.stem,type:E(d)}}({extension:t,hast:this.parse(),vfile:D})},parse:function(){return F.parse()},search:function(t,r){void 0===r&&(r={});var n=q.minQueryLength,e=q.snippetOffsetPadding,i=void 0===e?100:e;if(t.length<(void 0===n?1:n))return[];var o=this.textContent();return function(t){var r=t.content,n=t.snippetOffsetPadding;return t.searchResults.map(function(t){var e=t.start,i=t.end,o=t.value;return M({},t,{snippet:[r.slice(Math.max(0,e-n),e),o,r.slice(i,Math.min(r.length,i+n))]})})}({content:o,searchResults:L(o,t,r),snippetOffsetPadding:i})},textContent:function(){return F.textContent()}}} //# sourceMappingURL=index.module.js.map