UNPKG

obsidian-dev-utils

Version:

This is the collection of useful functions that you can use for your Obsidian plugin development

108 lines (107 loc) 3.25 kB
/** * @packageDocumentation * * This module provides utility functions for processing code blocks in Obsidian. */ import type { App, MarkdownPostProcessorContext } from 'obsidian'; import type { ValueProvider } from '../ValueProvider.cjs'; import type { CodeBlockMarkdownInformation } from './CodeBlockMarkdownInformation.cjs'; /** * Options for {@link getCodeBlockMarkdownInfo}. */ export interface GetCodeBlockMarkdownInfoOptions { /** * An Obsidian app instance. */ app: App; /** * A {@link MarkdownPostProcessorContext} object. */ ctx: MarkdownPostProcessorContext; /** * A {@link HTMLElement} representing the code block. */ el: HTMLElement; /** * A source of the code block. */ source: string; } /** * Options for {@link insertAfterCodeBlock} / {@link insertBeforeCodeBlock}. */ export interface InsertCodeBlockOptions extends GetCodeBlockMarkdownInfoOptions { /** * A number of lines to offset the insertion by. Default is `0`. */ lineOffset?: number; /** * Whether to preserve the line prefix of the code block. Default is `false`. */ shouldPreserveLinePrefix?: boolean; /** * A text to insert after the code block. */ text: string; } /** * Options for {@link removeCodeBlock}. */ export interface RemoveCodeBlockOptions extends GetCodeBlockMarkdownInfoOptions { /** * Whether to keep the gap after removing the code block. Default is `false`. */ shouldKeepGap?: boolean; } /** * Options for {@link replaceCodeBlock}. */ export interface ReplaceCodeBlockOptions extends GetCodeBlockMarkdownInfoOptions { /** * An abort signal to control the execution of the function. */ abortSignal?: AbortSignal; /** * Provides a new code block. */ codeBlockProvider: ValueProvider<string, [string]>; /** * Whether to keep the gap when the new code block is empty. Default is `false`. */ shouldKeepGapWhenEmpty?: boolean; /** * Whether to preserve the line prefix of the code block. Default is `false`. */ shouldPreserveLinePrefix?: boolean; } /** * Gets the information about a code block in a Markdown section. * * @param options - The options for the function. * @returns The information about the code block in the Markdown section. */ export declare function getCodeBlockMarkdownInfo(options: GetCodeBlockMarkdownInfoOptions): Promise<CodeBlockMarkdownInformation | null>; /** * Inserts text after the code block. * * @param options - The options for the function. */ export declare function insertAfterCodeBlock(options: InsertCodeBlockOptions): Promise<void>; /** * Inserts text before the code block. * * @param options - The options for the function. */ export declare function insertBeforeCodeBlock(options: InsertCodeBlockOptions): Promise<void>; /** * Removes the code block. * * @param options - The options for the function. */ export declare function removeCodeBlock(options: RemoveCodeBlockOptions): Promise<void>; /** * Replaces the code block. * * @param options - The options for the function. */ export declare function replaceCodeBlock(options: ReplaceCodeBlockOptions): Promise<void>;