Skip to content

Commit 656d462

Browse files
committed
0.0.5
Added ability to collapse
1 parent 8b40205 commit 656d462

File tree

7 files changed

+215
-33
lines changed

7 files changed

+215
-33
lines changed

README.md

Lines changed: 97 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,24 @@ content: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euism
2929

3030
<img src="https://i.imgur.com/pBTJAFa.png">
3131

32+
Leave the title field blank to only display the admonition.
33+
````markdown
34+
```note
35+
title:
36+
content: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla.
37+
```
38+
````
39+
40+
<img src="https://i.imgur.com/pNU2MB7.png">
41+
3242
**Please note that when the title is included, you _must_ specificy the content as well.**
3343

44+
### Collapsible
45+
46+
Use `collapse: open` or `collapse: closed` to create a collapsible admonition.
47+
48+
<img width='640px' src="https://thumbs.gfycat.com/UniqueVillainousHarpseal-size_restricted.gif">
49+
3450
## Admonition Types
3551

3652
The following admonition types are currently supported:
@@ -54,7 +70,11 @@ See [this](https://squidfunk.github.io/mkdocs-material/reference/admonitions/) f
5470

5571
## Customization
5672

57-
The admonitions are each styled with with the following classes, which can be override to custom their appearance:
73+
This is all of the CSS applied to the admonitions. Override these classes to customize the look.
74+
75+
### Base Classes
76+
77+
Every admonition receives the following CSS classes:
5878

5979
```css
6080
.admonition {
@@ -66,7 +86,7 @@ The admonitions are each styled with with the following classes, which can be ov
6686
background-color: var(--background-secondary);
6787
border-left: 0.2rem solid;
6888
border-radius: 0.1rem;
69-
box-shadow: var(--background-modifier-box-shadow);
89+
box-shadow: 0 0.2rem 0.5rem var(--background-modifier-box-shadow);
7090
}
7191
.admonition-title::before {
7292
position: absolute;
@@ -92,7 +112,9 @@ The admonitions are each styled with with the following classes, which can be ov
92112
}
93113
```
94114

95-
Additionally, each admonition type will receive the ```.admonition-<type>``` class:
115+
### Type Specific
116+
117+
Additionally, each admonition type will receive the `.admonition-<type>` class:
96118

97119
```css
98120
/* Example of .admonition-note */
@@ -109,30 +131,89 @@ Additionally, each admonition type will receive the ```.admonition-<type>``` cla
109131
}
110132
```
111133

112-
The admonition icons are svgs defined as variables on the ```:root``` with the name ```--admonition-icon--<type>```. Override this variable to customize the icon. Example:
134+
#### Type Icons
135+
136+
The admonition icons are svgs defined as variables on the `:root` with the name `--admonition-icon--<type>`. Override this variable to customize the icon. Example:
137+
113138
```css
114139
--admonition-icon--quote: url("data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M14 17h3l2-4V7h-6v6h3M6 17h3l2-4V7H5v6h3l-2 4z'/></svg>");
115140
```
116141

142+
### Collapsible
143+
144+
If an icon is collapsible, it will receive the following CSS:
145+
146+
```css
147+
details.admonition:not([open]) {
148+
padding-bottom: 0;
149+
box-shadow: none;
150+
}
151+
details.admonition > summary {
152+
outline: none;
153+
list-style: none;
154+
display: block;
155+
min-height: 1rem;
156+
padding: 0.4rem 1.8rem 0.4rem 2rem;
157+
border-top-left-radius: 0.1rem;
158+
border-top-right-radius: 0.1rem;
159+
cursor: pointer;
160+
}
161+
details.admonition > summary::-webkit-details-marker {
162+
display: none;
163+
}
164+
165+
details.admonition > summary:after {
166+
position: absolute;
167+
top: 8px;
168+
right: 8px;
169+
width: 20px;
170+
height: 20px;
171+
background-color: currentColor;
172+
-webkit-mask-image: var(--admonition-details-icon);
173+
mask-image: var(--admonition-details-icon);
174+
-webkit-mask-repeat: no-repeat;
175+
mask-repeat: no-repeat;
176+
-webkit-mask-size: contain;
177+
mask-size: contain;
178+
transform: rotate(0deg);
179+
transition: transform 0.25s;
180+
content: "";
181+
}
182+
details.admonition[open] > summary:after {
183+
transform: rotate(90deg);
184+
}
185+
```
186+
187+
### No Title
188+
189+
An icon without a title will have this CSS:
190+
191+
```css
192+
.admonition-title.no-title {
193+
display: none;
194+
}
195+
```
196+
117197
## Todo
118-
- Add the ability to collapse the admonition
119-
- Custom admonitions
120-
- Settings tab to customize icon and color of included admonitions
121-
- Longterm - ability to render markdown inside an admonition
198+
199+
- Add the ability to collapse the admonition
200+
- Custom admonitions
201+
- Settings tab to customize icon and color of included admonitions
202+
- Longterm - ability to render markdown inside an admonition
122203

123204
## Installation
124205

125206
### From GitHub
126-
- Download the Latest Release from the Releases section of the GitHub Repository
127-
- Copy the `main.js`, `styles.css` and `manifest.json` files from the release to your vault's plugins folder: `<vault>/.obsidian/plugins/`
128-
Note: On some machines the `.obsidian` folder may be hidden. On MacOS you should be able to press `Command+Shift+Dot` to show the folder in Finder.
129-
- Reload Obsidian
130-
- If prompted about Safe Mode, you can disable safe mode and enable the plugin.
131-
Otherwise head to Settings, third-party plugins, make sure safe mode is off and
132-
enable the plugin from there.
133207

208+
- Download the Latest Release from the Releases section of the GitHub Repository
209+
- Copy the `main.js`, `styles.css` and `manifest.json` files from the release to your vault's plugins folder: `<vault>/.obsidian/plugins/`
210+
Note: On some machines the `.obsidian` folder may be hidden. On MacOS you should be able to press `Command+Shift+Dot` to show the folder in Finder.
211+
- Reload Obsidian
212+
- If prompted about Safe Mode, you can disable safe mode and enable the plugin.
213+
Otherwise head to Settings, third-party plugins, make sure safe mode is off and
214+
enable the plugin from there.
134215

135216
## Warning
136217

137218
This plugin comes with no guarantee of stability and bugs may delete data.
138-
Please ensure you have automated backups.
219+
Please ensure you have automated backups.

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": "0.0.4",
4+
"version": "0.0.5",
55
"minAppVersion": "0.11.0",
66
"description": "Admonition block-styled content for Obsidian.md",
77
"author": "Jeremy Valentine",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-admonition",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Admonition block-styled content for Obsidian.md",
55
"main": "main.js",
66
"scripts": {

src/admonitions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
interface Admonition {
2-
[key: string]: string;
2+
[admonitionType: string]: string;
33
}
44
const admonitions = [
55
"note",

src/main.css

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
--admonition-icon--bug: url("data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M14 12h-4v-2h4m0 6h-4v-2h4m6-6h-2.81a5.985 5.985 0 0 0-1.82-1.96L17 4.41 15.59 3l-2.17 2.17a6.002 6.002 0 0 0-2.83 0L8.41 3 7 4.41l1.62 1.63C7.88 6.55 7.26 7.22 6.81 8H4v2h2.09c-.05.33-.09.66-.09 1v1H4v2h2v1c0 .34.04.67.09 1H4v2h2.81c1.04 1.79 2.97 3 5.19 3s4.15-1.21 5.19-3H20v-2h-2.09c.05-.33.09-.66.09-1v-1h2v-2h-2v-1c0-.34-.04-.67-.09-1H20V8z'/></svg>");
1212
--admonition-icon--example: url("data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M7 13v-2h14v2H7m0 6v-2h14v2H7M7 7V5h14v2H7M3 8V5H2V4h2v4H3m-1 9v-1h3v4H2v-1h2v-.5H3v-1h1V17H2m2.25-7a.75.75 0 0 1 .75.75c0 .2-.08.39-.21.52L3.12 13H5v1H2v-.92L4 11H2v-1h2.25z'/></svg>");
1313
--admonition-icon--quote: url("data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M14 17h3l2-4V7h-6v6h3M6 17h3l2-4V7H5v6h3l-2 4z'/></svg>");
14+
--admonition-details-icon: url("data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M8.59 16.58L13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.42z'/></svg>");
1415
}
1516

1617
/** Constants */
@@ -24,7 +25,7 @@
2425
background-color: var(--background-secondary);
2526
border-left: 0.2rem solid;
2627
border-radius: 0.1rem;
27-
box-shadow: var(--background-modifier-box-shadow);
28+
box-shadow: 0 0.2rem 0.5rem var(--background-modifier-box-shadow);
2829
}
2930
.admonition-title::before {
3031
position: absolute;
@@ -44,10 +45,57 @@
4445
font-weight: 700;
4546
border-left: 0.2rem solid;
4647
}
48+
.admonition-title.no-title {
49+
display: none;
50+
}
51+
4752
.admonition-content {
4853
margin-bottom: 0.6rem;
4954
}
5055

56+
.admonition > .admonition-title.no-title + .admonition-content {
57+
margin: 1em 0;
58+
}
59+
60+
details.admonition:not([open]) {
61+
padding-bottom: 0;
62+
box-shadow: none;
63+
}
64+
details.admonition > summary {
65+
outline: none;
66+
list-style: none;
67+
display: block;
68+
min-height: 1rem;
69+
padding: 0.4rem 1.8rem 0.4rem 2rem;
70+
border-top-left-radius: 0.1rem;
71+
border-top-right-radius: 0.1rem;
72+
cursor: pointer;
73+
}
74+
details.admonition > summary::-webkit-details-marker {
75+
display: none;
76+
}
77+
78+
details.admonition > summary:after {
79+
position: absolute;
80+
top: 8px;
81+
right: 8px;
82+
width: 20px;
83+
height: 20px;
84+
background-color: currentColor;
85+
-webkit-mask-image: var(--admonition-details-icon);
86+
mask-image: var(--admonition-details-icon);
87+
-webkit-mask-repeat: no-repeat;
88+
mask-repeat: no-repeat;
89+
-webkit-mask-size: contain;
90+
mask-size: contain;
91+
transform: rotate(0deg);
92+
transition: transform 0.25s;
93+
content: "";
94+
}
95+
details.admonition[open] > summary:after {
96+
transform: rotate(90deg);
97+
}
98+
5199
/** Type Specific */
52100

53101
.admonition-note {
@@ -111,13 +159,13 @@
111159
}
112160

113161
.admonition-question {
114-
border-color: #00b8d4;
162+
border-color: #64dd17;
115163
}
116164
.admonition-question > .admonition-title {
117165
background-color: rgba(100, 221, 23, 0.1);
118166
}
119167
.admonition-question > .admonition-title::before {
120-
background-color: #00b8d4;
168+
background-color: #64dd17;
121169
-webkit-mask-image: var(--admonition-icon--question);
122170
mask-image: var(--admonition-icon--question);
123171
}

src/main.ts

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,80 @@ export default class Admonition extends Plugin {
4343
let type =
4444
ADMONITION_MAP[classType.split("language-").pop().trim()];
4545
if (!type) return;
46-
const {
46+
let params = Object.fromEntries(
47+
block.innerText
48+
.split("\n")
49+
.map((l) => l.split(":").map((s) => s.trim()))
50+
);
51+
let {
4752
title = type[0].toUpperCase() + type.slice(1).toLowerCase(),
48-
content = block.innerText
49-
} = Object.fromEntries(
50-
block.innerText.split("\n").map((l) => l.split(": "))
53+
content = block.innerText,
54+
collapse
55+
} = params;
56+
console.log(
57+
"🚀 ~ file: main.ts ~ line 56 ~ Admonition ~ codeBlocks.forEach ~ params",
58+
params,
59+
block.innerText
60+
.split("\n")
61+
.map((l) => l.split(":").map((s) => s.trim()))
62+
);
63+
64+
if (
65+
Object.prototype.hasOwnProperty.call(params, "title") &&
66+
params.title === undefined &&
67+
params.collapse
68+
) {
69+
title = "";
70+
}
71+
if (
72+
Object.prototype.hasOwnProperty.call(params, "collapse") &&
73+
(params.collapse.length == 0 ||
74+
params.collapse === undefined)
75+
) {
76+
collapse = "closed";
77+
}
78+
console.log(
79+
"🚀 ~ file: main.ts ~ line 69 ~ Admonition ~ codeBlocks.forEach ~ params.collapse",
80+
collapse
81+
);
82+
this.buildAdmonition(
83+
block.parentElement,
84+
type,
85+
title,
86+
content,
87+
collapse
5188
);
52-
this.buildAdmonition(block.parentElement, type, title, content);
5389
}
5490
});
5591
}
5692
buildAdmonition(
5793
el: HTMLElement,
5894
type: string,
5995
title: string,
60-
content: string
96+
content: string,
97+
collapse?: string
6198
) {
62-
let admonition = createDiv({
63-
cls: `admonition admonition-${type}`
99+
let attrs,
100+
els = [
101+
"div" as keyof HTMLElementTagNameMap,
102+
"div" as keyof HTMLElementTagNameMap
103+
];
104+
if (collapse && ["open", "closed"].includes(collapse)) {
105+
els = [
106+
"details" as keyof HTMLElementTagNameMap,
107+
"summary" as keyof HTMLElementTagNameMap
108+
];
109+
attrs = {
110+
[collapse]: true
111+
};
112+
}
113+
114+
let admonition = createEl(els[0], {
115+
cls: `admonition admonition-${type}`,
116+
attr: attrs
64117
});
65-
admonition.createDiv({
66-
cls: "admonition-title",
118+
admonition.createEl(els[1], {
119+
cls: `admonition-title ${!title.trim().length ? "no-title" : ""}`,
67120
text: title
68121
});
69122
admonition.createEl("p", {

versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"0.0.4": "0.11.0"
2+
"0.0.5": "0.11.0"
33
}

0 commit comments

Comments
 (0)