Skip to content

Commit 7191a62

Browse files
authored
feat: support markers: '*' and custom inline title (#1)
1 parent ee2b59e commit 7191a62

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ You might change `github-colors-dark-media.css` to `github-colors-dark-class.css
9595

9696
Refer to the [source code](./styles) for more details.
9797

98+
### Customization
99+
100+
In order to also support [Obsidian callouts syntax](https://help.obsidian.md/Editing+and+formatting/Callouts) it is possible to allow any type of markers with the following setting:
101+
102+
```js
103+
md.use(MarkdownItGitHubAlerts, {
104+
markers: '*'
105+
})
106+
```
107+
Alternative titles are also supported, by appending it to the marker like this:
108+
109+
```markdown
110+
> [!note] Nota bene
111+
> The custom title will replace the regular title.
112+
```
113+
98114
## Sponsors
99115

100116
<p align="center">

src/index.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface MarkdownItGitHubAlertsOptions {
55
* List of markers to match.
66
* @default ['TIP', 'NOTE', 'IMPORTANT', 'WARNING', 'CAUTION']
77
*/
8-
markers?: string[]
8+
markers?: string[] | '*'
99

1010
/**
1111
* If markers case sensitively on matching.
@@ -54,7 +54,8 @@ const MarkdownItGitHubAlerts: MarkdownIt.PluginWithOptions<MarkdownItGitHubAlert
5454
classPrefix = 'markdown-alert',
5555
} = options
5656

57-
const RE = new RegExp(`^\\[\\!(${markers.join('|')})\\]\\s`, matchCaseSensitive ? '' : 'i')
57+
const markerNameRE = markers === '*' ? '\\w+' : markers.join('|')
58+
const RE = new RegExp(`^\\[\\!(${markerNameRE})\\]([^\\n\\r]*)`, matchCaseSensitive ? '' : 'i')
5859

5960
md.core.ruler.after('block', 'github-alerts', (state) => {
6061
const tokens = state.tokens
@@ -72,13 +73,9 @@ const MarkdownItGitHubAlerts: MarkdownIt.PluginWithOptions<MarkdownItGitHubAlert
7273
const match = firstContent.content.match(RE)
7374
if (!match)
7475
continue
75-
7676
const type = match[1].toLowerCase() as keyof typeof icons
77-
const title = titles[type] ?? capitalize(type)
78-
const icon = icons[type]
79-
if (!icon)
80-
throw new Error(`No icon found for marker ${type}`)
81-
77+
const title = match[2].trim() || (titles[type] ?? capitalize(type))
78+
const icon = icons[type] ?? ''
8279
firstContent.content = firstContent.content.slice(match[0].length).trimStart()
8380
open.type = 'alert_open'
8481
open.tag = 'div'

test/fixtures.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ describe('fixtures', () => {
3838
xhtmlOut: true,
3939
})
4040

41-
md.use(MarkdownItGitHubAlerts)
41+
md.use(MarkdownItGitHubAlerts, {
42+
markers: '*',
43+
})
4244

4345
const rendered = [
4446
md.render(content),

test/input/1.basic.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@
1414
1515
> [!CAUTION]
1616
> Negative potential consequences of an action.
17+
18+
# Custom
19+
20+
> [!nOtE] My title
21+
> With `markers: '*'` case of chars is not required and titles are supported.
22+
23+
> [!custom]
24+
> Also any other alert name is allowed.

test/output/1.basic.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ <h1>Hello</h1>
99
</div>
1010
<div class="markdown-alert markdown-alert-caution"><p class="markdown-alert-title"><svg class="octicon octicon-stop mr-2" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M4.47.22A.749.749 0 0 1 5 0h6c.199 0 .389.079.53.22l4.25 4.25c.141.14.22.331.22.53v6a.749.749 0 0 1-.22.53l-4.25 4.25A.749.749 0 0 1 11 16H5a.749.749 0 0 1-.53-.22L.22 11.53A.749.749 0 0 1 0 11V5c0-.199.079-.389.22-.53Zm.84 1.28L1.5 5.31v5.38l3.81 3.81h5.38l3.81-3.81V5.31L10.69 1.5ZM8 4a.75.75 0 0 1 .75.75v3.5a.75.75 0 0 1-1.5 0v-3.5A.75.75 0 0 1 8 4Zm0 8a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path></svg>Caution</p><p>Negative potential consequences of an action.</p>
1111
</div>
12+
<h1>Custom</h1>
13+
<div class="markdown-alert markdown-alert-note"><p class="markdown-alert-title"><svg class="octicon octicon-info mr-2" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path></svg>My title</p><p>With <code>markers: '*'</code> case of chars is not required and titles are supported.</p>
14+
</div>
15+
<div class="markdown-alert markdown-alert-custom"><p class="markdown-alert-title">Custom</p><p>Also any other alert name is allowed.</p>
16+
</div>
1217

1318
<style>
1419
html {

0 commit comments

Comments
 (0)