ExtractTitle returns the title of the template from YAML front-matter
(filePath string)
| 100 | |
| 101 | // ExtractTitle returns the title of the template from YAML front-matter |
| 102 | func ExtractTitle(filePath string) string { |
| 103 | contents, err := os.ReadFile(filePath) |
| 104 | frontmatterBoundaries := detectFrontmatter(contents) |
| 105 | if err == nil && frontmatterBoundaries[0] == 0 { |
| 106 | templateData := struct { |
| 107 | Title string |
| 108 | }{} |
| 109 | if err := yaml.Unmarshal(contents[0:frontmatterBoundaries[1]], &templateData); err == nil && templateData.Title != "" { |
| 110 | return templateData.Title |
| 111 | } |
| 112 | } |
| 113 | return "" |
| 114 | } |
| 115 | |
| 116 | // ExtractContents returns the template contents without the YAML front-matter |
| 117 | func ExtractContents(filePath string) []byte { |