artisticat1 / obsidian-latex-suite

Make typesetting LaTeX as fast as handwriting through snippets, text expansion, and editor enhancements
MIT License
1.37k stars 55 forks source link

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

Open RyotaUshio opened 11 months ago

RyotaUshio commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

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.

artisticat1 commented 11 months ago

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.

JxJxxJxJ commented 9 months ago

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"},
$$ 
<cursor>
$$

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

RyotaUshio commented 9 months ago

@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

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

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

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

RyotaUshio commented 9 months ago

@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.

JxJxxJxJ commented 8 months ago

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