package seo import ( "strings" "unicode" ) // Arial's character widths, in thousandths of an em, for printable ASCII // (32 to 126). Google shows titles in Arial at about 20px and descriptions // at about 14px, so these give a title's width as it shows in results. var arial = [95]int{ 278, 278, 355, 556, 556, 889, 667, 191, 333, 333, 389, 584, 278, 333, 278, 278, // space to / 556, 556, 556, 556, 556, 556, 556, 556, 556, 556, // 0-9 278, 278, 584, 584, 584, 556, 1015, // : to @ 667, 667, 722, 722, 667, 611, 778, 722, 278, 500, 667, 556, 833, 722, 778, // A-O 667, 778, 722, 667, 611, 722, 667, 944, 667, 667, 611, // P-Z 278, 278, 278, 469, 556, 333, // [ to ` 556, 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, 556, 556, // a-o 556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, // p-z 334, 260, 334, 584, // { to ~ } // widthAt is a text's width in pixels at a font size, in Arial. Characters // outside ASCII count as a digit's width; wide scripts count double. func widthAt(s string, px float64) int { var units int for _, r := range s { switch { case r >= 32 && r <= 126: units += arial[r-32] case unicode.Is(unicode.Han, r) || unicode.Is(unicode.Hiragana, r) || unicode.Is(unicode.Katakana, r) || unicode.Is(unicode.Hangul, r): units += 1000 default: units += 556 } } return int(float64(units)*px/1000 + 0.5) } // TitlePixels is about how wide a title shows in Google's results. func TitlePixels(s string) int { return widthAt(s, 20) } // DescriptionPixels is about how wide a meta description shows. func DescriptionPixels(s string) int { return widthAt(s, 14) } // Where Google cuts titles and descriptions off on desktop, about. const ( TitleMaxPixels = 600 DescriptionMaxPixels = 920 ) var stopWords = map[string]bool{ "the": true, "and": true, "for": true, "with": true, "you": true, "your": true, "are": true, "how": true, "what": true, "can": true, "that": true, "this": true, "from": true, "into": true, "les": true, "des": true, "une": true, "der": true, "die": true, "das": true, "und": true, "best": false, } // words are a text's significant words, lower case. func words(s string) []string { var out []string for _, w := range strings.FieldsFunc(strings.ToLower(s), func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsDigit(r) }) { if len([]rune(w)) >= 3 && !stopWords[w] { out = append(out, w) } } return out } // covers is the share of a query's significant words a title contains // (as a word, or the start of one: "host" covers "hosting"). func covers(title, query string) float64 { q := words(query) if len(q) == 0 { return 1 } t := words(title) hit := 0 for _, w := range q { for _, tw := range t { if tw == w || strings.HasPrefix(tw, w) || strings.HasPrefix(w, tw) && len(tw) >= 4 { hit++ break } } } return float64(hit) / float64(len(q)) } // --- robots.txt --- // robots is a robots.txt's rules by user agent (lower case; "*" for all). type robots struct { groups map[string][]rule sitemaps []string found bool } type rule struct { allow bool path string } func parseRobots(txt string) *robots { r := &robots{groups: map[string][]rule{}, found: true} var agents []string inRules := false for _, line := range strings.Split(txt, "\n") { if i := strings.Index(line, "#"); i >= 0 { line = line[:i] } k, v, ok := strings.Cut(line, ":") if !ok { continue } k, v = strings.ToLower(strings.TrimSpace(k)), strings.TrimSpace(v) switch k { case "user-agent": if inRules { agents, inRules = nil, false } agents = append(agents, strings.ToLower(v)) case "allow", "disallow": inRules = true if k == "disallow" && v == "" { // "Disallow:" with nothing allows everything; it still // makes the group exist. for _, a := range agents { r.groups[a] = append(r.groups[a], rule{allow: true, path: "/"}) } continue } for _, a := range agents { r.groups[a] = append(r.groups[a], rule{allow: k == "allow", path: v}) } case "sitemap": r.sitemaps = append(r.sitemaps, v) } } return r } // allowed reports whether an agent may fetch a path: the group for its name // if there is one, else "*"; the longest matching rule wins, Allow on ties. func (r *robots) allowed(agent, path string) bool { if r == nil { return true } rules, ok := r.groups[strings.ToLower(agent)] if !ok { rules = r.groups["*"] } best, allow := -1, true for _, ru := range rules { if ru.path == "" || !matchPath(ru.path, path) { continue } if l := len(ru.path); l > best || (l == best && ru.allow) { best, allow = l, ru.allow } } return allow } // matchPath matches a robots.txt path pattern, with * and a final $. func matchPath(pattern, path string) bool { end := strings.HasSuffix(pattern, "$") pattern = strings.TrimSuffix(pattern, "$") parts := strings.Split(pattern, "*") if !strings.HasPrefix(path, parts[0]) { return false } rest := path[len(parts[0]):] for _, p := range parts[1:] { i := strings.Index(rest, p) if i < 0 { return false } rest = rest[i+len(p):] } return !end || rest == "" || strings.HasSuffix(pattern, "*") } // AICrawlers are the crawlers AI assistants and their training use, with // what each is for. var AICrawlers = []struct{ Agent, For string }{ {"GPTBot", "OpenAI's training crawler"}, {"OAI-SearchBot", "ChatGPT search"}, {"ChatGPT-User", "ChatGPT reading a page for a user"}, {"ClaudeBot", "Anthropic's crawler"}, {"Claude-User", "Claude reading a page for a user"}, {"Claude-SearchBot", "Claude search"}, {"PerplexityBot", "Perplexity"}, {"Google-Extended", "Gemini and Google's AI training"}, {"Applebot-Extended", "Apple Intelligence"}, {"CCBot", "Common Crawl, used by many models"}, }