Skip to content

Commit c9d00fe

Browse files
committed
feat(#141): move 'fallback' icon to separate option
It didn't sit right with me that there would be a 'special' marker type that has subtly different behavior than any other type of marker. `fallbackIcon` turned into a separate option, and added class `fallback-alert` to the div for full control of styles.
1 parent b008758 commit c9d00fe

3 files changed

Lines changed: 29 additions & 21 deletions

File tree

src/parser/github-alerts.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ import MarkdownIt from 'markdown-it';
88
import config from '../config.js';
99
import 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+
1319
const titles = config.alertsOptions?.titles ?? {};
1420
const matchCaseSensitive = config.alertsOptions?.matchCaseSensitive ?? false;
1521
const 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

3437
for (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+
3944
const 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

static/markdown.css

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,13 @@ blockquote {
142142
align-items: center;
143143
line-height: 1;
144144
}
145-
.markdown-alert .markdown-alert-title .octicon {
145+
.markdown-alert .markdown-alert-title svg {
146146
margin-right: 0.5rem;
147147
display: inline-block;
148148
overflow: visible !important;
149149
vertical-align: text-bottom;
150150
fill: currentColor;
151151
}
152-
/* default style for unconfigured custom markers (Obsidian Callout style) */
153-
.markdown-alert {
154-
border-left: .25rem solid var(--alert-note);
155-
}
156-
.markdown-alert .markdown-alert-title {
157-
color: var(--alert-note);
158-
}
159152
/* default styles for GitHub style markers */
160153
.markdown-alert-note {
161154
border-left: .25rem solid var(--alert-note);
@@ -187,6 +180,13 @@ blockquote {
187180
.markdown-alert-caution .markdown-alert-title {
188181
color: var(--alert-caution);
189182
}
183+
/* default style for unconfigured custom markers (Obsidian Callout style) */
184+
.fallback-alert {
185+
border-left: .25rem solid var(--alert-note);
186+
}
187+
.fallback-alert .markdown-alert-title {
188+
color: var(--alert-note);
189+
}
190190

191191
/* --------------------------------------------------------------------------
192192
* COPY-CODE-BUTTON --------------------------------------------------------- */

tests/rendering/markdown-additional.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ This paragraph has a red background color.{style=background-color:red}
8989
> Markers fall back to `[!NOTE]` by default
9090
>
9191
> Optionally, the fallback icon can be set separately as
92-
> `config.alertsOptions.icons.fallback`
92+
> `config.alertsOptions.fallbackIcon`
9393
9494
> [!fOoBaR]
9595
> The marker is case-insensitive and turns into Title Case

0 commit comments

Comments
 (0)