optiminimize
This commit is contained in:
@@ -27,6 +27,10 @@ type Post struct {
|
||||
Description string
|
||||
OGImage string
|
||||
OGImageCustom bool
|
||||
CodeLangs []string
|
||||
HasCode bool
|
||||
HasErgon bool
|
||||
HasArchASM bool
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
@@ -71,6 +75,21 @@ func (s *Store) Load(dir string) error {
|
||||
ogImagePath = "/blog/og/" + slug + ".png"
|
||||
}
|
||||
|
||||
codeLangs := extractCodeLangs(raw)
|
||||
var bundleLangs []string
|
||||
hasErgon := false
|
||||
hasArchASM := false
|
||||
for _, l := range codeLangs {
|
||||
switch l {
|
||||
case "ergon":
|
||||
hasErgon = true
|
||||
case "archasm":
|
||||
hasArchASM = true
|
||||
default:
|
||||
bundleLangs = append(bundleLangs, l)
|
||||
}
|
||||
}
|
||||
|
||||
s.index[slug] = len(s.posts)
|
||||
s.posts = append(s.posts, Post{
|
||||
Slug: slug,
|
||||
@@ -80,6 +99,10 @@ func (s *Store) Load(dir string) error {
|
||||
Description: description,
|
||||
OGImage: ogImagePath,
|
||||
OGImageCustom: ogImageCustom,
|
||||
CodeLangs: bundleLangs,
|
||||
HasCode: len(bundleLangs) > 0 || hasErgon || hasArchASM,
|
||||
HasErgon: hasErgon,
|
||||
HasArchASM: hasArchASM,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -200,6 +223,32 @@ func parseFrontMatter(raw []byte, slug, path string) (title string, body []byte,
|
||||
return title, body, date, ogImage, ogImageCustom, description
|
||||
}
|
||||
|
||||
var codeBlockRe = regexp.MustCompile("(?m)^```([a-zA-Z0-9_+-]+)?$")
|
||||
|
||||
func containsLang(langs []string, target string) bool {
|
||||
for _, l := range langs {
|
||||
if l == target {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func extractCodeLangs(raw []byte) []string {
|
||||
seen := make(map[string]bool)
|
||||
var langs []string
|
||||
for _, m := range codeBlockRe.FindAllSubmatch(raw, -1) {
|
||||
if len(m) >= 2 && len(m[1]) > 0 {
|
||||
lang := strings.ToLower(string(m[1]))
|
||||
if !seen[lang] {
|
||||
seen[lang] = true
|
||||
langs = append(langs, lang)
|
||||
}
|
||||
}
|
||||
}
|
||||
return langs
|
||||
}
|
||||
|
||||
func fileModTime(path string) time.Time {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user