This commit is contained in:
greeneclipse committed 2026-10-14 20:28:38 +02:00
1 parent 8b540d22bc
commit c92ee93482
24 files changed
+1495 -327

No files matched your search

@@ -2,6 +2,7 @@ package aiprompts
import (
"encoding/json"
"fmt"
"regexp"
"strings"
)
@@ -63,15 +64,13 @@ const DefaultMetaTitleRuleEN = "Start with the New product name and add the main
func DeriveDescriptionFormulaFromLegacyParts(parts LegacyEnhanceParts) DescriptionFormula {
out := DescriptionFormula{MetaDescription: strings.TrimSpace(parts.MetaRules)}
if out.MetaDescription != "" {
out.MetaTitle = DefaultLegacyMetaTitleRule
// Current default prompts encode both SEO fields explicitly. Preserve
// each role rather than nesting the title rule inside meta_description.
meta := strings.TrimSpace(strings.Trim(out.MetaDescription, "{} "))
if idx := strings.Index(meta, "\nmeta_description:"); idx > 0 && strings.HasPrefix(meta, "meta_title:") {
out.MetaTitle = strings.TrimSpace(strings.TrimPrefix(meta[:idx], "meta_title:"))
out.MetaDescription = strings.TrimSpace(meta[idx+len("\nmeta_description:"):])
if mt, md, ok := splitLocalizedMetaRules(out.MetaDescription); ok {
out.MetaTitle, out.MetaDescription = mt, md
} else {
out.MetaTitle = DefaultLegacyMetaTitleRule
}
}
body := strings.TrimSpace(parts.DescriptionRules)
if body == "" {
return out
@@ -274,8 +273,7 @@ func EffectiveDescriptionFormula(stored any, categoryPrompt string) DescriptionF
// are editor instruction delimiters, not text for SEO output.
func splitLocalizedMetaRules(body string) (title, description string, ok bool) {
body = strings.TrimSpace(body)
if strings.HasPrefix(body, "{") && strings.HasSuffix(body, "}") &&
!strings.HasPrefix(body, "{{") {
if strings.HasPrefix(body, "{") && strings.HasSuffix(body, "}") {
body = strings.TrimSpace(body[1 : len(body)-1])
}
lower := strings.ToLower(body)
@@ -286,11 +284,36 @@ func splitLocalizedMetaRules(body string) (title, description string, ok bool) {
if idx < 0 {
return "", "", false
}
title = strings.TrimSpace(body[len("meta_title:"):idx])
// Both legacy {meta_title: ...\nmeta_description: ...} and the newer
// {meta_title: ...}\n{meta_description: ...} are valid. Store plain
// instructions in description_template, keeping {} only in the editor.
title = strings.TrimSpace(strings.TrimSpace(body[len("meta_title:"):idx]))
title = strings.TrimSpace(title)
// The two-brace form leaves the separator "}\n{" between the roles
// after the outer braces were removed. Strip ONLY those delimiters, never
// user variables such as {{name}} at the end of an instruction.
if strings.HasSuffix(title, "{") {
title = strings.TrimSpace(strings.TrimSuffix(title, "{"))
title = strings.TrimSpace(strings.TrimSuffix(title, "}"))
}
description = strings.TrimSpace(body[idx+len("meta_description:"):])
return title, description, title != "" && description != ""
}
// DefaultSEOMetaInstructions returns the two translated prompt rules for a
// language as plain text (without the editor's instruction braces).
func DefaultSEOMetaInstructions(lang string) (title, description string, err error) {
prompt, err := OfflineDefaultPrompt(lang)
if err != nil {
return "", "", err
}
title, description, ok := splitLocalizedMetaRules(ExtractEnhanceSectionBody(prompt, SectionMetaStart, SectionMetaEnd))
if !ok {
return "", "", fmt.Errorf("invalid meta rules for %q", lang)
}
return title, description, nil
}
func isBuiltInMetaRoleBoilerplate(body string) bool {
lower := strings.ToLower(strings.TrimSpace(body))
return strings.HasPrefix(lower, "role: meta")