获取当前文件

  1. const active_file = this.app.workspace.getActiveFile();
  2. if (active_file) {
  3. folder = active_file.parent;
  4. }

获取父文件夹

  1. folder = active_file.parent;

获取文档内容

  1. const editor = active_view.editor;
  2. const doc = editor.getDoc();
  3. doc.replaceSelection(output_content);

appendToFileWithTemplate

  1. protected async appendToFileWithTemplate(file: TFile, templatePath: string, section: 'top' | 'bottom') {
  2. try {
  3. const templateContent: string = await this.getTemplateContent(templatePath);
  4. const formattedTemplateContent: string = await this.formatter.formatFileContent(templateContent);
  5. const fileContent: string = await this.app.vault.cachedRead(file);
  6. const newFileContent: string = section === "top" ?
  7. `${formattedTemplateContent}\n${fileContent}` :
  8. `${fileContent}\n${formattedTemplateContent}`
  9. await this.app.vault.modify(file, newFileContent);
  10. await replaceTemplaterTemplatesInCreatedFile(this.app, file, true);
  11. return file;
  12. }
  13. catch (e) {
  14. log.logError(e);
  15. return null;
  16. }
  17. }

闪念到当期文件

  1. private async captureToActiveFile() {
  2. const activeFile: TFile = this.app.workspace.getActiveFile();
  3. if (!activeFile) {
  4. log.logError("Cannot capture to active file - no active file.")
  5. }
  6. let content: string = await this.getCaptureContent();
  7. content = await this.formatter.formatContent(content, this.choice);
  8. if (this.choice.format.enabled) {
  9. content = await templaterParseTemplate(this.app, content, activeFile);
  10. }
  11. if (!content) return;
  12. // 读取指定文件缓存
  13. if (this.choice.prepend) {
  14. const fileContent: string = await this.app.vault.cachedRead(activeFile);
  15. const newFileContent: string = `${fileContent}${content}`
  16. // 修改文件内容
  17. await this.app.vault.modify(activeFile, newFileContent);
  18. } else {
  19. appendToCurrentLine(content, this.app);
  20. }
  21. }

添加到当前行

  1. export function appendToCurrentLine(toAppend: string, app: App) {
  2. try {
  3. const activeView = app.workspace.getActiveViewOfType(MarkdownView);
  4. if (!activeView) {
  5. log.logError(`unable to append '${toAppend}' to current line.`);
  6. return;
  7. }
  8. activeView.editor.replaceSelection(toAppend);
  9. } catch {
  10. log.logError(`unable to append '${toAppend}' to current line.`);
  11. }
  12. }

QuickAdd关键代码

  1. export function appendToCurrentLine(toAppend: string, app: App) {
  2. try {
  3. const activeView = app.workspace.getActiveViewOfType(MarkdownView);
  4. if (!activeView) {
  5. log.logError(`unable to append '${toAppend}' to current line.`);
  6. return;
  7. }
  8. activeView.editor.replaceSelection(toAppend);
  9. } catch {
  10. log.logError(`unable to append '${toAppend}' to current line.`);
  11. }
  12. }

Templater关键代码

  1. async append_template_to_active_file(template_file: TFile): Promise<void> {
  2. // 获取当前view类型
  3. const active_view =
  4. this.app.workspace.getActiveViewOfType(MarkdownView);
  5. if (active_view === null) {
  6. // 没有打开的笔记view
  7. log_error(
  8. new TemplaterError("No active view, can't append templates.")
  9. );
  10. return;
  11. }
  12. const running_config = this.create_running_config(
  13. template_file,
  14. active_view.file,
  15. RunMode.AppendActiveFile
  16. );
  17. const output_content = await errorWrapper(
  18. async () => this.read_and_parse_template(running_config),
  19. "Template parsing error, aborting."
  20. );
  21. // errorWrapper failed
  22. if (output_content == null) {
  23. return;
  24. }
  25. const editor = active_view.editor;
  26. const doc = editor.getDoc();
  27. doc.replaceSelection(output_content);
  28. await this.plugin.editor_handler.jump_to_next_cursor_location(active_view.file, true);
  29. }

设置光标位置

  1. async jump_to_next_cursor_location(): Promise<void> {
  2. const active_view =
  3. this.app.workspace.getActiveViewOfType(MarkdownView);
  4. if (!active_view) {
  5. return;
  6. }
  7. const active_file = active_view.file;
  8. await active_view.save();
  9. const content = await this.app.vault.read(active_file);
  10. const { new_content, positions } =
  11. this.replace_and_get_cursor_positions(content);
  12. if (positions) {
  13. await this.app.vault.modify(active_file, new_content);
  14. this.set_cursor_location(positions);
  15. }
  16. }

名人名言

思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问
—- 牙叔教程

声明

部分内容来自网络
本教程仅用于学习, 禁止用于其他用途

bilibili

牙叔教程

微信公众号 牙叔教程

03教程11Templater学习 - 图1

QQ群

747748653
03教程11Templater学习 - 图2