package server import ( "io" "net" "net/http" "net/url" "path" "regexp" "strings" "unicode" "golang.org/x/net/html" "pageswiki/internal/store" ) const socialImagePath = "/static/social-card-v1.png" type socialMetadata struct { Title, Description, URL, SiteName, Locale string ShareText string Image, ImageType, ImageAlt, Card string ImageWidth, ImageHeight int NoIndex bool } var socialMacroPattern = regexp.MustCompile(`\{\{[^{}]*\}\}`) func socialText(raw string, limit int) string { raw = strings.Map(func(r rune) rune { if unicode.IsControl(r) && !unicode.IsSpace(r) { return -1 } return r }, raw) runes := []rune(strings.Join(strings.Fields(socialMacroPattern.ReplaceAllString(raw, " ")), " ")) if len(runes) > limit { return string(runes[:limit-1]) + "…" } return string(runes) } // Parse only a bounded prefix of canonical HTML, never an expanded macro, // embedded document, script, external image or user-specific projection. func socialHTML(raw string) (string, string) { z := html.NewTokenizer(io.LimitReader(strings.NewReader(raw), 64<<10)) z.SetMaxBuf(64 << 10) var heading, text strings.Builder skip, inHeading, headingDone := "", false, false for { kind := z.Next() if kind == html.ErrorToken { break } token := z.Token() if skip != "" { if kind == html.EndTagToken && token.Data == skip { skip = "" } continue } if kind == html.StartTagToken { switch token.Data { case "script", "style", "template", "iframe", "noscript", "svg": skip = token.Data continue } if token.Data == "h1" && !headingDone { inHeading = true } } if kind == html.EndTagToken && token.Data == "h1" { inHeading = false headingDone = true } if kind == html.TextToken { text.WriteString(token.Data) if inHeading { heading.WriteString(token.Data) } } else { text.WriteByte(' ') } } return socialText(heading.String(), 90), socialText(text.String(), 200) } // Configured Domain/Campus identity is authoritative. Never let forwarded Host, // Origin, query strings or credentials contaminate URLs sent to third parties. func (s *Server) socialOrigin(r *http.Request) string { host, err := s.store.GetDomainEffectiveHost(s.domainUUIDForRequest(r)) if err != nil || host == "" || strings.ContainsAny(host, "/\\?#@ \t\r\n") { return "" } scheme := "https" if host == "localhost" || net.ParseIP(host) != nil { if r.TLS == nil { scheme = "http" } if requestHost, port, err := net.SplitHostPort(r.Host); err == nil && requestHost == host { host = net.JoinHostPort(host, port) } if strings.Contains(host, ":") && !strings.HasPrefix(host, "[") && net.ParseIP(host) != nil { host = "[" + host + "]" } } return (&url.URL{Scheme: scheme, Host: host}).String() } func (s *Server) socialMetadataForRequest(r *http.Request, name string, data map[string]any) *socialMetadata { switch name { case "anonymous_view.gohtml", "page.gohtml", "home.gohtml", "search_home.gohtml", "city_map.gohtml", "city_node.gohtml": default: return nil } domain := s.domainUUIDForRequest(r) origin := s.socialOrigin(r) if origin == "" { return nil } mode, err := s.store.GetConfigInDomain(domain, "authorization_mode") noIndex := err != nil || mode == "enforcing" && !(name == "anonymous_view.gohtml" && isAnonymousProjection(r)) site := socialText(s.getWikiName(domain), 90) m := &socialMetadata{Title: site, Description: site, SiteName: site, Locale: "en_US", URL: origin + cityPageHref(r.URL.Path), Image: origin + socialImagePath, ImageType: "image/png", ImageAlt: "Pages Wiki · Knowledge City", ImageWidth: 1200, ImageHeight: 630, Card: "summary_large_image", NoIndex: noIndex} if strings.HasPrefix(s.getLocale(domain), "ja") { m.Locale = "ja_JP" } if title, ok := data["Title"].(string); ok && title != "" { m.Title = socialText(title, 90) } if source, ok := data["SocialSource"].(string); ok { title, description := socialHTML(source) if title != "" { m.Title = title } if description != "" { m.Description = description } } if view, ok := data["ContentView"].(contentView); ok { if view.Kind != "html" { m.Description = socialText(view.Filename+" · "+view.ContentType, 200) } mime := strings.ToLower(strings.Split(view.ContentType, ";")[0]) // Use the semantic address so the image always follows the current // public document; no private asset/revision URLs or foreground decode. if view.Kind == "image" && view.SizeBytes > 0 && view.SizeBytes < 5<<20 && (mime == "image/png" || mime == "image/jpeg" || mime == "image/gif" || mime == "image/webp") { if path, ok := data["Path"].(string); ok && strings.HasPrefix(path, "/") { m.Image = origin + cityPageHref("/v/raw/path:"+path) m.ImageType, m.ImageAlt, m.ImageWidth, m.ImageHeight = mime, m.Title, 0, 0 m.Card = "summary" } } } if node, ok := data["Node"].(store.CityNodeDetail); ok { parent := "" if node.Quartier != nil { parent = node.Quartier.Name } m.URL = origin + cityDetailHref(node.Kind, node.Name, parent, node.UUID) m.Description = socialText(node.Description, 200) if m.Description == "" { m.Description = socialText(node.PathPrefix+" · "+site, 200) } } if r.URL.Path == "/" { m.Title = site } pageTitle := strings.TrimSpace(m.Title) if pageTitle == "" || pageTitle == site { candidate := path.Base(strings.TrimSuffix(r.URL.Path, "/")) if candidate != "." && candidate != "/" && candidate != "" { pageTitle = candidate } } // The X intent receives the canonical URL separately. Keep ShareText // limited to the human-readable title so callers do not duplicate it. m.ShareText = pageTitle + " - " + site return m }