Phase 3 part 7: Bing, IndexNow and search engine setup
SEO → Search engines: a pull request that adds the IndexNow key file, a robots.txt naming the sitemap, and turns the sitemap on when the Hugo config disables it (checked with a build); IndexNow submissions of pages that go live through Changes or Briefs once the key file is served; the sitemaps Search Console has; and, with an API key, Bing's traffic, searches and crawl issues. NewlyLive no longer queries while a result set holds the database's only connection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -29,6 +29,8 @@ type Repo struct {
|
||||
Token string // sent as a header, never stored in the copy
|
||||
Branch string // the branch that gets published
|
||||
Author string // "Name <email>" for commits
|
||||
|
||||
touched []string // files written outside content/, to commit
|
||||
}
|
||||
|
||||
// git runs a git command in the working copy, with the token as an HTTP
|
||||
@@ -103,7 +105,7 @@ func (r *Repo) NewBranch(ctx context.Context, name string) error {
|
||||
|
||||
// Diff is the working copy's uncommitted change, new files included.
|
||||
func (r *Repo) Diff(ctx context.Context) (string, error) {
|
||||
if _, err := r.git(ctx, "add", "-A", "--intent-to-add", "content"); err != nil {
|
||||
if _, err := r.git(ctx, append([]string{"add", "-A", "--intent-to-add", "content"}, r.touched...)...); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return r.git(ctx, "diff")
|
||||
@@ -111,7 +113,7 @@ func (r *Repo) Diff(ctx context.Context) (string, error) {
|
||||
|
||||
// CommitAndPush commits everything changed and pushes the current branch.
|
||||
func (r *Repo) CommitAndPush(ctx context.Context, branch, message string) error {
|
||||
if _, err := r.git(ctx, "add", "-A", "content"); err != nil {
|
||||
if _, err := r.git(ctx, append([]string{"add", "-A", "content"}, r.touched...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := r.git(ctx, "commit", "--quiet", "-am", message); err != nil {
|
||||
@@ -442,3 +444,93 @@ func (r *Repo) ReplaceBody(rel, body string) error {
|
||||
out := strings.Join(lines[:end+1], "\n") + "\n\n" + strings.TrimSpace(body) + "\n"
|
||||
return os.WriteFile(path, []byte(out), 0o644)
|
||||
}
|
||||
|
||||
// --- Files outside content ---
|
||||
|
||||
// inside resolves a path in the repository, refusing ones that leave it.
|
||||
func (r *Repo) inside(rel string) (string, error) {
|
||||
p := filepath.Clean(filepath.Join(r.Dir, rel))
|
||||
if !strings.HasPrefix(p, filepath.Clean(r.Dir)+string(filepath.Separator)) {
|
||||
return "", fmt.Errorf("%s is outside the repository", rel)
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Exists reports whether a file is in the working copy.
|
||||
func (r *Repo) Exists(rel string) bool {
|
||||
p, err := r.inside(rel)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
_, err = os.Stat(p)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// WriteFile writes a file anywhere in the repository and marks it to be
|
||||
// committed.
|
||||
func (r *Repo) WriteFile(rel, content string) error {
|
||||
p, err := r.inside(rel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(p, []byte(content), 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
r.touched = append(r.touched, filepath.ToSlash(rel))
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConfigFile is the site's main Hugo configuration file, or "".
|
||||
func (r *Repo) ConfigFile() string {
|
||||
for _, name := range []string{"hugo.toml", "hugo.yaml", "hugo.yml", "config.toml", "config.yaml", "config.yml",
|
||||
"config/_default/hugo.toml", "config/_default/hugo.yaml", "config/_default/config.toml", "config/_default/config.yaml"} {
|
||||
if r.Exists(name) {
|
||||
return name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var disableKinds = regexp.MustCompile(`(?m)^(\s*disableKinds\s*[:=]\s*\[)([^\]]*)(\])`)
|
||||
|
||||
// EnableSitemap takes "sitemap" out of the config's disableKinds, when it's
|
||||
// written on one line. It reports whether it changed anything.
|
||||
func (r *Repo) EnableSitemap() (bool, error) {
|
||||
cfg := r.ConfigFile()
|
||||
if cfg == "" {
|
||||
return false, nil
|
||||
}
|
||||
p, _ := r.inside(cfg)
|
||||
raw, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
changed := false
|
||||
out := disableKinds.ReplaceAllStringFunc(string(raw), func(m string) string {
|
||||
parts := disableKinds.FindStringSubmatch(m)
|
||||
var keep []string
|
||||
for _, k := range strings.Split(parts[2], ",") {
|
||||
if t := strings.Trim(strings.TrimSpace(k), `"'`); strings.EqualFold(t, "sitemap") {
|
||||
changed = true
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(k) != "" {
|
||||
keep = append(keep, strings.TrimSpace(k))
|
||||
}
|
||||
}
|
||||
return parts[1] + strings.Join(keep, ", ") + parts[3]
|
||||
})
|
||||
if !changed {
|
||||
return false, nil
|
||||
}
|
||||
return true, r.WriteFile(cfg, out)
|
||||
}
|
||||
|
||||
// BuiltFile reads a file from a build.
|
||||
func BuiltFile(outDir, rel string) (string, error) {
|
||||
raw, err := os.ReadFile(filepath.Join(outDir, rel))
|
||||
return string(raw), err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user