MCPcopy Create free account
hub / github.com/chhoumann/quickadd / appendToFileWithTemplate

Function appendToFileWithTemplate

src/engine/TemplateEngine.ts:708–747  ·  view source on GitHub ↗
(
		file: TFile,
		resolvedTemplatePath: string,
		section: "top" | "bottom"
	)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 8

parentFolderPathFunction · 0.90
reportErrorFunction · 0.90
templaterParseTemplateFunction · 0.85
isMacroAbortErrorFunction · 0.85
getTemplateContentMethod · 0.45
setTitleMethod · 0.45
setTargetFolderPathMethod · 0.45
formatFileContentMethod · 0.45

Tested by

no test coverage detected