@@ -5,104 +5,93 @@ import {
55} from "@fortawesome/fontawesome-svg-core" ;
66import { MarkdownRenderer } from "obsidian" ;
77
8- export async function getAdmonitionElement (
8+ export /* async */ function getAdmonitionElement (
99 type : string ,
1010 title : string ,
1111 iconName : string ,
1212 color : string ,
1313 collapse ?: string
14- ) : Promise < HTMLElement > {
15- return new Promise ( async ( resolve ) => {
16- let admonition ,
17- titleEl ,
18- attrs : { style : string ; open ?: string } = {
19- style : `--admonition-color: ${ color } ;`
20- } ;
21- if ( collapse ) {
22- if ( collapse === "open" ) {
23- attrs . open = "open" ;
24- }
25- admonition = createEl ( "details" , {
26- cls : `admonition admonition-${ type } ` ,
27- attr : attrs
28- } ) ;
29- titleEl = admonition . createEl ( "summary" , {
30- cls : `admonition-title ${
31- ! title . trim ( ) . length ? "no-title" : ""
32- } `
33- } ) ;
34- } else {
35- admonition = createDiv ( {
36- cls : `admonition admonition-${ type } ` ,
37- attr : attrs
38- } ) ;
39- titleEl = admonition . createDiv ( {
40- cls : `admonition-title ${
41- ! title . trim ( ) . length ? "no-title" : ""
42- } `
43- } ) ;
14+ ) : HTMLElement {
15+ let admonition ,
16+ titleEl ,
17+ attrs : { style : string ; open ?: string } = {
18+ style : `--admonition-color: ${ color } ;`
19+ } ;
20+ if ( collapse ) {
21+ if ( collapse === "open" ) {
22+ attrs . open = "open" ;
4423 }
24+ admonition = createEl ( "details" , {
25+ cls : `admonition admonition-${ type } ` ,
26+ attr : attrs
27+ } ) ;
28+ titleEl = admonition . createEl ( "summary" , {
29+ cls : `admonition-title ${ ! title . trim ( ) . length ? "no-title" : "" } `
30+ } ) ;
31+ } else {
32+ admonition = createDiv ( {
33+ cls : `admonition admonition-${ type } ` ,
34+ attr : attrs
35+ } ) ;
36+ titleEl = admonition . createDiv ( {
37+ cls : `admonition-title ${ ! title . trim ( ) . length ? "no-title" : "" } `
38+ } ) ;
39+ }
4540
46- if ( title && title . length ) {
47- /**
48- * Title structure
49- * <div|summary>.admonition-title
50- * <element>.admonition-title-content - Rendered Markdown top-level element (e.g. H1/2/3 etc, p)
51- * div.admonition-title-icon
52- * svg
53- * div.admonition-title-markdown - Container of rendered markdown
54- * ...rendered markdown children...
55- */
41+ if ( title && title . length ) {
42+ /**
43+ * Title structure
44+ * <div|summary>.admonition-title
45+ * <element>.admonition-title-content - Rendered Markdown top-level element (e.g. H1/2/3 etc, p)
46+ * div.admonition-title-icon
47+ * svg
48+ * div.admonition-title-markdown - Container of rendered markdown
49+ * ...rendered markdown children...
50+ */
5651
57- //get markdown
58- const markdownHolder = createDiv ( ) ;
59- await MarkdownRenderer . renderMarkdown (
60- title ,
61- markdownHolder ,
62- "" ,
63- null
64- ) ;
52+ //get markdown
53+ const markdownHolder = createDiv ( ) ;
54+ MarkdownRenderer . renderMarkdown ( title , markdownHolder , "" , null ) ;
6555
66- //admonition-title-content is first child of rendered markdown
67- const admonitionTitleContent = markdownHolder . children [ 0 ] ;
56+ //admonition-title-content is first child of rendered markdown
57+ const admonitionTitleContent = markdownHolder . children [ 0 ] ;
6858
69- //get children of markdown element, then remove them
70- const markdownElements = Array . from (
71- admonitionTitleContent ?. childNodes || [ ]
72- ) ;
73- admonitionTitleContent . innerHTML = "" ;
74- admonitionTitleContent . addClass ( "admonition-title-content" ) ;
59+ //get children of markdown element, then remove them
60+ const markdownElements = Array . from (
61+ admonitionTitleContent ?. childNodes || [ ]
62+ ) ;
63+ admonitionTitleContent . innerHTML = "" ;
64+ admonitionTitleContent . addClass ( "admonition-title-content" ) ;
7565
76- //build icon element
77- const iconEl = admonitionTitleContent . createDiv (
78- "admonition-title-icon"
66+ //build icon element
67+ const iconEl = admonitionTitleContent . createDiv (
68+ "admonition-title-icon"
69+ ) ;
70+ if ( iconName ) {
71+ iconEl . appendChild (
72+ icon (
73+ findIconDefinition ( {
74+ iconName : iconName as IconName ,
75+ prefix : "fas"
76+ } )
77+ ) . node [ 0 ]
7978 ) ;
80- if ( iconName ) {
81- iconEl . appendChild (
82- icon (
83- findIconDefinition ( {
84- iconName : iconName as IconName ,
85- prefix : "fas"
86- } )
87- ) . node [ 0 ]
88- ) ;
89- }
79+ }
9080
91- //add markdown children back
92- const admonitionTitleMarkdown = admonitionTitleContent . createDiv (
93- "admonition-title-markdown"
94- ) ;
95- for ( let i = 0 ; i < markdownElements . length ; i ++ ) {
96- admonitionTitleMarkdown . appendChild ( markdownElements [ i ] ) ;
97- }
98- titleEl . appendChild ( admonitionTitleContent || createDiv ( ) ) ;
81+ //add markdown children back
82+ const admonitionTitleMarkdown = admonitionTitleContent . createDiv (
83+ "admonition-title-markdown"
84+ ) ;
85+ for ( let i = 0 ; i < markdownElements . length ; i ++ ) {
86+ admonitionTitleMarkdown . appendChild ( markdownElements [ i ] ) ;
9987 }
88+ titleEl . appendChild ( admonitionTitleContent || createDiv ( ) ) ;
89+ }
10090
101- //add them to title element
91+ //add them to title element
10292
103- if ( collapse ) {
104- titleEl . createDiv ( "collapser" ) . createDiv ( "handle" ) ;
105- }
106- resolve ( admonition ) ;
107- } ) ;
93+ if ( collapse ) {
94+ titleEl . createDiv ( "collapser" ) . createDiv ( "handle" ) ;
95+ }
96+ return admonition ;
10897}
0 commit comments