Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add details custom container #455

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion docs/guide/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ This is a warning
::: danger
This is a dangerous warning
:::

::: details
This is a details block, which does not work in Internet Explorer or Edge.
:::
```

**Output**
Expand All @@ -158,22 +162,38 @@ This is a warning
This is a dangerous warning
:::

::: details
This is a details block, which does not work in Internet Explorer or Edge.
:::

### Custom Title

**Input**

```md
````md
::: danger STOP
Danger zone, do not proceed
:::

::: details Click me to view the code
```js
console.log('Hello, VitePress!')
```
:::
````

**Output**

::: danger STOP
Danger zone, do not proceed
:::

::: details Click me to view the code
```js
console.log('Hello, VitePress!')
```
:::

## Syntax Highlighting in Code Blocks

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:
Expand Down
6 changes: 5 additions & 1 deletion src/node/markdown/plugins/containers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const containerPlugin = (md: MarkdownIt) => {
.use(...createContainer('info', 'INFO'))
.use(...createContainer('warning', 'WARNING'))
.use(...createContainer('danger', 'WARNING'))
.use(...createContainer('details', 'Details'))
// explicitly escape Vue syntax
.use(container, 'v-pre', {
render: (tokens: Token[], idx: number) =>
Expand All @@ -31,11 +32,14 @@ function createContainer(klass: string, defaultTitle: string): ContainerArgs {
const token = tokens[idx]
const info = token.info.trim().slice(klass.length).trim()
if (token.nesting === 1) {
if (klass === 'details') {
return `<details class="${klass} custom-block">${info ? `<summary>${info}</summary>` : ''}\n`
}
return `<div class="${klass} custom-block"><p class="custom-block-title">${
info || defaultTitle
}</p>\n`
} else {
return `</div>\n`
return klass === 'details' ? `</details>\n` : `</div>\n`
}
}
}
Expand Down