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(parser): add support for parsing transformations #363

Merged
merged 3 commits into from
Dec 19, 2024
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
7 changes: 4 additions & 3 deletions EXAMPLE_VAULT/.obsidian/plugins/modal-form/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,6 @@
],
"version": "1",
"template": {
"createCommand": true,
"parsedTemplate": [
{
"_tag": "text",
Expand Down Expand Up @@ -514,7 +513,8 @@
},
{
"_tag": "variable",
"value": "dateOfBirth"
"value": "dateOfBirth",
"transformation": "stringify"
},
{
"_tag": "text",
Expand Down Expand Up @@ -576,7 +576,8 @@
"_tag": "text",
"value": "\n\n---\n> Last modified: <% tp.file.last_modified_date(\"dddd, MMMM Do YYYY HH:mm:ss\") %>"
}
]
],
"createCommand": true
}
}
]
Expand Down
105 changes: 101 additions & 4 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,111 @@ Variables are placeholders that get replaced with form field values. The variabl
Hello {{name}}! Your favorite color is {{color}}.
```

#### Basic Variable Syntax

- **Variable Declaration**:

```plaintext
{{ variableName }}
```

Example:

```plaintext
{{ name }}
```

Note that spaces around the variable name are ignored, allowing for flexibility in formatting.

```plaintext
This is a {{variableName}}. And this is also a valid {{ variableName }}
```

#### Variable with Transformations

- **Transformation Syntax**: You can apply transformations to a variable by using a pipe `|` followed by the transformation name.

```plaintext
{{ variableName | transformation }}
```

Example:

```plaintext
{{ name | trim }}
```

### Supported Transformations

The following transformations can be applied to variables:

1. **`upper`**: Converts the variable's value to uppercase.
- Usage:

```plaintext
{{ name | upper }}
```

2. **`lower`**: Converts the variable's value to lowercase.
- Usage:

```plaintext
{{ name | lower }}
```

3. **`trim`**: Removes whitespace from both ends of the variable's value.
- Usage:

```plaintext
{{ name | trim }}
```

4. **`stringify`**: Converts the variable's value to a JSON string.
- Usage:

```plaintext
{{ name | stringify }}
```

### Example Templates

Here are some examples of how to use the new template syntax:

- **Single Variable**:

```plaintext
Hello, {{ name }}!
```

- **Variable with Transformation**:

```plaintext
Hello, {{ name | upper }}!
```

- **Multiple Variables with Transformations**:

```plaintext
Hello, {{ name | trim }}! You are {{ age }} years old.
```

### Notes

- If an invalid transformation is specified, it will be silently ignored, and the variable will be processed without transformation.
- Spaces around the transformation operator (`|`) are ignored, allowing for flexibility in formatting:

```plaintext
{{ name | trim }} or {{ name|trim }} are both valid.
```

### Available Commands

#### Frontmatter Command

The frontmatter command controls which form fields appear in the YAML frontmatter section of your note:

```
{{# frontmatter pick: title, tags #}}
{# frontmatter pick: title, tags #}
```

Options:
Expand All @@ -38,13 +135,13 @@ Options:
You can combine both options:

```
{{# frontmatter pick: title, tags, date omit: draft #}}
{# frontmatter pick: title, tags, date omit: draft #}
```

If no options are specified, all form fields will be included in the frontmatter:

```
{{# frontmatter #}}
{# frontmatter #}
```

## Templater Support
Expand All @@ -67,7 +164,7 @@ Note: This feature is only available if you have the Templater plugin installed
Here's a complete template example that combines variables and frontmatter:

```
{{# frontmatter pick: title, tags #}}
{# frontmatter pick: title, tags #}

# {{title}}

Expand Down
Loading
Loading