From 275096a979fc93a7c2b3cd2297e8cf7e8cfa39e3 Mon Sep 17 00:00:00 2001 From: valentine195 <38669521+valentine195@users.noreply.github.com> Date: Wed, 17 Mar 2021 14:32:19 -0400 Subject: [PATCH] 0.0.4 - Fixed issue where undefined code blocks were sneaking through --- manifest.json | 2 +- package.json | 2 +- src/main.ts | 56 ++++++++++++++++++++++++++++----------------------- versions.json | 2 +- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/manifest.json b/manifest.json index dd5e295..d0b5c33 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-admonition", "name": "Admonition", - "version": "0.0.3", + "version": "0.0.4", "minAppVersion": "0.11.0", "description": "Admonition block-styled content for Obsidian.md", "author": "Jeremy Valentine", diff --git a/package.json b/package.json index 6dd5928..a1694fe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-admonition", - "version": "0.0.3", + "version": "0.0.4", "description": "Admonition block-styled content for Obsidian.md", "main": "main.js", "scripts": { diff --git a/src/main.ts b/src/main.ts index c746e58..9d80fa1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -17,34 +17,40 @@ export default class Admonition extends Plugin { (k) => `language-${k}` ); - codeBlocks = Array.prototype.map.call( - codeBlocks, - (element: HTMLElement): HTMLElement => { - const classList = Array.prototype.filter.call( - element.classList, - (cls: string) => classMap.includes(cls) - ); - if (classList.length) return element; - } - ); + codeBlocks = Array.prototype.map + .call( + codeBlocks, + (element: HTMLElement): HTMLElement => { + if (element) { + const classList = Array.prototype.filter.call( + element.classList, + (cls: string) => classMap.includes(cls) + ); + if (classList.length) return element; + } + } + ) + .filter((b: HTMLElement) => b); if (!codeBlocks.length) return; codeBlocks.forEach((block) => { - let classType = Array.prototype.find.call( - block.classList, - (cls: string) => classMap.includes(cls) - ); - if (!classType) return; - let type = - ADMONITION_MAP[classType.split("language-").pop().trim()]; - if (!type) return; - const { - title = type[0].toUpperCase() + type.slice(1).toLowerCase(), - content = block.innerText - } = Object.fromEntries( - block.innerText.split("\n").map((l) => l.split(": ")) - ); - this.buildAdmonition(block.parentElement, type, title, content); + if (block) { + let classType = Array.prototype.find.call( + block.classList, + (cls: string) => classMap.includes(cls) + ); + if (!classType) return; + let type = + ADMONITION_MAP[classType.split("language-").pop().trim()]; + if (!type) return; + const { + title = type[0].toUpperCase() + type.slice(1).toLowerCase(), + content = block.innerText + } = Object.fromEntries( + block.innerText.split("\n").map((l) => l.split(": ")) + ); + this.buildAdmonition(block.parentElement, type, title, content); + } }); } buildAdmonition( diff --git a/versions.json b/versions.json index e0f6782..c29f198 100644 --- a/versions.json +++ b/versions.json @@ -1,3 +1,3 @@ { - "0.0.3": "0.11.0" + "0.0.4": "0.11.0" }