( file: TFile, resolvedTemplatePath: string, section: "top" | "bottom" )
| 706 | } |
| 707 | |
| 708 | protected async appendToFileWithTemplate( |
| 709 | file: TFile, |
| 710 | resolvedTemplatePath: string, |
| 711 | section: "top" | "bottom" |
| 712 | ) { |
| 713 | try { |
| 714 | const templateContent: string = await this.getTemplateContent( |
| 715 | resolvedTemplatePath |
| 716 | ); |
| 717 | |
| 718 | // Use the existing file's basename as the title |
| 719 | const fileBasename = file.basename; |
| 720 | this.formatter.setTitle(fileBasename); |
| 721 | this.formatter.setTargetFolderPath(parentFolderPath(file.path)); |
| 722 | |
| 723 | let formattedTemplateContent: string = |
| 724 | await this.formatter.formatFileContent(templateContent); |
| 725 | if (file.extension === "md") { |
| 726 | formattedTemplateContent = await templaterParseTemplate( |
| 727 | this.app, |
| 728 | formattedTemplateContent, |
| 729 | file, |
| 730 | ); |
| 731 | } |
| 732 | const fileContent: string = await this.app.vault.cachedRead(file); |
| 733 | const newFileContent: string = |
| 734 | section === "top" |
| 735 | ? `${formattedTemplateContent}\n${fileContent}` |
| 736 | : `${fileContent}\n${formattedTemplateContent}`; |
| 737 | await this.app.vault.modify(file, newFileContent); |
| 738 | |
| 739 | return file; |
| 740 | } catch (err) { |
| 741 | if (isMacroAbortError(err)) { |
| 742 | throw err; |
| 743 | } |
| 744 | reportError(err, "Could not append to file with template"); |
| 745 | return null; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * Reads a template's content. The path MUST already be resolved via |
nothing calls this directly
no test coverage detected