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 default value to variable binding #58

Merged
merged 7 commits into from
Oct 25, 2023
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
4 changes: 2 additions & 2 deletions docs/content/0.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ title: Nuxt Architecture.

### `{{}}` Binding Variables

The `{{ $doc.variable }}` syntax allows you to bind variables in your Markdown content. This is especially useful when you want to dynamically insert values into your document.
The `{{ $doc.variable || 'defaultValue' }}` syntax allows you to bind variables in your Markdown content. This is especially useful when you want to dynamically insert values into your document.

To use this syntax, simply enclose the variable name within double curly braces, like so:

Expand All @@ -238,7 +238,7 @@ To use this syntax, simply enclose the variable name within double curly braces,
color: blue
---

# The color is {{ $doc.color }}.
# The color is {{ $doc.color || 'red' }}.
```

## Contributing
Expand Down
2 changes: 2 additions & 0 deletions playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
const mdcOptions = ref({ experimental: { autoUnwrap: true } })
const markdown = ref(`# Hello World

{{ $doc.name || 'Nuxt' }}

[span]

- [ ] Task 1 [span]
Expand Down
6 changes: 5 additions & 1 deletion src/from-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,15 @@ export default (opts: RemarkMDCOptions = {}) => {

// Bindings
function enterBindingContent (this: CompileContext, token: Token) {
const regex = /([^|]*)(?:\|\|\s*'(.*)')?/
const values = regex.exec(this.sliceSerialize(token))

this.enter({
type: 'textComponent',
name: 'binding',
attributes: {
value: this.sliceSerialize(token).trim()
value: values[1].trim(),
defaultValue: values[2]
}
}, token)
}
Expand Down
Loading