obsidian-auto-link-title icon indicating copy to clipboard operation
obsidian-auto-link-title copied to clipboard

Disable auto-link title when pasting into Frontmatter

Open darlal opened this issue 4 years ago • 2 comments
trafficstars

Currently the auto-linking happens whenever a link is pasted anywhere in the note. It would be great if a setting could be added to disable auto-linking when pasting into the Frontmatter section of the note.

Thanks for your work on this plugin!

darlal avatar Sep 12 '21 18:09 darlal

I came here to note this as well. Alternative, the plugin could have a "normal paste" command for when one just wants to paste a URL.

chrisgrieser avatar Dec 25 '21 13:12 chrisgrieser

I haven't tried any of this. But figured it'd post it here. Maybe this can help get the feature implemented quicker:

GPT4's advice:

To avoid getting the plugin triggered when pasting inside a frontmatter section, you can add a new function isInFrontMatter to check if the current cursor position is inside a frontmatter section. This function should return true if the cursor is inside a frontmatter section and false otherwise.

The Obsidian editor doesn't provide a built-in function to check if a position is inside a frontmatter section. However, frontmatter sections are usually defined as being between two lines consisting of three hyphens (---). So you can add this function to your EditorExtensions class to check if a position is inside a frontmatter section:

class EditorExtensions {
  static isInFrontMatter(editor: Editor): boolean {
    const cursorLine = editor.getCursor().line;
    let lineContent;
    let frontMatterStart = -1;
    let frontMatterEnd = -1;

    for (let i = 0; i <= cursorLine; i++) {
      lineContent = editor.getLine(i);
      if (lineContent === '---') {
        if (frontMatterStart === -1) {
          frontMatterStart = i;
        } else if (i > frontMatterStart) {
          frontMatterEnd = i;
          break;
        }
      }
    }

    return frontMatterStart !== -1 && cursorLine > frontMatterStart && cursorLine < frontMatterEnd;
  }

  // Other methods...
}

Then in your pasteUrlWithTitle function and manualPasteUrlWithTitle function, you can check if the cursor is inside a frontmatter section before doing anything else:

async pasteUrlWithTitle(clipboard: ClipboardEvent, editor: Editor): Promise<void> {
  if (EditorExtensions.isInFrontMatter(editor)) {
    return;
  }

  // Rest of your code...
}

async manualPasteUrlWithTitle(editor: Editor): Promise<void> {
  if (EditorExtensions.isInFrontMatter(editor)) {
    editor.replaceSelection(await navigator.clipboard.readText());
    return;
  }

  // Rest of your code...
}

In the above code, if the cursor is inside a frontmatter section, the function simply pastes the text from the clipboard without any further processing and then returns immediately, preventing the rest of the code in the function from executing.

thibaultmol avatar May 22 '23 10:05 thibaultmol