From 66385c0201ae23f6af395609d6b474adf2a7feb4 Mon Sep 17 00:00:00 2001 From: ZigHolding Date: Wed, 9 Jul 2025 21:58:10 +0800 Subject: [PATCH] selection as block --- manifest.json | 2 +- src/commands.ts | 6 +- src/wxmp.ts | 156 ++++++++++++++++++++++++++++++++++-------------- 3 files changed, 114 insertions(+), 50 deletions(-) diff --git a/manifest.json b/manifest.json index d260ce0..a2918a1 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "note-sync", "name": "Note Sync", - "version": "0.6.0", + "version": "0.6.2", "minAppVersion": "1.8.3", "description": "Sync notes or plugins between vaults.", "author": "ZigHolding", diff --git a/src/commands.ts b/src/commands.ts index 907033e..618f869 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -221,15 +221,11 @@ const cmd_export_wxmp = (plugin:NoteSyncPlugin) => ({ hotkeys: [{ modifiers: ['Alt', 'Shift'], key: 'P' }], callback: async () => { if(!plugin.easyapi.cfile){return} - let htmls = []; let ctx = plugin.easyapi.ceditor.getSelection(); if(!ctx){ plugin.wxmp.tfile_to_wxmp(plugin.easyapi.cfile); }else{ - let html = plugin.wxmp.marked.marked(ctx); - let rhtml = await plugin.wxmp.html_to_wxmp(html); - htmls.push(rhtml); - plugin.wxmp.copy_as_html(htmls); + await plugin.wxmp.selection_to_wxmp(); } new Notice(`${plugin.strings.cmd_export_wxmp}: OK`,5000) } diff --git a/src/wxmp.ts b/src/wxmp.ts index 67c89fb..1215684 100644 --- a/src/wxmp.ts +++ b/src/wxmp.ts @@ -30,24 +30,30 @@ export class Wxmp { } if (!config['h1']) { - config['h1'] = this.format_wxmp_h1; + config['h1'] = this.format_wxmp_h1.bind(this); } if (!config['h2']) { - config['h2'] = this.format_wxmp_h2; + config['h2'] = this.format_wxmp_h2.bind(this); } if (!config['h3']) { - config['h3'] = this.format_wxmp_h3; + config['h3'] = this.format_wxmp_h3.bind(this); } if (!config['p code']) { - config['p code'] = this.format_wxmp_p_code; + config['p code'] = this.format_wxmp_p_code.bind(this); } if (!config['li code']) { - config['li code'] = this.format_wxmp_li_code; + config['li code'] = this.format_wxmp_li_code.bind(this); + } + + if (!config['section@html']) { + config['section@html'] = this.format_section_html.bind(this); } if (!config['section@cards-album']) { config['section@cards-album'] = this.format_code_block_cards_album.bind(this); } + + return config; } @@ -121,56 +127,112 @@ export class Wxmp { return new XMLSerializer().serializeToString(doc.body); } + async section_to_wxmp(section:any,sec:string){ + if (section.type == 'yaml') { + return null; + } + + let rhtml:any = null; + + for(let k in this.ctx_map){ + if(k.startsWith('section@')){ + let tpl = this.ctx_map[k]; + if (typeof tpl == 'function') { + rhtml = await tpl(section,sec); + } else { + let rendered = await this.plugin.easyapi.tpl.parse_templater( + tpl, true,{section:section,sec:sec},[0] + ); + if (rendered.length > 0 && rendered[0].trim() != '') { + rhtml = rendered[0]; + } + } + if(rhtml){ + break; + } + } + } + + if(!rhtml){ + if(section.type == 'code'){ + let items = sec.split('\n'); + items[0] = items[0].slice(0,3)+'js'+'\n//'+items[0].slice(3); + sec = items.join('\n'); + } + let html = this.marked.marked(sec); + rhtml = await this.html_to_wxmp(html); + } + return rhtml; + } + + async selection_to_wxmp(){ + let htmls = ['

']; + let sel = this.plugin.easyapi.ceditor.cm.state.selection.main; + if(sel.from==sel.to){ + return null + } + let ctx = await this.plugin.easyapi.ccontent; + if(!ctx){ + return null + } + for(let section of this.plugin.easyapi.cmeta?.sections || []){ + if(sel.to<=section.position.start.offset){ + continue + } + if(sel.from>=section.position.end.offset){ + continue + } + let from = Math.max(sel.from,section.position.start.offset); + let to = Math.min(sel.to,section.position.end.offset); + let sec = ctx.slice(from,to).trim(); + + if(section.type=='code'){ + let items = this.plugin.easyapi.editor.slice_by_position(ctx, section.position).trim().split('\n') + if(!sec.startsWith(items[0])){ + sec = items[0]+'\n'+sec; + } + if(!sec.endsWith(items.last() || '')){ + sec = sec + '\n' + items.last(); + } + } + + if(sec==''){continue} + let rhtml = await this.section_to_wxmp(section,sec); + if(!rhtml){continue} + + if(Array.isArray(rhtml)){ + htmls.push(...rhtml); + }else{ + htmls.push(rhtml); + } + htmls.push('

') + } + this.copy_as_html(htmls); + } + async tfile_to_wxmp(tfile: TFile) { - let htmls = []; + let htmls = ['

']; let ctx = await this.app.vault.read(tfile); for (let section of this.app.metadataCache.getFileCache(tfile)?.sections || []) { if (section.type == 'yaml') { continue } let sec = this.plugin.easyapi.editor.slice_by_position(ctx, section.position); + let rhtml = await this.section_to_wxmp(section,sec); + if(!rhtml){continue} - let rhtml:any = null; - - for(let k in this.ctx_map){ - if(k.startsWith('section@')){ - let tpl = this.ctx_map[k]; - if (typeof tpl == 'function') { - rhtml = await tpl(section,sec); - } else { - let rendered = await this.plugin.easyapi.tpl.parse_templater( - tpl, true,{section:section,sec:sec},[0] - ); - if (rendered.length > 0 && rendered[0].trim() != '') { - rhtml = rendered[0]; - } - } - if(rhtml){ - break; - } - } - } - - if(!rhtml){ - if(section.type == 'code'){ - let items = sec.split('\n'); - items[0] = items[0].slice(0,3)+'js'+'\n//'+items[0].slice(3); - sec = items.join('\n'); - } - let html = this.marked.marked(sec); - rhtml = await this.html_to_wxmp(html); - htmls.push(rhtml); + if(Array.isArray(rhtml)){ + htmls.push(...rhtml); }else{ - if(Array.isArray(rhtml)){ - htmls.push(...rhtml); - }else{ - htmls.push(rhtml); - } + htmls.push(rhtml); } + htmls.push('

'); } - this.plugin.wxmp.copy_as_html(htmls); + this.copy_as_html(htmls); } + + async html_to_wxmp(html: string) { let rhtml; @@ -511,6 +573,14 @@ export class Wxmp { return items; } + async format_section_html(section: any, sec:string) { + if(section.type=='html'){ + return sec; + }else{ + return null; + } + } + generateSideBySideImages(base64Images: string[]) { const sectionStart = `
`; @@ -567,6 +637,4 @@ export class Wxmp { return null; } } - - }