add AI engines
[uweb.git] / zh / searchurl / txt / mdkatex.html
blob5e4aacd0a78bea131895c3c3ca84ca65014bcf02
1 <head>
2 <base>
3 <script src="https://fastly.jsdelivr.net/npm/marked@3.0.7/marked.min.js"></script>
4 <script src="https://cdn.jsdelivr.net/npm/katex@0.13.11/dist/katex.min.js" crossorigin="anonymous"></script>
5 <script>
6 const renderer = new marked.Renderer();
7 var options = {
8 renderer: renderer,
9 pedantic: false,
10 gfm: true,
11 breaks: false,
12 sanitize: false,
13 smartLists: true,
14 smartypants: false,
15 xhtml: false
17 marked.setOptions(options);
19 function htmldecode(text){
20 var temp = document.createElement("div");
21 temp.innerHTML = text;
22 var output = temp.innerText || temp.textContent;
23 temp = null;
24 return output;
27 function mathsExpression(expr) {
28 if (expr.match(/^\$\$[\s\S]*\$\$$/)) {
29 expr = expr.substr(2, expr.length - 4);
30 return katex.renderToString(expr, { displayMode: true });
31 } else if (expr.match(/^\$[\s\S]*\$$/)) {
32 expr = htmldecode(expr); // temp solution
33 expr = expr.substr(1, expr.length - 2);
34 return katex.renderToString(expr, { displayMode: false });
38 const unchanged = new marked.Renderer()
39 renderer.code = function(code, lang, escaped) {
40 if (!lang) {
41 const math = mathsExpression(code);
42 if (math) {
43 return math;
46 return unchanged.code(code, lang, escaped);
49 renderer.codespan = function(text) {
50 const math = mathsExpression(text);
51 if (math) {
52 return math;
54 return unchanged.codespan(text);
57 function markedWithKatex(text) {
58 return marked(text, options);
61 function absFile(url) {
62 this.name=url;
63 this.slice = async(offset, length) =>{
64 const headers = new Headers();
65 headers.append('range', 'bytes=' + offset + '-' + ( offset + length -1).toString());
67 const opts = {
68 credentials: 'include',
69 headers : headers
72 const resp = await fetch( this.name, opts );
73 return await resp.arrayBuffer();
74 //alert(JSON.stringify(resp.headers));
75 //return await resp.text();
79 var file;
80 var blocksize = 20480;
81 function loadSlice(){
82 function render(u8array){
83 document.body.innerHTML = markedWithKatex(new TextDecoder().decode(u8array));
84 scroll(0,0);
86 function setPageUrl(pageurl){
87 let a = document.createElement('a');
88 a.href = "i:5c"+pageurl;
89 a.click();
92 var page = 1;
93 var extrasize = blocksize; //extrasize should <= blocksize
94 let lhash = location.hash;
95 if(lhash){
96 if("p"!==lhash.charAt(1)){//#offset=&[len]
97 let iAmpersand = lhash.indexOf('&',8);
98 let offset = parseInt(lhash.substring(8,iAmpersand));
99 let len = parseInt(lhash.substring(iAmpersand+1));
100 {//update urlInc() url
101 let href = location.href;
102 let iHash = href.indexOf('#');
103 href=href.substring(0,iHash)+"#page="+Math.floor((offset+len)/blocksize);
104 setPageUrl(href);
106 file.slice(offset,len).then((buffer)=>{
107 let u8 = new Uint8Array(buffer);
108 render(u8);
110 return;
112 //#page=
113 page = parseInt(lhash.substring(6));
114 }else
115 setPageUrl(location.href+"#page=1");
116 file.slice(blocksize*(page-1),blocksize+extrasize).then((buffer)=>{
117 let u8 = new Uint8Array(buffer);
118 let u8len = u8.length;
119 let iStart = 0;
120 let limit = Math.min(u8len,extrasize);
121 if(1!=page){
122 for(let i=1;i<limit;i=i+2){
123 if(10==u8[i] && 10==u8[i-1]) {
124 iStart = i+1;
125 break;
129 let iEnd = Math.min(u8len,blocksize);
130 limit = Math.min(u8len,blocksize+extrasize);
131 for(let i=blocksize+1;i<limit;i=i+2){
132 if(10==u8[i] && 10==u8[i-1]) {
133 iEnd = i+1;
134 break;
137 render(u8.slice(iStart,iEnd));
141 //?url=#offset=&[len]&page=
143 let url = location.search.substring(5);
144 file = new absFile(url);
145 document.getElementsByTagName('base')[0].href=url;
147 window.addEventListener('hashchange',loadSlice);
148 </script>
149 </head>
150 <body>
151 <script>loadSlice();</script>
152 </body>