Repository navigation
Enhancement: Add ability to open snippet in modal for editing #424
Description
Is your feature request related to a problem? Please describe.
For some relatively complex snippets, they're much more pleasant to insert with the snippet feature than update once they exist. This is also useful for snippets with choice fields, especially if they're not single choice or if snippets are enhanced to support other, more complex fields.
Describe the solution you'd like
I would like to be able to highlight a snippet, click the snippet button, and - if the snippet supports editing in place - be able to view the snippet in the modal view again for editing.
I would like to be able to define a snippet similar to this (everything but the new editing key is from a real snippet):
"Linked Art": {
"description": "",
"body": "{{< art src=\"[[&src]]\" alt=\"[[&alt]]\" class=\"[[classes]]\" content_warning=\"[[&content_warning]]\" />}}",
"fields": [
{
"name": "src",
"title": "Source URL for the image",
"type": "string",
"single": true,
"default": ""
},
{
"name": "alt",
"title": "Alt text for the art.",
"type": "string",
"single": true,
"default": ""
},
{
"name": "content_warning",
"title": "Content Warning",
"description": "List any CWs appropriate for the art.",
"type": "string",
"single": true,
"default": ""
},
{
"name": "classes",
"title": "Class List",
"description": "Specify any classes to add to the block in a space separated list.",
"type": "string",
"single": true,
"default": ""
}
],
"editing": {
// if this doesn't match, it's not this snippet
"matchRegex": "{{< art src=.+?\\\/>}}",
// if it did match, check each of these on the matched
// text to find the appropriate value
"parseRegexes": [
"src=\"(?<src>.+?)\"",
"alt=\"(?<alt>.+?)\"",
"class=\"(?<classes>.+?)\"",
"content_warning=\"(?<content_warning>.+?)\""
]
}
}I think there could be some performance considerations here, but:
- The sorts of snippets that this would most benefit from are ones that don't have extremely long blocks of text.
- The regex should always be iterating over highlighted text, not the whole file.
- Each check is likely very small.
Describe alternatives you've considered
I think it should be possible to define a script which analyzes the highlighted text against the defined snippets, break out the values, open the new snippet dialogue, prepopulate the fields with the values already defined, then replace the highlighted text when you insert. This is probably okay but it would be better to have some explicit support for this in the extension or a document describing how to accomplish this so people could self-implement.
Additional context
By way of example, I'd like to go from this markdown (snippet highlighted before either clicking the snippet button in wysiwyg or right clicking for context menu or using the command pallette):
<!-- broken across lines for readability -->
{{<
art src="https://cool.art/arachnid.png"
alt="An illustration of a spitting spider waving its forelegs menacingly."
class="foo bar"
content_warning="Spiders"
/>}}To this UI:

Activity
estruyf commentedon Mar 8, 2023
Started thinking about this one out of the blue, what if we would add a wrapper around the snippet?
If you click on edit, and you are in the block, it will allow you to open the snippet edit dialog, instead of making you choose which snippet to insert.
michaeltlombardi commentedon Mar 9, 2023
Would Front Matter automatically wrap newly-added snippets to the page? Can you walk me through the flow for this for both:
If it's adding the comments around it, I think it's definitely doable - I'm wondering if we could highlight the snippet and run a command from the palette to say "edit snippet" and pick the snippet it relates to.
estruyf commentedon Mar 10, 2023
It goes as follows:
comment wrapper. The wrapper is something the extension will add automatically.I think we can add a hover-card for the snippets to tell the user the snippet can be updated.
michaeltlombardi commentedon Mar 10, 2023
That seems pretty useful! Can I retroactively add those comments myself and have it work?
What goes into the JSON string, if anything?
Using one of my own snippets as an example:
estruyf commentedon Apr 17, 2023
Working on it:
Screen.Recording.2023-04-17.at.18.38.32.mov
estruyf commentedon Apr 18, 2023
frontMatter.snippets.wrapper.enabled: that way we can add more settings later if needed.#424 - Implementation of the snippet wrapper
estruyf commentedon Apr 18, 2023
First initial version is available for testing. The setting to turn on/off the wrapper has to be implemented.
Enhancement: Add ability to open snippet in modal for editing #424
estruyf commentedon Apr 18, 2023
Added a
frontMatter.snippets.wrappersetting to enable/disable the wrapper.#424 - update setting