@@ -8,8 +8,14 @@ import MarkdownIt from 'markdown-it';
88import config from '../config.js' ;
99import octicons from '@primer/octicons' ;
1010
11- // config.json alertsOptions
12- const icons : Record < string , string > = { } ;
11+ const resolveIcon = ( icon : string ) : string => {
12+ // Todo:
13+ // - inline svg
14+ // - svg from filepath
15+ const iconName = icon as keyof typeof octicons ;
16+ return octicons [ iconName ] ?. toSVG ( ) ;
17+ } ;
18+
1319const titles = config . alertsOptions ?. titles ?? { } ;
1420const matchCaseSensitive = config . alertsOptions ?. matchCaseSensitive ?? false ;
1521const classPrefix = config . alertsOptions ?. classPrefix ?? 'markdown-alert' ;
@@ -26,16 +32,15 @@ const mergedIcons = {
2632 ...config . alertsOptions ?. icons ,
2733} ;
2834
29- // Icon for markers that have no configured icon
30- // Defaults to same as [!note]
31- // Can also be customized separately
32- mergedIcons [ 'fallback' ] ??= mergedIcons [ 'note' ] ;
35+ const resolvedIcons : Record < string , string > = { } ;
3336
3437for ( const marker in mergedIcons ) {
35- const octicon = mergedIcons [ marker ] as keyof typeof octicons ;
36- icons [ marker ] = octicons [ octicon ] . toSVG ( ) ;
38+ resolvedIcons [ marker ] = resolveIcon ( mergedIcons [ marker ] ) ;
3739}
3840
41+ const fallbackIconOpt = config . alertsOptions ?. fallbackIcon ?? mergedIcons [ 'note' ] ;
42+ const fallbackIcon = resolveIcon ( fallbackIconOpt ) ;
43+
3944const MarkdownItGitHubAlerts = ( md : MarkdownIt ) => {
4045 const markerNameRE = '\\w+' ;
4146 const RE = new RegExp (
@@ -58,25 +63,28 @@ const MarkdownItGitHubAlerts = (md: MarkdownIt) => {
5863 if ( ! firstContent ) continue ;
5964 const match = firstContent . content . match ( RE ) ;
6065 if ( ! match ) continue ;
61- const type = match [ 1 ] . toLowerCase ( ) as keyof typeof icons ;
66+ const type = match [ 1 ] . toLowerCase ( ) as keyof typeof resolvedIcons ;
6267 const title = match [ 2 ] . trim ( ) || ( titles [ type ] ?? capitalize ( type ) ) ;
63- const icon = icons [ type ] ?? icons [ 'fallback' ] ;
68+ const isFallback = ! ( type in resolvedIcons ) ;
69+ const icon = isFallback ? fallbackIcon : resolvedIcons [ type ] ;
6470 firstContent . content = firstContent . content . slice ( match [ 0 ] . length ) . trimStart ( ) ;
6571 open . type = 'alert_open' ;
6672 open . tag = 'div' ;
6773 open . meta = {
6874 title,
6975 type,
7076 icon,
77+ isFallback,
7178 } ;
7279 close . type = 'alert_close' ;
7380 close . tag = 'div' ;
7481 }
7582 }
7683 } ) ;
7784 md . renderer . rules . alert_open = function ( tokens , idx ) {
78- const { title, type, icon } = tokens [ idx ] . meta ;
79- return `<div class="${ classPrefix } ${ classPrefix } -${ type } "><p class="${ classPrefix } -title">${ icon } ${ title } </p>` ;
85+ const { title, type, icon, isFallback } = tokens [ idx ] . meta ;
86+ const classes = [ classPrefix , `${ classPrefix } -${ type } ` , isFallback ? 'fallback' : '' ] ;
87+ return `<div class="${ classes . join ( ' ' ) } "><p class="${ classPrefix } -title">${ icon } ${ title } </p>` ;
8088 } ;
8189} ;
8290
0 commit comments