Skip to content

Add backlinks feature (issue #164)#478

Merged
compscidr merged 2 commits intomainfrom
feature/backlinks
Mar 3, 2026
Merged

Add backlinks feature (issue #164)#478
compscidr merged 2 commits intomainfrom
feature/backlinks

Conversation

@compscidr
Copy link
Collaborator

Summary

  • Track internal backlinks between blog posts by parsing markdown content for /posts/YYYY/MM/DD/slug links
  • Track external backlinks via HTTP Referer headers with hit counts and timestamps
  • Extract external links from post content for admin visibility
  • Display four admin-only sections on post pages: Backlinks, Linked Posts, External Links, External Backlinks
  • Compute backlinks on post create/update and seed existing posts during migration

Changes

  • blog/post.go: Added Backlink, ExternalBacklink models and ExternalLinks() method
  • blog/blog.go: Added ComputeBacklinks, GetBacklinks, GetOutboundLinks, GetExternalBacklinks, TrackReferer methods; wired referer tracking into Post() and NoRoute() handlers
  • admin/admin.go: Call ComputeBacklinks after create/update; pass backlink data to admin template
  • templates/post.html: Added admin-only backlink sections
  • templates/post-admin.html: Added backlink sections
  • tools/migrate.go: Added new models to AutoMigrate; added seedBacklinks for existing posts
  • blog/blog_test.go: Added TestBacklinks and TestExternalBacklinks tests

Test plan

  • go test ./... passes
  • Create two posts where one links to the other; verify backlink appears on target post
  • Visit a post with an external referer header; verify external backlink recorded
  • Verify external URLs in post content appear under "External Links"
  • Verify non-admin users do not see backlink sections

Closes #164

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings March 3, 2026 06:23
@codecov
Copy link

codecov bot commented Mar 3, 2026

Codecov Report

❌ Patch coverage is 54.96183% with 59 lines in your changes missing coverage. Please review.
✅ Project coverage is 45.36%. Comparing base (5901b46) to head (2054e7c).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
tools/migrate.go 31.03% 19 Missing and 1 partial ⚠️
blog/blog.go 76.25% 15 Missing and 4 partials ⚠️
blog/post.go 0.00% 11 Missing ⚠️
admin/admin.go 18.18% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #478      +/-   ##
==========================================
+ Coverage   43.23%   45.36%   +2.13%     
==========================================
  Files           6        6              
  Lines        1071     1186     +115     
==========================================
+ Hits          463      538      +75     
- Misses        573      607      +34     
- Partials       35       41       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a backlinks feature to the blog by persisting (1) internal post-to-post link relationships derived from markdown content and (2) external backlinks derived from HTTP Referer headers, then exposing this data in admin-only UI sections.

Changes:

  • Introduces Backlink and ExternalBacklink models plus post-level external link extraction.
  • Adds backlink computation/retrieval + referer tracking logic to blog handlers.
  • Extends migrations to create/seed backlink data and adds tests for both internal and external backlinks.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
tools/migrate.go Migrates new tables and seeds internal backlinks for existing posts.
blog/post.go Adds backlink models and Post.ExternalLinks() extraction helper.
blog/blog.go Implements backlink computation/queries and referer tracking; wires tracking into handlers.
admin/admin.go Triggers backlink recomputation on post create/update; passes backlink data to templates.
templates/post.html Adds admin-only sections for backlinks/outbound links/external links/external backlinks.
templates/post-admin.html Adds backlink/outbound/external link sections to the admin post view.
blog/blog_test.go Adds tests covering internal backlinks and external referer tracking behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@compscidr compscidr force-pushed the feature/backlinks branch from 2054e7c to 6d17025 Compare March 3, 2026 06:37
compscidr and others added 2 commits March 2, 2026 22:38
Implements issue #164: internal backlinks (which posts link to each other),
external backlink tracking via HTTP Referer headers, and external link
extraction from post content. Backlink sections are admin-only and visible
on both the regular post page and the admin post editor.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Fix swapped logged_in/is_admin in admin Post handler
- Add indexes on Backlink fields and uniqueIndex on ExternalBacklink
- Use gorm:"type:text" for Referer column to support long URLs
- Distinguish ErrRecordNotFound from other DB errors in TrackReferer
- Use gorm.Expr for atomic hit_count increment
- Compare hostnames without ports for self-referral detection
- Fix comment: "Skip non-HTTP(S) schemes"
- Use exact slug match and bounded date range in ComputeBacklinks/seedBacklinks
- Log errors from strconv.Atoi and db.Create in seedBacklinks
- Add test for self-referral with port mismatch

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@compscidr compscidr force-pushed the feature/backlinks branch from 6d17025 to 8cca2a8 Compare March 3, 2026 06:38
@compscidr compscidr merged commit 6622c2f into main Mar 3, 2026
2 checks passed
@compscidr compscidr deleted the feature/backlinks branch March 3, 2026 06:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

implement backlinks

2 participants