obsidian-latex-suite icon indicating copy to clipboard operation
obsidian-latex-suite copied to clipboard

[FEATURE] New snippet option: "quote-level-aware snippets"

Open RyotaUshio opened this issue 7 months ago • 8 comments

Description of the Problem

The preset snippet dm is very useful, but it doesn't work perfectly when triggered inside a callout or a blockquote, since it doesn't take the quote level (number of ">"s) into consideration. For example:

https://github.com/artisticat1/obsidian-latex-suite/assets/72342591/df837de6-e139-42e2-b5c9-ec8bb396beee

Description of the Solution

In a plugin of mine, I implemented a command like a "callout-compatible dm". Here is the code: https://github.com/RyotaUshio/obsidian-math-booster/blob/master/src/utils/plugin.ts#L80

So it would be great if Latex Suite could execute it via dm, which would be much easier than finding the command from the command palette or pressing a hotkey for it.

I'm not sure this kind of thing is compatible with the snippet system that Latex Suite employs.

Thank you!

RyotaUshio avatar Nov 28 '23 16:11 RyotaUshio

I think the same applies to environment snippets like align as well.

    // Environments
    { trigger: "pmat", replacement: "\\begin{pmatrix}\n$0\n\\end{pmatrix}", options: "mA" },
    { trigger: "bmat", replacement: "\\begin{bmatrix}\n$0\n\\end{bmatrix}", options: "mA" },
    { trigger: "Bmat", replacement: "\\begin{Bmatrix}\n$0\n\\end{Bmatrix}", options: "mA" },
    { trigger: "vmat", replacement: "\\begin{vmatrix}\n$0\n\\end{vmatrix}", options: "mA" },
    { trigger: "Vmat", replacement: "\\begin{Vmatrix}\n$0\n\\end{Vmatrix}", options: "mA" },
    { trigger: "case", replacement: "\\begin{cases}\n$0\n\\end{cases}", options: "mA" },
    { trigger: "align", replacement: "\\begin{align}\n$0\n\\end{align}", options: "mA" },
    { trigger: "array", replacement: "\\begin{array}\n$0\n\\end{array}", options: "mA" },
    { trigger: "matrix", replacement: "\\begin{matrix}\n$0\n\\end{matrix}", options: "mA" },

So it would be great if there were a new snippet option like >: quote-level-aware snippets.

RyotaUshio avatar Nov 29 '23 12:11 RyotaUshio

So let me rephrase my proposal: introducing "quote-level-aware snippets".

A quote-level-aware snippet replaces all \ns in replacement with \n + an appropriate number of >s, depending on the quote level of the current line that the cursor is placed on.

Alternatively, it might make sense to make it the default behavior when replacement contains \ns.

RyotaUshio avatar Nov 29 '23 12:11 RyotaUshio

I agree this would be good to have. (It should also be applied to the matrix shortcut that converts Enter to \\ \n.)

It'd probably require parsing the syntaxTree from CodeMirror to determine whether the cursor lies inside a blockquote, and setting Mode appropriately.

artisticat1 avatar Nov 29 '23 14:11 artisticat1

Seems that using the following snippets can help getting around the most basic usecase at least:

    {trigger: "dm", replacement: "$$\n$0\n$$", options: "tAw"},
    {trigger: "> dm", replacement: "> $$\n> $0\n> $$", options: "tAw"},
  • First usecase: dm expands to
$$ 
<cursor>
$$
  • Second usecase:
> [!Callout]
> dm

expands to

> [!Callout]
> $$
> <cursor>
> $$

And afortunately, this does not break things even after doing > dm on a blank line

  • Third usecase: > dm expands to
> $$
> <cursor>
> $$

Hopefully this is enough of a solution for anybody that also encountered this problem.

JxJxxJxJ avatar Feb 03 '24 05:02 JxJxxJxJ

@JxJxxJxJ I must say you are a legend! This works like a charm.

Just to improve your snippet a little bit, here's my version:

{trigger: /\n> (.*)dm/, replacement: "\n> [[0]]$$\n> $0\n> $$", options: "tAw"},

This way we can

  • avoid this snippet to be triggered when > appears inside a line rather than the beginning of a line
  • and also make it work when there are some other characters between > and dm.

Update: This version also handles quote levels deeper than one.

    {trigger: /\n((> )+)(.*)dm/, replacement: "\n[[0]][[2]]$$\n[[0]]$0\n[[0]]$$", options: "tAw"},

RyotaUshio avatar Feb 03 '24 08:02 RyotaUshio

Very happy it works for you! :) May I ask you how are you making your callouts? I've just noticed that when doing multiple levels I run into some issues... I was using something like

    {trigger: ";prob", replacement: "> [!Problem] \n> $0 ", options: "tA"},
    {trigger: ";ex", replacement: "> [!Example] \n> $0 ", options: "tA"},

for many of my snippets but it does not work very well. I wish there was an easier way to share snippets haha

JxJxxJxJ avatar Feb 03 '24 09:02 JxJxxJxJ

@JxJxxJxJ How about this?

{trigger: /\n((> )*);prob/, replacement: "\n[[0]]> [!Problem]\n[[0]]> $0\n[[0]]> ", options: "tAw"},

I guess it will work even for nested callouts.

RyotaUshio avatar Feb 04 '24 17:02 RyotaUshio

Sorry for the late response, works just fine thank you very much

JxJxxJxJ avatar Feb 17 '24 17:02 JxJxxJxJ