@@ -3,182 +3,187 @@ import { getIconNode } from "./icons";
33import { AdmonitionIconDefinition , INestedAdmonition } from "../@types" ;
44
55export function getMatches (
6- src : string ,
7- from : number ,
8- toMatch : string
6+ src : string ,
7+ from : number ,
8+ toMatch : string
99) : INestedAdmonition {
10- const split = src . split ( "\n" ) . slice ( from ) ;
11- 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 ) ) ;
1212
13- let next = first + 1 ;
14- for ( ; next < split . length ; next ++ ) {
15- if ( ! / ^ (?: { 2 , 4 } | \t ) + [ \s \S ] * ?/ . test ( split [ next ] ) ) break ;
16- }
13+ let next = first + 1 ;
14+ for ( ; next < split . length ; next ++ ) {
15+ if ( ! / ^ (?: { 2 , 4 } | \t ) + [ \s \S ] * ?/ . test ( split [ next ] ) ) break ;
16+ }
1717
18- let innerSrc = split . slice ( first + 1 , next ) . join ( "\n" ) ;
18+ let innerSrc = split . slice ( first + 1 , next ) . join ( "\n" ) ;
1919
20- const toRemove = innerSrc . split ( "\n" ) [ 0 ] . match ( / ^ ( \s + ) / ) ;
21- 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" ) , "" ) ;
2222
23- return {
24- start : first + from ,
25- end : next + from - 1 ,
26- src : innerSrc ,
27- type : toMatch . split ( "-" ) . pop ( ) ,
28- } ;
23+ return {
24+ start : first + from ,
25+ end : next + from - 1 ,
26+ src : innerSrc ,
27+ type : toMatch . split ( "-" ) . pop ( )
28+ } ;
2929}
3030
3131function startsWithAny ( str : string , needles : string [ ] ) {
32- for ( let i = 0 ; i < needles . length ; i ++ ) {
33- if ( str . startsWith ( needles [ i ] ) ) {
34- return i ;
35- }
36- }
32+ for ( let i = 0 ; i < needles . length ; i ++ ) {
33+ if ( str . startsWith ( needles [ i ] ) ) {
34+ return i ;
35+ }
36+ }
3737
38- return false ;
38+ return false ;
3939}
4040
4141export function getParametersFromSource ( type : string , src : string ) {
42- const keywordTokens = [ "title:" , "collapse:" ] ;
42+ const keywordTokens = [ "title:" , "collapse:" ] ;
4343
44- const keywords = [ "title" , "collapse" ] ;
44+ const keywords = [ "title" , "collapse" ] ;
4545
46- let lines = src . split ( "\n" ) ;
46+ let lines = src . split ( "\n" ) ;
4747
48- let skipLines = 0 ;
48+ let skipLines = 0 ;
4949
50- let params : { [ k : string ] : string } = { } ;
50+ let params : { [ k : string ] : string } = { } ;
5151
52- for ( let i = 0 ; i < lines . length ; i ++ ) {
53- let keywordIndex = startsWithAny ( lines [ i ] , keywordTokens ) ;
52+ for ( let i = 0 ; i < lines . length ; i ++ ) {
53+ let keywordIndex = startsWithAny ( lines [ i ] , keywordTokens ) ;
5454
55- if ( keywordIndex === false ) {
56- break ;
57- }
55+ if ( keywordIndex === false ) {
56+ break ;
57+ }
5858
59- let foundKeyword = keywords [ keywordIndex ] ;
59+ let foundKeyword = keywords [ keywordIndex ] ;
6060
61- if ( params [ foundKeyword ] !== undefined ) {
62- break ;
63- }
61+ if ( params [ foundKeyword ] !== undefined ) {
62+ break ;
63+ }
6464
65- params [ foundKeyword ] = lines [ i ]
66- . substr ( keywordTokens [ keywordIndex ] . length )
67- . trim ( ) ;
68- ++ skipLines ;
69- }
65+ params [ foundKeyword ] = lines [ i ]
66+ . substr ( keywordTokens [ keywordIndex ] . length )
67+ . trim ( ) ;
68+ ++ skipLines ;
69+ }
7070
71- let {
72- title = type [ 0 ] . toUpperCase ( ) + type . slice ( 1 ) . toLowerCase ( ) ,
73- collapse /* = "none" */ ,
74- } = params ;
71+ let {
72+ title = type [ 0 ] . toUpperCase ( ) + type . slice ( 1 ) . toLowerCase ( ) ,
73+ collapse /* = "none" */
74+ } = params ;
7575
76- let content = lines . slice ( skipLines ) . join ( "\n" ) ;
76+ let content = lines . slice ( skipLines ) . join ( "\n" ) ;
7777
78- /**
79- * If the admonition should collapse, but something other than open or closed was provided, set to closed.
80- */
81- if (
78+ /**
79+ * If the admonition should collapse, but something other than open or closed was provided, set to closed.
80+ */
81+ if (
8282 collapse &&
83- collapse . length &&
84- collapse !== "none" &&
85- collapse !== "open" &&
86- collapse !== "closed"
87- ) {
88- collapse = "closed" ;
89- }
90-
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- }
98-
99- return { title, collapse, content } ;
83+ collapse . length &&
84+ collapse !== "none" &&
85+ collapse !== "open" &&
86+ collapse !== "closed"
87+ ) {
88+ collapse = "closed" ;
89+ }
90+
91+ /**
92+ * If the admonition should collapse, but title was blanked, set the default title.
93+ */
94+ if (
95+ title . trim ( ) === "" &&
96+ collapse &&
97+ collapse . length &&
98+ collapse !== "none"
99+ ) {
100+ title = type [ 0 ] . toUpperCase ( ) + type . slice ( 1 ) . toLowerCase ( ) ;
101+ new Notice ( "An admonition must have a title if it is collapsible." ) ;
102+ }
103+
104+ return { title, collapse, content } ;
100105}
101106
102107export /* async */ function getAdmonitionElement (
103- type : string ,
104- title : string ,
105- icon : AdmonitionIconDefinition ,
106- color : string ,
107- collapse ?: string
108+ type : string ,
109+ title : string ,
110+ icon : AdmonitionIconDefinition ,
111+ color : string ,
112+ collapse ?: string
108113) : HTMLElement {
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 ;
114+ let admonition ,
115+ titleEl ,
116+ attrs : { style : string ; open ?: string } = {
117+ style : `--admonition-color: ${ color } ;`
118+ } ;
119+ if ( collapse ) {
120+ if ( collapse === "open" ) {
121+ attrs . open = "open" ;
122+ }
123+ admonition = createEl ( "details" , {
124+ cls : `admonition admonition-${ type } ` ,
125+ attr : attrs
126+ } ) ;
127+ titleEl = admonition . createEl ( "summary" , {
128+ cls : `admonition-title ${ ! title . trim ( ) . length ? "no-title" : "" } `
129+ } ) ;
130+ } else {
131+ admonition = createDiv ( {
132+ cls : `admonition admonition-${ type } ` ,
133+ attr : attrs
134+ } ) ;
135+ titleEl = admonition . createDiv ( {
136+ cls : `admonition-title ${ ! title . trim ( ) . length ? "no-title" : "" } `
137+ } ) ;
138+ }
139+
140+ if ( title && title . length ) {
141+ /**
142+ * Title structure
143+ * <div|summary>.admonition-title
144+ * <element>.admonition-title-content - Rendered Markdown top-level element (e.g. H1/2/3 etc, p)
145+ * div.admonition-title-icon
146+ * svg
147+ * div.admonition-title-markdown - Container of rendered markdown
148+ * ...rendered markdown children...
149+ */
150+
151+ //get markdown
152+ const markdownHolder = createDiv ( ) ;
153+ MarkdownRenderer . renderMarkdown ( title , markdownHolder , "" , null ) ;
154+
155+ //admonition-title-content is first child of rendered markdown
156+ const admonitionTitleContent = markdownHolder . children [ 0 ] ;
157+
158+ //get children of markdown element, then remove them
159+ const markdownElements = Array . from (
160+ admonitionTitleContent ?. childNodes || [ ]
161+ ) ;
162+ admonitionTitleContent . innerHTML = "" ;
163+ admonitionTitleContent . addClass ( "admonition-title-content" ) ;
164+
165+ //build icon element
166+ const iconEl = admonitionTitleContent . createDiv (
167+ "admonition-title-icon"
168+ ) ;
169+ if ( icon && icon . name && icon . type ) {
170+ iconEl . appendChild ( getIconNode ( icon ) ) ;
171+ }
172+
173+ //add markdown children back
174+ const admonitionTitleMarkdown = admonitionTitleContent . createDiv (
175+ "admonition-title-markdown"
176+ ) ;
177+ for ( let i = 0 ; i < markdownElements . length ; i ++ ) {
178+ admonitionTitleMarkdown . appendChild ( markdownElements [ i ] ) ;
179+ }
180+ titleEl . appendChild ( admonitionTitleContent || createDiv ( ) ) ;
181+ }
182+
183+ //add them to title element
184+
185+ if ( collapse ) {
186+ titleEl . createDiv ( "collapser" ) . createDiv ( "handle" ) ;
187+ }
188+ return admonition ;
184189}
0 commit comments