mdview.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <head>
  2. <base>
  3. <script src="https://fastly.jsdelivr.net/npm/marked@3.0.7/marked.min.js"></script>
  4. <script>
  5. function absFile(url) {
  6. this.name=url;
  7. this.slice = async(offset, length) =>{
  8. let resp = new Promise(function (resolve, reject) {
  9. let r=new XMLHttpRequest;
  10. r.onload=function(){
  11. if (r.status == 200)
  12. resolve(r.response);
  13. else
  14. reject(r.statusText);
  15. }
  16. r.onerror = () => reject(r.statusText);
  17. r.open("GET",this.name,!0);
  18. r.setRequestHeader('range', 'bytes=' + offset + '-' + ( offset + length -1).toString());
  19. r.send(null);});
  20. return await resp.arrayBuffer();
  21. }
  22. }
  23. marked.setOptions({
  24. gfm: true,
  25. });
  26. var file;
  27. var blocksize = 20480;
  28. function loadSlice(){
  29. function render(u8array){
  30. document.body.innerHTML = marked(new TextDecoder().decode(u8array));
  31. scroll(0,0);
  32. }
  33. function setPageUrl(pageurl){
  34. let a = document.createElement('a');
  35. a.href = "i:5c"+pageurl;
  36. a.click();
  37. }
  38. var page = 1;
  39. var extrasize = blocksize; //extrasize should <= blocksize
  40. let lhash = location.hash;
  41. if(lhash){
  42. if("p"!==lhash.charAt(1)){//#offset=&[len]
  43. let iAmpersand = lhash.indexOf('&',8);
  44. let offset = parseInt(lhash.substring(8,iAmpersand));
  45. let len = parseInt(lhash.substring(iAmpersand+1));
  46. {//update urlInc() url
  47. let href = location.href;
  48. let iHash = href.indexOf('#');
  49. href=href.substring(0,iHash)+"#page="+Math.floor((offset+len)/blocksize);
  50. setPageUrl(href);
  51. }
  52. file.slice(offset,len).then((buffer)=>{
  53. let u8 = new Uint8Array(buffer);
  54. render(u8);
  55. });
  56. return;
  57. }
  58. //#page=
  59. page = parseInt(lhash.substring(6));
  60. }else
  61. setPageUrl(location.href+"#page=1");
  62. file.slice(blocksize*(page-1),blocksize+extrasize).then((buffer)=>{
  63. let u8 = new Uint8Array(buffer);
  64. let u8len = u8.length;
  65. let iStart = 0;
  66. let limit = Math.min(u8len,extrasize);
  67. if(1!=page){
  68. for(let i=1;i<limit;i=i+2){
  69. if(10==u8[i] && 10==u8[i-1]) {
  70. iStart = i+1;
  71. break;
  72. }
  73. }
  74. }
  75. let iEnd = Math.min(u8len,blocksize);
  76. limit = Math.min(u8len,blocksize+extrasize);
  77. for(let i=blocksize+1;i<limit;i=i+2){
  78. if(10==u8[i] && 10==u8[i-1]) {
  79. iEnd = i+1;
  80. break;
  81. }
  82. }
  83. render(u8.slice(iStart,iEnd));
  84. });
  85. }
  86. //#offset=&[len]&page=
  87. {
  88. let url = location.href;
  89. file = new absFile(url);
  90. }
  91. window.addEventListener('hashchange',loadSlice);
  92. </script>
  93. </head>
  94. <body>
  95. <script>loadSlice();</script>
  96. </body>