Options
All
  • Public
  • Public/Protected
  • All
Menu

obsidian-community-lib

This module contains various utility functions commonly used in Obsidian plugins.

Index

Functions

addAllFeatherIcons

  • addAllFeatherIcons(attr?: { height: string; viewBox: string; width: string }): void
  • Adds all official Feather Icons to Obsidian. https://feathericons.com/

    deprecated

    As of Obsidian 0.13.27 this is no longer needed, because Obsidian ships with lucide, a maintained fork of feather. (https://lucide.dev/)

    Parameters

    • attr: { height: string; viewBox: string; width: string } = ...

      SVG Attributes for the Icon. The default should work for most usecases.

      • height: string
      • viewBox: string
      • width: string

    Returns void

addFeatherIcon

  • addFeatherIcon(name: string, attr?: { height: string; viewBox: string; width: string }): string | void
  • Adds a specific Feather Icon to Obsidian.

    deprecated

    As of Obsidian 0.13.27 this is no longer needed, because Obsidian ships with lucide, a maintained fork of feather. (https://lucide.dev/)

    Parameters

    • name: string

      official Name of the Icon (https://feathericons.com/)

    • attr: { height: string; viewBox: string; width: string } = ...

      SVG Attributes for the Icon. The default should work for most usecases.

      • height: string
      • viewBox: string
      • width: string

    Returns string | void

    Icon name

Const addMD

  • addMD(noteName: string): string
  • Add '.md' to noteName if it isn't already there.

    Parameters

    • noteName: string

      with or without '.md' on the end.

    Returns string

    noteName with '.md' on the end.

addRenderedMarkdownButton

  • addRenderedMarkdownButton<YourPlugin>(plugin: YourPlugin, containerEl: HTMLElement, source: string, fetch: boolean, displayText: string): void
  • Add a button to an HTMLELement, which, when clicked, pops up a RenderedMarkdownModal showing rendered markdown.

    Use fetch to indicate whether the markdown string needs to be fetched, or if it has been provided as a string already.

    Type parameters

    • YourPlugin: Plugin_2<YourPlugin>

    Parameters

    • plugin: YourPlugin
    • containerEl: HTMLElement

      HTMLElement to add the button to

    • source: string

      Raw markdown content or url to find raw markdown.

    • fetch: boolean

      True → fetch markdown from source as url. False → source is already a markdown string.

    • displayText: string

      Text to display in the button.

    Returns void

base64ToArrayBuffer

  • base64ToArrayBuffer(base64: string): ArrayBuffer
  • Convert a base64 String to an ArrayBuffer. You can then use the ArrayBuffer to save the asset to disk.

    deprecated

    Obsidian offers it's own method as of 0.14.5

    Parameters

    • base64: string

      base64 string to be converted.

    Returns ArrayBuffer

    ArrayBuffer

copy

  • copy(content: string, success?: () => any, failure?: (reason?: any) => any): Promise<void>
  • Copy content to the users clipboard.

    Parameters

    • content: string

      The content to be copied to clipboard.

    • success: () => any = ...

      The callback to run when text is successfully copied. Default throws a new Notice

        • (): any
        • Returns any

    • failure: (reason?: any) => any = ...

      The callback to run when text was not able to be copied. Default throws a new Notice, and console logs the error.`

        • (reason?: any): any
        • Parameters

          • Optional reason: any

          Returns any

    Returns Promise<void>

createNewMDNote

  • createNewMDNote(newName: string, currFilePath?: string): Promise<TFile>
  • Create a new markdown note named newName in the user's preffered new-note-folder.

    Parameters

    • newName: string

      Name of new note (with or without '.md')

    • currFilePath: string = ""

    Returns Promise<TFile>

    new TFile

getActiveFileContent

  • getActiveFileContent(cached?: boolean): Promise<string | null>
  • Parameters

    • cached: boolean = true

      Return cached file content or return what's on disk.

    Returns Promise<string | null>

getAvailablePathForAttachments

  • getAvailablePathForAttachments(fileName: string, format: string, sourceFile: TFile): string
  • This is a helper method for an undocumented API of Obsidian.

    Parameters

    • fileName: string

      The Filename for your Attachment

    • format: string

      The Fileformat of your Attachment

    • sourceFile: TFile

      The Sourcefile from where the Attachment gets added, this is needed because the Attachment Folder might be different based on where it gets inserted.

    Returns string

    The Attachment Path

getSelectionFromCurrFile

  • getSelectionFromCurrFile(cached?: boolean): Promise<string | null>
  • Check if something is selected in the current file and return that selection, otherwise return the entire content of the current file.

    Parameters

    • cached: boolean = true

    Returns Promise<string | null>

    null if not focussed on a markdown file

getSelectionFromEditor

  • getSelectionFromEditor(editor: Editor): string
  • Given an editor, check if something is selected and return that selection, otherwise return the entire content of the editor

    Parameters

    • editor: Editor

    Returns string

hoverPreview

  • hoverPreview<YourView>(event: MouseEvent, view: YourView, to: string): void
  • When hovering a link going to to, show the Obsidian hover-preview of that note.

    You probably have to hold down Ctrl when hovering the link for the preview to appear!

    Type parameters

    • YourView: ItemView<YourView>

      The ViewType of your view

    Parameters

    • event: MouseEvent
    • view: YourView

      The view with the link being hovered

    • to: string

      The basename of the note to preview.

    Returns void

    void

Const isInVault

  • isInVault(noteName: string, sourcePath?: string): boolean
  • Check if noteName is the name of a note that exists in the vault.

    Parameters

    • noteName: string

      Basename of the note to search for.

    • sourcePath: string = ""

    Returns boolean

    boolean

isLinked

  • isLinked(resolvedLinks: ResolvedLinks, from: string, to: string, directed?: boolean): boolean
  • Given a list of resolved links from app.metadataCache, check if from has a link to to

    Parameters

    • resolvedLinks: ResolvedLinks
    • from: string

      Note name with link leaving (With or without '.md')

    • to: string

      Note name with link arriving (With or without '.md')

    • directed: boolean = true

    Returns boolean

isResolved

  • isResolved(to: string, from: string): boolean
  • Check if the link fromto is resolved or not.

    Parameters

    • to: string
    • from: string

    Returns boolean

    boolean

openOrSwitch

  • openOrSwitch(dest: string, event: MouseEvent, options?: { createNewFile: boolean }): Promise<void>
  • When clicking a link, check if that note is already open in another leaf, and switch to that leaf, if so. Otherwise, open the note in a new pane.

    Parameters

    • dest: string

      Name of note to open. If you want to open a non-md note, be sure to add the file extension.

    • event: MouseEvent
    • options: { createNewFile: boolean } = ...
      • createNewFile: boolean

    Returns Promise<void>

    Promise

openView

  • openView<YourView>(viewType: string, viewClass: Constructor<YourView>, side?: "left" | "right"): Promise<YourView>
  • Open your view on the chosen side if it isn't already open

    Type parameters

    • YourView: ItemView<YourView>

    Parameters

    • viewType: string
    • viewClass: Constructor<YourView>

      The class constructor of your view

    • side: "left" | "right" = "right"

    Returns Promise<YourView>

    The opened view

resolvedLinksComplete

  • resolvedLinksComplete(noFiles: number): boolean
  • Check if app.metadataCache.ResolvedLinks have fully initalised.

    Parameters

    • noFiles: number

      Number of files in your vault.

    Returns boolean

saveViewSide

  • saveViewSide<YourPlugin>(plugin: YourPlugin, viewType: string, settingName: string): Promise<"left" | "right">
  • Check which side of the workspace your viewType is on, and save it into plugin.settings[settingName].

    Tip: Run this function on plugin.unload to save the last side your view was on when closing, then openView on the same side it was last.

    Type parameters

    • YourPlugin: Plugin_2<YourPlugin>

    Parameters

    • plugin: YourPlugin
    • viewType: string
    • settingName: string

    Returns Promise<"left" | "right">

    side

splitAtYaml

  • splitAtYaml(content: string): [string, string]
  • Check if the content of a note has YAML. If so, return an array of the YAML and the rest of the note. If not, return ['', content]

    Parameters

    • content: string

    Returns [string, string]

Const stripMD

  • stripMD(noteName: string): string
  • Strip '.md' off the end of a note name to get its basename.

    Works with the edgecase where a note has '.md' in its basename: Obsidian.md.md, for example.

    Parameters

    • noteName: string

      with or without '.md' on the end.

    Returns string

    noteName without '.md'

wait

  • wait(delay: number): Promise<void>
  • You can await this Function to delay execution

    Parameters

    • delay: number

      The delay in ms

    Returns Promise<void>

waitForResolvedLinks

  • waitForResolvedLinks(delay?: number, max?: number): Promise<void>
  • Wait for app.metadataCache.ResolvedLinks to have fully initialised.

    Parameters

    • delay: number = 1000
    • max: number = 50

    Returns Promise<void>

Generated using TypeDoc