Skip to content

Enhancement: Add ability to open snippet in modal for editing #424

Closed
@michaeltlombardi

Description

@michaeltlombardi

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:

  1. The sorts of snippets that this would most benefit from are ones that don't have extremely long blocks of text.
  2. The regex should always be iterating over highlighted text, not the whole file.
  3. 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:

Screenshot of the Front Matter UI for inserting a snippet with the fields populated appropriately

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions