diff --git a/manifest.json b/manifest.json index d0385e6..5f2892d 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-excalidraw-plugin", "name": "Excalidraw", - "version": "1.2.18", + "version": "1.2.19", "minAppVersion": "0.12.0", "description": "An Obsidian plugin to edit and view Excalidraw drawings", "author": "Zsolt Viczian", diff --git a/src/ExcalidrawData.ts b/src/ExcalidrawData.ts index 0f93638..7c95454 100644 --- a/src/ExcalidrawData.ts +++ b/src/ExcalidrawData.ts @@ -13,6 +13,16 @@ import { } from "./constants"; import { TextMode } from "./ExcalidrawView"; + +declare module "obsidian" { + interface MetadataCache { + blockCache: { + getForFile(x:any,f:TAbstractFile):any; + } + } +} + + const DRAWING_REG = /[\r\n]# Drawing[\r\n](```json[\r\n])?(.*)(```)?(%%)?/gm; //![[link|alias]]![alias](link) @@ -261,17 +271,42 @@ export class ExcalidrawData { private async parse(text:string):Promise{ const getTransclusion = async (text:string) => { //file-name#^blockref - //1 2 - const REG_FILE_BLOCKREF = /(.*)#\^(.*)/g; + //1 2 3 + const REG_FILE_BLOCKREF = /(.*)#(\^)?(.*)/g; const parts=text.matchAll(REG_FILE_BLOCKREF).next(); - if(parts.done || !parts.value[1] || !parts.value[2]) return text; //filename and/or blockref not found + if(parts.done || !parts.value[1] || !parts.value[3]) return text; //filename and/or blockref not found const file = this.app.metadataCache.getFirstLinkpathDest(parts.value[1],this.file.path); const contents = await this.app.vault.cachedRead(file); + const isParagraphRef = parts.value[2] ? true : false; //does the reference contain a ^ character? + const id = parts.value[3]; //the block ID or heading text + const blocks = (await this.app.metadataCache.blockCache.getForFile({isCancelled: ()=>false},file)).blocks.filter((block:any)=>block.node.type!="comment"); + if(!blocks) return text; + if(isParagraphRef) { + const para = blocks.filter((block:any)=>block.node.id == id)[0]?.node; + if(!para) return text; + const startPos = para.position.start.offset; + const endPos = para.children[para.children.length-1]?.position.start.offset-1; //alternative: filter((c:any)=>c.type=="blockid")[0] + return contents.substr(startPos,endPos-startPos) + } else { + const headings = blocks.filter((block:any)=>block.display.startsWith("#")); + let startPos:number = null; + let endPos:number = null; + for(let i=0;i any, ctx?: any): EventRef; } } @@ -259,7 +263,6 @@ export default class ExcalidrawPlugin extends Plugin { this.hover.sourcePath = e.sourcePath; }; this.registerEvent( - //@ts-ignore this.app.workspace.on('hover-link',hoverEvent) ); @@ -436,7 +439,6 @@ export default class ExcalidrawPlugin extends Plugin { id: "excalidraw-download-lib", name: t("DOWNLOAD_LIBRARY"), callback: async () => { - //@ts-ignore if(this.app.isMobile) { const prompt = new Prompt(this.app, "Please provide a filename",'my-library','filename, leave blank to cancel action'); prompt.openAndGetValue( async (filename:string)=> { diff --git a/versions.json b/versions.json index 88dd417..f3b707f 100644 --- a/versions.json +++ b/versions.json @@ -1,3 +1,3 @@ { - "1.2.18": "0.11.13" + "1.2.19": "0.11.13" }