获取当前文件
const active_file = this.app.workspace.getActiveFile();if (active_file) {folder = active_file.parent;}
获取父文件夹
folder = active_file.parent;
获取文档内容
const editor = active_view.editor;const doc = editor.getDoc();doc.replaceSelection(output_content);
appendToFileWithTemplate
protected async appendToFileWithTemplate(file: TFile, templatePath: string, section: 'top' | 'bottom') {try {const templateContent: string = await this.getTemplateContent(templatePath);const formattedTemplateContent: string = await this.formatter.formatFileContent(templateContent);const fileContent: string = await this.app.vault.cachedRead(file);const newFileContent: string = section === "top" ?`${formattedTemplateContent}\n${fileContent}` :`${fileContent}\n${formattedTemplateContent}`await this.app.vault.modify(file, newFileContent);await replaceTemplaterTemplatesInCreatedFile(this.app, file, true);return file;}catch (e) {log.logError(e);return null;}}
闪念到当期文件
private async captureToActiveFile() {const activeFile: TFile = this.app.workspace.getActiveFile();if (!activeFile) {log.logError("Cannot capture to active file - no active file.")}let content: string = await this.getCaptureContent();content = await this.formatter.formatContent(content, this.choice);if (this.choice.format.enabled) {content = await templaterParseTemplate(this.app, content, activeFile);}if (!content) return;// 读取指定文件缓存if (this.choice.prepend) {const fileContent: string = await this.app.vault.cachedRead(activeFile);const newFileContent: string = `${fileContent}${content}`// 修改文件内容await this.app.vault.modify(activeFile, newFileContent);} else {appendToCurrentLine(content, this.app);}}
添加到当前行
export function appendToCurrentLine(toAppend: string, app: App) {try {const activeView = app.workspace.getActiveViewOfType(MarkdownView);if (!activeView) {log.logError(`unable to append '${toAppend}' to current line.`);return;}activeView.editor.replaceSelection(toAppend);} catch {log.logError(`unable to append '${toAppend}' to current line.`);}}
QuickAdd关键代码
export function appendToCurrentLine(toAppend: string, app: App) {try {const activeView = app.workspace.getActiveViewOfType(MarkdownView);if (!activeView) {log.logError(`unable to append '${toAppend}' to current line.`);return;}activeView.editor.replaceSelection(toAppend);} catch {log.logError(`unable to append '${toAppend}' to current line.`);}}
Templater关键代码
async append_template_to_active_file(template_file: TFile): Promise<void> {// 获取当前view类型const active_view =this.app.workspace.getActiveViewOfType(MarkdownView);if (active_view === null) {// 没有打开的笔记viewlog_error(new TemplaterError("No active view, can't append templates."));return;}const running_config = this.create_running_config(template_file,active_view.file,RunMode.AppendActiveFile);const output_content = await errorWrapper(async () => this.read_and_parse_template(running_config),"Template parsing error, aborting.");// errorWrapper failedif (output_content == null) {return;}const editor = active_view.editor;const doc = editor.getDoc();doc.replaceSelection(output_content);await this.plugin.editor_handler.jump_to_next_cursor_location(active_view.file, true);}
设置光标位置
async jump_to_next_cursor_location(): Promise<void> {const active_view =this.app.workspace.getActiveViewOfType(MarkdownView);if (!active_view) {return;}const active_file = active_view.file;await active_view.save();const content = await this.app.vault.read(active_file);const { new_content, positions } =this.replace_and_get_cursor_positions(content);if (positions) {await this.app.vault.modify(active_file, new_content);this.set_cursor_location(positions);}}
名人名言
思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问
—- 牙叔教程
声明
部分内容来自网络
本教程仅用于学习, 禁止用于其他用途
bilibili
微信公众号 牙叔教程

QQ群
747748653

