74 lines
1.5 KiB
Go
74 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"net/url"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
func (fileContent *FileContent) FormatContent() string {
|
|
content := fileContent.Content
|
|
badChars := make(map[string]string)
|
|
badChars["'"] = "'"
|
|
for k, v := range badChars {
|
|
content = strings.ReplaceAll(content, k, v)
|
|
}
|
|
|
|
return content
|
|
}
|
|
|
|
func (file *TreeFile) CategoriesAsUrl() string {
|
|
escapedCategories := []string{}
|
|
for _, c := range file.Category {
|
|
escapedCategories = append(escapedCategories, url.QueryEscape(c))
|
|
}
|
|
|
|
return strings.Join(escapedCategories, "/")
|
|
}
|
|
|
|
func (file *TreeFile) GmiName() string {
|
|
return strings.Replace(file.Name, ".md", ".gmi", 1)
|
|
}
|
|
|
|
func (file *TreeFile) CategoryPath() string {
|
|
return "/" + strings.Join(file.Category, "/")
|
|
}
|
|
|
|
func (tree *Tree) GetIndexFile() (*TreeFile, error) {
|
|
for _, file := range tree.RootFiles {
|
|
if file.Name == "index.md" {
|
|
return file, nil
|
|
}
|
|
}
|
|
|
|
return nil, errors.New("index file not found")
|
|
}
|
|
|
|
func GetLastFiles(files []*TreeFile) []*TreeFile {
|
|
sortingFiles := make(map[string]*TreeFile)
|
|
for _, f := range files {
|
|
sortingFiles[f.Id] = f
|
|
}
|
|
|
|
keys := make([]string, 0, len(sortingFiles))
|
|
for k := range sortingFiles {
|
|
keys = append(keys, k)
|
|
}
|
|
sort.Sort(sort.Reverse(sort.StringSlice(keys)))
|
|
|
|
length := 10
|
|
if len(keys) < 10 {
|
|
length = len(keys)
|
|
}
|
|
lastFiles := make([]*TreeFile, 0, length)
|
|
for i, k := range keys {
|
|
if i == length {
|
|
break
|
|
}
|
|
|
|
lastFiles = append(lastFiles, sortingFiles[k])
|
|
}
|
|
|
|
return lastFiles
|
|
}
|