From 8bc3ae56bee16176e209c2e70b734e39f1285154 Mon Sep 17 00:00:00 2001 From: Jeremy Valentine <38669521+valentine195@users.noreply.github.com> Date: Mon, 6 May 2024 19:32:33 -0400 Subject: [PATCH] fix: Check for Statblock-style links and transform them before parsing the `statblock-link` property --- package-lock.json | 8 ++++---- package.json | 2 +- src/tracker/view.ts | 36 +++++++++++++++++++++++------------- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3b89ef8..cac239a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,7 @@ "@codemirror/language": "^6.9.1", "@codemirror/search": "^6.5.3", "@javalent/dice-roller": "^10.5.7", - "@javalent/fantasy-statblocks": "^4.2.2", + "@javalent/fantasy-statblocks": "^4.4.0", "@tsconfig/svelte": "^5.0.2", "@types/node": "^20.6.3", "ajv": "^8.12.0", @@ -936,9 +936,9 @@ "dev": true }, "node_modules/@javalent/fantasy-statblocks": { - "version": "4.2.2", - "resolved": "https://registry.npmjs.org/@javalent/fantasy-statblocks/-/fantasy-statblocks-4.2.2.tgz", - "integrity": "sha512-3dtIrojukkg85tCvrGo6UhqJzALtXWfwSjnfHsoDLiQAfMxZsR76Pw3FBiRilifXjp8hdAxZZOJcGlUXvNMfTQ==", + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/@javalent/fantasy-statblocks/-/fantasy-statblocks-4.4.0.tgz", + "integrity": "sha512-k/oGT9pKnkKCgIlgBXftN8PQOQVSBy0dfq42D3+Q5C9Os7dq8Jt4rhxrpBg/lziCvU4t6RZ9k/dRiVLtv8zOWQ==", "dev": true, "dependencies": { "yaml": "^2.1.3" diff --git a/package.json b/package.json index 479dd68..4a6f43d 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "@codemirror/language": "^6.9.1", "@codemirror/search": "^6.5.3", "@javalent/dice-roller": "^10.5.7", - "@javalent/fantasy-statblocks": "^4.2.2", + "@javalent/fantasy-statblocks": "^4.4.0", "@tsconfig/svelte": "^5.0.2", "@types/node": "^20.6.3", "ajv": "^8.12.0", diff --git a/src/tracker/view.ts b/src/tracker/view.ts index 019f859..30c2b9e 100644 --- a/src/tracker/view.ts +++ b/src/tracker/view.ts @@ -158,6 +158,12 @@ export class CreatureView extends ItemView { } async renderEmbed(embedLink: string) { + if ( + this.plugin.canUseStatBlocks && + window.FantasyStatblocks.isStatblockLink?.(embedLink) + ) { + embedLink = window.FantasyStatblocks.parseStatblockLink(embedLink); + } if (/\[.+\]\(.+\)/.test(embedLink)) { //md [, embedLink] = embedLink.match(/\[.+?\]\((.+?)\)/); @@ -167,28 +173,32 @@ export class CreatureView extends ItemView { } const { path, subpath } = parseLinktext(embedLink); + const file = this.app.metadataCache.getFirstLinkpathDest(path, "/"); - const fileContent = await app.vault.cachedRead(file); let content = `Oops! Something is wrong with your statblock-link:
${embedLink}`; - if (subpath && fileContent) { - const cache = app.metadataCache.getFileCache(file); - const subpathResult = resolveSubpath(cache, subpath); - if (subpathResult) { - content = fileContent.slice( - subpathResult.start.offset, - subpathResult.end.offset - ); + if (file) { + const fileContent = await this.app.vault.cachedRead(file); + if (subpath && fileContent) { + const cache = app.metadataCache.getFileCache(file); + const subpathResult = resolveSubpath(cache, subpath); + if (subpathResult) { + content = fileContent.slice( + subpathResult.start.offset, + subpathResult.end.offset + ); + } + } else if (fileContent) { + content = fileContent; } - } else if (fileContent) { - content = fileContent; } - await MarkdownRenderer.renderMarkdown( + await MarkdownRenderer.render( + this.app, content, this.statblockEl.createDiv("markdown-rendered"), path, - null + this ); }