//u002f//u002a//nClass representing a twpub plugin//n//u002a//u002f//n//nconst {cleanStylesheet,cleanStyleAttribute} = require(/'//u002f//u002ftransform-stylesheets/'),//n//t{flattenTree} = require(/'//u002f//u002fflatten-tree/'),//n//t{hash} = require(/'//u002f//u002futils/');//n//nconst URL_PREFIX = /'https://u002f//u002fexample.com//u002f/';//n//nclass TwpubPlugin {//n//n//tconstructor (app,options) {//n//t//tthis.app = app;//n//t//tthis.epubReader = options.epubReader;//n//t//tthis.fields = {}; //u002f//u002f Fields of the plugin tiddler itself//n//t//tthis.tiddlers = {}; //u002f//u002f Payload tiddlers//n//t//tthis.errors = []; //u002f//u002f Array of conversion errors//n//t}//n//n//tlogError(message) {//n//t//tthis.errors.push(message);//n//t}//n//n//tconvertEpub() {//n//t//t//u002f//u002f Get the hash of the epub//n//t//tthis.hash = this.epubReader.epubHash.slice(0,16);//n//t//t//u002f//u002f Construct the title of the plugin//n//t//tthis.titlePrefix = /'$//u003a//u002fplugins//u002ftwpub//u002f/' + this.hash;//n//t//t//u002f//u002f For text chunks, make a map of href (including anchor) to title//n//t//tthis.createAnchorToTitleMapping();//n//t//t//u002f//u002f Convert the text, TOC, stylesheets and images into tiddlers//n//t//tthis.convertText();//n//t//tthis.convertToc();//n//t//tthis.convertStylesheets();//n//t//tthis.convertImages();//n//t//t//u002f//u002f Setup the fields of the plugin tiddler//n//t//tthis.fields.list = /'readme errors cover text/';//n//t//tthis.fields.version = /'v0.0.1/';//n//t//tthis.fields[/'plugin-type/'] = /'plugin/';//n//t//tthis.fields.type = /'application//u002fjson/';//n//t//tthis.fields[/'epub-title/'] = this.epubReader.getMetadataItem(/'dc:title/',/'(Untitled)/');//n//t//tthis.fields.name = /'TWPUB/';//n//t//tthis.fields.title = this.titlePrefix;//n//t//tthis.fields.description = this.fields[/'epub-title/'];//n//t//tthis.fields[/'converter-version/'] = this.app.version.toString();//n//t//tthis.fields[/'epub-creator/'] = this.epubReader.getMetadataItem(/'dc:creator/',/'(Unknown)/');//n//t//tthis.fields[/'conversion-errors/'] = this.errors.length.toString();//n//t//tthis.fields[/'count-chunks/'] = this.epubReader.chunks.length.toString();//n//t//tthis.fields[/'count-images/'] = Object.keys(this.epubReader.images).length.toString();//n//t//t//u002f//u002f Cover tab//n//t//tif(this.epubReader.hasMetadataItem(/'cover/')) {//n//t//t//tconst href = this.epubReader.getManifestItem(this.epubReader.getMetadataItem(/'cover/')).href;//n//t//t//tif(href) {//n//t//t//t//tthis.fields[/'cover-image/'] = this.titlePrefix + /'/images//u002f/' + this.epubReader.getManifestItem(this.epubReader.getMetadataItem(/'cover/')).href;//n//t//t//t//tthis.addTiddler({//n//t//t//t//t//ttitle: this.titlePrefix + /'/cover/',//n//t//t//t//t//ttype: /'text//u002fvnd.tiddlywiki/',//n//t//t//t//t//ttext: /'<$transclude tiddler=///'///' + this.titlePrefix + /'///' subtiddler=///'///' + this.fields[/'cover-image/'] + /'///'//u002f>/' //n//t//t//t//t});//n//t//t//t}//n//t//t}//n//t//t//u002f//u002f Readme tab//n//t//tthis.addTiddler({//n//t//t//ttitle: this.titlePrefix + /'/readme/',//n//t//t//ttype: /'text//u002fvnd.tiddlywiki/',//n//t//t//ttext: [/'epub-title/',//n//t//t//t//t//t/'epub-creator/',//n//t//t//t//t//t/'converter-version/',//n//t//t//t//t//t/'conversion-errors/',//n//t//t//t//t//t/'count-chunks/',//n//t//t//t//t//t/'count-images/'//n//t//t//t//t].filter(field => field in this.fields).map(field => |${field} |''${this.fields[field]}'' |).join(/'//n/')//n//t//t});//n//t//t//u002f//u002f Errors tab//n//t//tthis.addTiddler({//n//t//t//ttitle: this.titlePrefix + /'/errors/',//n//t//t//ttype: /'text//u002fvnd.tiddlywiki/',//n//t//t//ttext: this.epubReader.errors.concat(this.errors).map(message => /'# /' + message + /'//n/').join(/'//n/') || /'None/'//n//t//t});//n//t//t//u002f//u002f Full text tab//n//t//tthis.addTiddler({//n//t//t//ttitle: this.titlePrefix + /'/text/',//n//t//t//ttype: /'text//u002fvnd.tiddlywiki/',//n//t//t//ttext: //u005cdefine link-actions()//n<$action-sendmessage $message=///'tm-scroll///' selector={{{ [<navigateTo>escapecss[]addprefix[#]] }}}//u002f>//n//u005cend//n//n<$linkcatcher actions=<<link-actions>>>//n//n<div class=///'tc-table-of-contents///'>//n<<toc /'${this.titlePrefix}//u002ftoc/'>//n</div>//n//n<$list filter=///'[all[tiddlers+shadows]prefix[${this.titlePrefix}//u002ftext//u002f]sort[]]///'>//n<a id=<<currentTiddler>>>//n<$transclude mode=///'inline///'//u002f>//n</a>//n</$list>//n//n</$linkcatcher>//n//t//t});//n//t}//n//n//tcreateAnchorToTitleMapping() {//n//t//tthis.mapAnchorToTitle = Object.create(null);//n//t//tthis.epubReader.chunks.forEach((chunk,index) => {//n//t//t//tconst title = this.makeTextTiddlerTitle(index);//n//t//t//t//u002f//u002f If we've not seen the file before, add a mapping for the file itself, without an anchor ID//n//t//t//tif(!this.mapAnchorToTitle[chunk.href]) {//n//t//t//t//tthis.mapAnchorToTitle[chunk.href] = title;//n//t//t//t}//n//t//t//t//u002f//u002f Add mappings for each anchor ID//n//t//t//tchunk.anchorIds.forEach(anchorId => {//n//t//t//t//tif(!this.mapAnchorToTitle[chunk.href + /'#/' + anchorId]) {//n//t//t//t//t//tthis.mapAnchorToTitle[chunk.href + /'#/' + anchorId] = title; //n//t//t//t//t}//n//t//t//t});//n//t//t});//n//t}//n//n//tmakeTextTiddlerTitle(index) {//n//t//treturn this.titlePrefix + /'/text//u002f/' + (///'///' + index).padStart(9,/'0/');//n//t}//n//n//tconvertText() {//n//t//tthis.epubReader.chunks.forEach((chunk,index) => {//n//t//t//t//u002f//u002f Construct the title for this chunk//n//t//t//tconst title = this.makeTextTiddlerTitle(index);//n//t//t//t//u002f//u002f Collect the scoping classes to be wrapped around this chunk//n//t//t//tconst scopingClasses = chunk.stylesheetIds.map(id => this.makeStylesheetScopeClass(id));//n//t//t//t//u002f//u002f Process some elements and attributes to wikitext//n//t//t//tthis.processTextChunk(chunk);//n//t//t//t//u002f//u002f Flatten the nodes to text//n//t//t//tconst flatText = flattenTree(chunk.nodes);//n//t//t//t//u002f//u002f Add the tiddler//n//t//t//tthis.addTiddler({//n//t//t//t//trole: /'text/',//n//t//t//t//ttitle: title,//n//t//t//t//ttype: /'text//u002fvnd.twpub/',//n//t//t//t//ttext: /'<div class=///'///' + scopingClasses.join(/' /') + /'///'>/' + flatText + /'