Skip to content

Commit 79030e8

Browse files
author
valentine195
committed
5.0.1
- Fixed issue where collapsible by default was not working
1 parent 805de66 commit 79030e8

File tree

4 files changed

+151
-150
lines changed

4 files changed

+151
-150
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "obsidian-admonition",
33
"name": "Admonition",
4-
"version": "5.0.0",
4+
"version": "5.0.1",
55
"minAppVersion": "0.11.0",
66
"description": "Admonition block-styled content for Obsidian.md",
77
"author": "Jeremy Valentine",

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-admonition",
3-
"version": "5.0.0",
3+
"version": "5.0.1",
44
"description": "Admonition block-styled content for Obsidian.md",
55
"main": "main.js",
66
"scripts": {
@@ -26,9 +26,5 @@
2626
"rollup-plugin-css-only": "^3.1.0",
2727
"tslib": "^2.0.3",
2828
"typescript": "^4.0.3"
29-
},
30-
"dependencies": {
31-
"font-blast": "^0.7.0",
32-
"superagent": "^6.1.0"
3329
}
3430
}

src/util/util.ts

Lines changed: 148 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,179 +1,184 @@
11
import { MarkdownRenderer, Notice } from "obsidian";
2-
import { findIconDefinition, /* icon, */ IconName } from "./icons";
32
import { getIconNode } from "./icons";
43
import { AdmonitionIconDefinition, INestedAdmonition } from "../@types";
54

65
export function getMatches(
7-
src: string,
8-
from: number,
9-
toMatch: string
6+
src: string,
7+
from: number,
8+
toMatch: string
109
): INestedAdmonition {
11-
const split = src.split("\n").slice(from);
12-
const first = split.indexOf(split.find((l) => l == toMatch));
10+
const split = src.split("\n").slice(from);
11+
const first = split.indexOf(split.find((l) => l == toMatch));
1312

14-
let next = first + 1;
15-
for (; next < split.length; next++) {
16-
if (!/^(?: {2,4}|\t)+[\s\S]*?/.test(split[next])) break;
17-
}
13+
let next = first + 1;
14+
for (; next < split.length; next++) {
15+
if (!/^(?: {2,4}|\t)+[\s\S]*?/.test(split[next])) break;
16+
}
1817

19-
let innerSrc = split.slice(first + 1, next).join("\n");
18+
let innerSrc = split.slice(first + 1, next).join("\n");
2019

21-
const toRemove = innerSrc.split("\n")[0].match(/^(\s+)/);
22-
innerSrc = innerSrc.replace(new RegExp(`^${toRemove[0] || ""}`, "gm"), "");
20+
const toRemove = innerSrc.split("\n")[0].match(/^(\s+)/);
21+
innerSrc = innerSrc.replace(new RegExp(`^${toRemove[0] || ""}`, "gm"), "");
2322

24-
return {
25-
start: first + from,
26-
end: next + from - 1,
27-
src: innerSrc,
28-
type: toMatch.split("-").pop()
29-
};
23+
return {
24+
start: first + from,
25+
end: next + from - 1,
26+
src: innerSrc,
27+
type: toMatch.split("-").pop(),
28+
};
3029
}
3130

3231
function startsWithAny(str: string, needles: string[]) {
33-
for (let i = 0; i < needles.length; i++) {
34-
if (str.startsWith(needles[i])) {
35-
return i;
36-
}
37-
}
32+
for (let i = 0; i < needles.length; i++) {
33+
if (str.startsWith(needles[i])) {
34+
return i;
35+
}
36+
}
3837

39-
return false;
38+
return false;
4039
}
4140

4241
export function getParametersFromSource(type: string, src: string) {
43-
const keywordTokens = ["title:", "collapse:"];
42+
const keywordTokens = ["title:", "collapse:"];
4443

45-
const keywords = ["title", "collapse"];
44+
const keywords = ["title", "collapse"];
4645

47-
let lines = src.split("\n");
46+
let lines = src.split("\n");
4847

49-
let skipLines = 0;
48+
let skipLines = 0;
5049

51-
let params: { [k: string]: string } = {};
50+
let params: { [k: string]: string } = {};
5251

53-
for (let i = 0; i < lines.length; i++) {
54-
let keywordIndex = startsWithAny(lines[i], keywordTokens);
52+
for (let i = 0; i < lines.length; i++) {
53+
let keywordIndex = startsWithAny(lines[i], keywordTokens);
5554

56-
if (keywordIndex === false) {
57-
break;
58-
}
55+
if (keywordIndex === false) {
56+
break;
57+
}
5958

60-
let foundKeyword = keywords[keywordIndex];
59+
let foundKeyword = keywords[keywordIndex];
6160

62-
if (params[foundKeyword] !== undefined) {
63-
break;
64-
}
61+
if (params[foundKeyword] !== undefined) {
62+
break;
63+
}
6564

66-
params[foundKeyword] = lines[i]
67-
.substr(keywordTokens[keywordIndex].length)
68-
.trim();
69-
++skipLines;
70-
}
65+
params[foundKeyword] = lines[i]
66+
.substr(keywordTokens[keywordIndex].length)
67+
.trim();
68+
++skipLines;
69+
}
7170

72-
let {
73-
title = type[0].toUpperCase() + type.slice(1).toLowerCase(),
74-
collapse = "none"
75-
} = params;
71+
let {
72+
title = type[0].toUpperCase() + type.slice(1).toLowerCase(),
73+
collapse /* = "none" */,
74+
} = params;
7675

77-
let content = lines.slice(skipLines).join("\n");
76+
let content = lines.slice(skipLines).join("\n");
7877

79-
/**
80-
* If the admonition should collapse, but something other than open or closed was provided, set to closed.
81-
*/
82-
if (collapse !== "none" && collapse !== "open" && collapse !== "closed") {
83-
collapse = "closed";
84-
}
78+
/**
79+
* If the admonition should collapse, but something other than open or closed was provided, set to closed.
80+
*/
81+
if (
82+
collapse &&
83+
collapse.length &&
84+
collapse !== "none" &&
85+
collapse !== "open" &&
86+
collapse !== "closed"
87+
) {
88+
collapse = "closed";
89+
}
8590

86-
/**
87-
* If the admonition should collapse, but title was blanked, set the default title.
88-
*/
89-
if (title.trim() === "" && collapse !== "none") {
90-
title = type[0].toUpperCase() + type.slice(1).toLowerCase();
91-
new Notice("An admonition must have a title if it is collapsible.");
92-
}
91+
/**
92+
* If the admonition should collapse, but title was blanked, set the default title.
93+
*/
94+
if (title.trim() === "" && collapse !== "none") {
95+
title = type[0].toUpperCase() + type.slice(1).toLowerCase();
96+
new Notice("An admonition must have a title if it is collapsible.");
97+
}
9398

94-
return { title, collapse, content };
99+
return { title, collapse, content };
95100
}
96101

97102
export /* async */ function getAdmonitionElement(
98-
type: string,
99-
title: string,
100-
icon: AdmonitionIconDefinition,
101-
color: string,
102-
collapse?: string
103+
type: string,
104+
title: string,
105+
icon: AdmonitionIconDefinition,
106+
color: string,
107+
collapse?: string
103108
): HTMLElement {
104-
let admonition,
105-
titleEl,
106-
attrs: { style: string; open?: string } = {
107-
style: `--admonition-color: ${color};`
108-
};
109-
if (collapse) {
110-
if (collapse === "open") {
111-
attrs.open = "open";
112-
}
113-
admonition = createEl("details", {
114-
cls: `admonition admonition-${type}`,
115-
attr: attrs
116-
});
117-
titleEl = admonition.createEl("summary", {
118-
cls: `admonition-title ${!title.trim().length ? "no-title" : ""}`
119-
});
120-
} else {
121-
admonition = createDiv({
122-
cls: `admonition admonition-${type}`,
123-
attr: attrs
124-
});
125-
titleEl = admonition.createDiv({
126-
cls: `admonition-title ${!title.trim().length ? "no-title" : ""}`
127-
});
128-
}
129-
130-
if (title && title.length) {
131-
/**
132-
* Title structure
133-
* <div|summary>.admonition-title
134-
* <element>.admonition-title-content - Rendered Markdown top-level element (e.g. H1/2/3 etc, p)
135-
* div.admonition-title-icon
136-
* svg
137-
* div.admonition-title-markdown - Container of rendered markdown
138-
* ...rendered markdown children...
139-
*/
140-
141-
//get markdown
142-
const markdownHolder = createDiv();
143-
MarkdownRenderer.renderMarkdown(title, markdownHolder, "", null);
144-
145-
//admonition-title-content is first child of rendered markdown
146-
const admonitionTitleContent = markdownHolder.children[0];
147-
148-
//get children of markdown element, then remove them
149-
const markdownElements = Array.from(
150-
admonitionTitleContent?.childNodes || []
151-
);
152-
admonitionTitleContent.innerHTML = "";
153-
admonitionTitleContent.addClass("admonition-title-content");
154-
155-
//build icon element
156-
const iconEl = admonitionTitleContent.createDiv(
157-
"admonition-title-icon"
158-
);
159-
if (icon && icon.name && icon.type) {
160-
iconEl.appendChild(getIconNode(icon));
161-
}
162-
163-
//add markdown children back
164-
const admonitionTitleMarkdown = admonitionTitleContent.createDiv(
165-
"admonition-title-markdown"
166-
);
167-
for (let i = 0; i < markdownElements.length; i++) {
168-
admonitionTitleMarkdown.appendChild(markdownElements[i]);
169-
}
170-
titleEl.appendChild(admonitionTitleContent || createDiv());
171-
}
172-
173-
//add them to title element
174-
175-
if (collapse) {
176-
titleEl.createDiv("collapser").createDiv("handle");
177-
}
178-
return admonition;
109+
let admonition,
110+
titleEl,
111+
attrs: { style: string; open?: string } = {
112+
style: `--admonition-color: ${color};`,
113+
};
114+
if (collapse) {
115+
if (collapse === "open") {
116+
attrs.open = "open";
117+
}
118+
admonition = createEl("details", {
119+
cls: `admonition admonition-${type}`,
120+
attr: attrs,
121+
});
122+
titleEl = admonition.createEl("summary", {
123+
cls: `admonition-title ${!title.trim().length ? "no-title" : ""}`,
124+
});
125+
} else {
126+
admonition = createDiv({
127+
cls: `admonition admonition-${type}`,
128+
attr: attrs,
129+
});
130+
titleEl = admonition.createDiv({
131+
cls: `admonition-title ${!title.trim().length ? "no-title" : ""}`,
132+
});
133+
}
134+
135+
if (title && title.length) {
136+
/**
137+
* Title structure
138+
* <div|summary>.admonition-title
139+
* <element>.admonition-title-content - Rendered Markdown top-level element (e.g. H1/2/3 etc, p)
140+
* div.admonition-title-icon
141+
* svg
142+
* div.admonition-title-markdown - Container of rendered markdown
143+
* ...rendered markdown children...
144+
*/
145+
146+
//get markdown
147+
const markdownHolder = createDiv();
148+
MarkdownRenderer.renderMarkdown(title, markdownHolder, "", null);
149+
150+
//admonition-title-content is first child of rendered markdown
151+
const admonitionTitleContent = markdownHolder.children[0];
152+
153+
//get children of markdown element, then remove them
154+
const markdownElements = Array.from(
155+
admonitionTitleContent?.childNodes || []
156+
);
157+
admonitionTitleContent.innerHTML = "";
158+
admonitionTitleContent.addClass("admonition-title-content");
159+
160+
//build icon element
161+
const iconEl = admonitionTitleContent.createDiv(
162+
"admonition-title-icon"
163+
);
164+
if (icon && icon.name && icon.type) {
165+
iconEl.appendChild(getIconNode(icon));
166+
}
167+
168+
//add markdown children back
169+
const admonitionTitleMarkdown = admonitionTitleContent.createDiv(
170+
"admonition-title-markdown"
171+
);
172+
for (let i = 0; i < markdownElements.length; i++) {
173+
admonitionTitleMarkdown.appendChild(markdownElements[i]);
174+
}
175+
titleEl.appendChild(admonitionTitleContent || createDiv());
176+
}
177+
178+
//add them to title element
179+
180+
if (collapse) {
181+
titleEl.createDiv("collapser").createDiv("handle");
182+
}
183+
return admonition;
179184
}

versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
"4.2.1": "0.11.0",
1111
"4.3.1": "0.12.0",
1212
"4.4.2": "0.12.2",
13-
"5.0.0": "0.12.2"
13+
"5.0.1": "0.12.2"
1414
}

0 commit comments

Comments
 (0)