Skip to content

Commit a8f147f

Browse files
authored
feat: add details custom container (#455)
1 parent 5b04bb9 commit a8f147f

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

docs/guide/markdown.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ This is a warning
138138
::: danger
139139
This is a dangerous warning
140140
:::
141+
142+
::: details
143+
This is a details block, which does not work in Internet Explorer or Edge.
144+
:::
141145
```
142146

143147
**Output**
@@ -158,22 +162,38 @@ This is a warning
158162
This is a dangerous warning
159163
:::
160164

165+
::: details
166+
This is a details block, which does not work in Internet Explorer or Edge.
167+
:::
168+
161169
### Custom Title
162170

163171
**Input**
164172

165-
```md
173+
````md
166174
::: danger STOP
167175
Danger zone, do not proceed
168176
:::
177+
178+
::: details Click me to view the code
179+
```js
180+
console.log('Hello, VitePress!')
169181
```
182+
:::
183+
````
170184

171185
**Output**
172186

173187
::: danger STOP
174188
Danger zone, do not proceed
175189
:::
176190

191+
::: details Click me to view the code
192+
```js
193+
console.log('Hello, VitePress!')
194+
```
195+
:::
196+
177197
## Syntax Highlighting in Code Blocks
178198

179199
VitePress uses [Prism](https://prismjs.com/) to highlight language syntax in Markdown code blocks, using coloured text. Prism supports a wide variety of programming languages. All you need to do is append a valid language alias to the beginning backticks for the code block:

src/node/markdown/plugins/containers.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const containerPlugin = (md: MarkdownIt) => {
77
.use(...createContainer('info', 'INFO'))
88
.use(...createContainer('warning', 'WARNING'))
99
.use(...createContainer('danger', 'WARNING'))
10+
.use(...createContainer('details', 'Details'))
1011
// explicitly escape Vue syntax
1112
.use(container, 'v-pre', {
1213
render: (tokens: Token[], idx: number) =>
@@ -31,11 +32,14 @@ function createContainer(klass: string, defaultTitle: string): ContainerArgs {
3132
const token = tokens[idx]
3233
const info = token.info.trim().slice(klass.length).trim()
3334
if (token.nesting === 1) {
35+
if (klass === 'details') {
36+
return `<details class="${klass} custom-block">${info ? `<summary>${info}</summary>` : ''}\n`
37+
}
3438
return `<div class="${klass} custom-block"><p class="custom-block-title">${
3539
info || defaultTitle
3640
}</p>\n`
3741
} else {
38-
return `</div>\n`
42+
return klass === 'details' ? `</details>\n` : `</div>\n`
3943
}
4044
}
4145
}

0 commit comments

Comments
 (0)