Skip to content

Commit

Permalink
fix: Check for Statblock-style links and transform them before parsin…
Browse files Browse the repository at this point in the history
…g the `statblock-link` property
  • Loading branch information
valentine195 committed May 6, 2024
1 parent a395715 commit 8bc3ae5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 23 additions & 13 deletions src/tracker/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/\[.+?\]\((.+?)\)/);
Expand All @@ -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:<br />${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
);
}

Expand Down

0 comments on commit 8bc3ae5

Please sign in to comment.