Skip to content

Commit

Permalink
3.3.4
Browse files Browse the repository at this point in the history
- Fixed issue where images no longer rendered in admonitions
  • Loading branch information
valentine195 committed Apr 30, 2021
1 parent 2313887 commit 27c4a60
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 92 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-admonition",
"name": "Admonition",
"version": "3.3.3",
"version": "3.3.4",
"minAppVersion": "0.11.0",
"description": "Admonition block-styled content for Obsidian.md",
"author": "Jeremy Valentine",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-admonition",
"version": "3.3.3",
"version": "3.3.4",
"description": "Admonition block-styled content for Obsidian.md",
"main": "main.js",
"scripts": {
Expand Down
10 changes: 5 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,25 +317,25 @@ export default class ObsidianAdmonition
* Collapsible -> <details> <summary> Title </summary> <div> Content </div> </details>
* Regular -> <div> <div> Title </div> <div> Content </div> </div>
*/
let admonitionElement = await getAdmonitionElement(
let admonitionElement = getAdmonitionElement(
type,
title,
this.admonitions[type].icon,
this.admonitions[type].color,
collapse
);

/**
* Create a unloadable component.
*/
let admonitionContent = admonitionElement.createDiv({
cls: "admonition-content"
});
let markdownRenderChild = new MarkdownRenderChild(
admonitionElement
);
markdownRenderChild.containerEl = admonitionElement;

let admonitionContent = admonitionElement.createDiv({
cls: "admonition-content"
});

/**
* Render the content as markdown and append it to the admonition.
*/
Expand Down
157 changes: 73 additions & 84 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,104 +5,93 @@ import {
} from "@fortawesome/fontawesome-svg-core";
import { MarkdownRenderer } from "obsidian";

export async function getAdmonitionElement(
export /* async */ function getAdmonitionElement(
type: string,
title: string,
iconName: string,
color: string,
collapse?: string
): Promise<HTMLElement> {
return new Promise(async (resolve) => {
let admonition,
titleEl,
attrs: { style: string; open?: string } = {
style: `--admonition-color: ${color};`
};
if (collapse) {
if (collapse === "open") {
attrs.open = "open";
}
admonition = createEl("details", {
cls: `admonition admonition-${type}`,
attr: attrs
});
titleEl = admonition.createEl("summary", {
cls: `admonition-title ${
!title.trim().length ? "no-title" : ""
}`
});
} else {
admonition = createDiv({
cls: `admonition admonition-${type}`,
attr: attrs
});
titleEl = admonition.createDiv({
cls: `admonition-title ${
!title.trim().length ? "no-title" : ""
}`
});
): HTMLElement {
let admonition,
titleEl,
attrs: { style: string; open?: string } = {
style: `--admonition-color: ${color};`
};
if (collapse) {
if (collapse === "open") {
attrs.open = "open";
}
admonition = createEl("details", {
cls: `admonition admonition-${type}`,
attr: attrs
});
titleEl = admonition.createEl("summary", {
cls: `admonition-title ${!title.trim().length ? "no-title" : ""}`
});
} else {
admonition = createDiv({
cls: `admonition admonition-${type}`,
attr: attrs
});
titleEl = admonition.createDiv({
cls: `admonition-title ${!title.trim().length ? "no-title" : ""}`
});
}

if (title && title.length) {
/**
* Title structure
* <div|summary>.admonition-title
* <element>.admonition-title-content - Rendered Markdown top-level element (e.g. H1/2/3 etc, p)
* div.admonition-title-icon
* svg
* div.admonition-title-markdown - Container of rendered markdown
* ...rendered markdown children...
*/
if (title && title.length) {
/**
* Title structure
* <div|summary>.admonition-title
* <element>.admonition-title-content - Rendered Markdown top-level element (e.g. H1/2/3 etc, p)
* div.admonition-title-icon
* svg
* div.admonition-title-markdown - Container of rendered markdown
* ...rendered markdown children...
*/

//get markdown
const markdownHolder = createDiv();
await MarkdownRenderer.renderMarkdown(
title,
markdownHolder,
"",
null
);
//get markdown
const markdownHolder = createDiv();
MarkdownRenderer.renderMarkdown(title, markdownHolder, "", null);

//admonition-title-content is first child of rendered markdown
const admonitionTitleContent = markdownHolder.children[0];
//admonition-title-content is first child of rendered markdown
const admonitionTitleContent = markdownHolder.children[0];

//get children of markdown element, then remove them
const markdownElements = Array.from(
admonitionTitleContent?.childNodes || []
);
admonitionTitleContent.innerHTML = "";
admonitionTitleContent.addClass("admonition-title-content");
//get children of markdown element, then remove them
const markdownElements = Array.from(
admonitionTitleContent?.childNodes || []
);
admonitionTitleContent.innerHTML = "";
admonitionTitleContent.addClass("admonition-title-content");

//build icon element
const iconEl = admonitionTitleContent.createDiv(
"admonition-title-icon"
//build icon element
const iconEl = admonitionTitleContent.createDiv(
"admonition-title-icon"
);
if (iconName) {
iconEl.appendChild(
icon(
findIconDefinition({
iconName: iconName as IconName,
prefix: "fas"
})
).node[0]
);
if (iconName) {
iconEl.appendChild(
icon(
findIconDefinition({
iconName: iconName as IconName,
prefix: "fas"
})
).node[0]
);
}
}

//add markdown children back
const admonitionTitleMarkdown = admonitionTitleContent.createDiv(
"admonition-title-markdown"
);
for (let i = 0; i < markdownElements.length; i++) {
admonitionTitleMarkdown.appendChild(markdownElements[i]);
}
titleEl.appendChild(admonitionTitleContent || createDiv());
//add markdown children back
const admonitionTitleMarkdown = admonitionTitleContent.createDiv(
"admonition-title-markdown"
);
for (let i = 0; i < markdownElements.length; i++) {
admonitionTitleMarkdown.appendChild(markdownElements[i]);
}
titleEl.appendChild(admonitionTitleContent || createDiv());
}

//add them to title element
//add them to title element

if (collapse) {
titleEl.createDiv("collapser").createDiv("handle");
}
resolve(admonition);
});
if (collapse) {
titleEl.createDiv("collapser").createDiv("handle");
}
return admonition;
}
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"2.0.1": "0.11.0",
"3.1.2": "0.11.0",
"3.2.2": "0.11.0",
"3.3.3": "0.11.0"
"3.3.4": "0.11.0"
}

0 comments on commit 27c4a60

Please sign in to comment.