export interface ChapterItem { rawTitle:string title:string /** * the chapter content */ text:string /** * the chapter number */ number:number key:string indexOf:number } export type Chapters = ChapterItem[] function formatLines(text:string){ const lines = text.split('\n') // .reduce((result,line)=>{ // const childLines = line.split('\r\n').map(childLine=>{ // return childLine.split("\r") // }) // return result.concat( // ...childLines // ) // },[]) return lines } export function splitChapter(text:string){ const lines = formatLines(text) const chapters:Chapters = [] /** * the chapters array index, not the chapter number * like pointer */ let chapterIndex = -1 let offset = 0 for(let lineIndex=0;lineIndex#\*])\1{2,}$/.test(text) ) { return "" } if(/SPLIT\s*CHAPTER/.test(text)) return "" if(/CHAPTER\s*SPLIT/.test(text)) return "" return null } function randomString(count:number) { const seeds = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']; return Array(count).fill("").map(()=>{ const index = Math.floor(Math.random() * 1000) % (seeds.length) return seeds[index] }).join("") } const generateKey = () => randomString(20) export async function readFile(file:File){ const reader = new FileReader() const promise = new Promise((resolve,reject)=>{ reader.addEventListener("loadend",function(){ let txt = reader.result as unknown as string txt = txt.replace('\r\n','\n').replace('\r','\n') resolve(reader.result as unknown as string) }) reader.addEventListener("error",reject) }) reader.readAsText(file,"utf-8") return promise }