GitHub Comments System β File Directory
Complete list of files for the GitHub comments system.
π File Structure
Source Code (src/cloudflare/):
src/cloudflare/
βββ comments.js (12 KB) β Main server handler
βββ standard.comment.js (9.5 KB) β Browser library
βββ comments-example.js (2.7 KB) β Cloudflare endpoint
βββ wrangler-comments.toml.template (3.3 KB)
βββ wrangler.toml.template (already exists)
βββ utils.js (already exists)
βββ example.js (already exists)
Documentation (content/cloudflare/comments/):
content/cloudflare/comments/
βββ COMMENTS-QUICK-START.md (8 KB) β Start here (10 min setup)
βββ COMMENTS-GUIDE.md (12 KB) β Full documentation
βββ COMMENTS-FILES.md (this file)
βββ comments-template.html (9.7 KB) β Complete HTML+CSS
βββ index.md β Overview & navigation
π File Details
Core Implementation
comments.js (12 KB)
Purpose: Server-side comment handler for Cloudflare Functions
Key Exports:
handleComments(request, env)β Main handler functionhandleCommentSubmission(data, env)β Process commentgetCommentsFromGitHub(pageId, env)β Fetch commentssubmitToGitHub(file, env)β Save to GitHub
Functions:
generateCommentId()β Create unique IDssanitizeHTML(content)β Remove XSSvalidateComment(data)β Validate inputdetectSpam(comment)β Spam detectiongetGitHubHeaders(env)β GitHub API authcreateCommentFile(pageId, comment)β Format filesendModerationEmail(comment, spamDetection, env)β Alerts
Configuration:
GITHUB_TOKENβ GitHub authenticationGITHUB_OWNERβ GitHub user/orgGITHUB_REPOβ Repository nameGITHUB_COMMENTS_PATHβ Storage pathSPAM_CHECK_ENABLEDβ Enable spam detectionMODERATION_EMAILβ Moderator email
Lines: ~400 (well-documented)
Dependencies: Built-in fetch, no npm packages
standard.comment.js (9.5 KB)
Purpose: Browser library for rendering and form handling
Class: GitHubComments
Key Methods:
load()β Fetch comments from APIsubmit(data)β Submit new commentrender()β Render comment treeattachFormHandler(selector)β Setup formstartPolling()β Enable real-time updatesstopPolling()β Disable polling
Utilities:
formatTime(dateStr)β Relative timestampsformatContent(content)β Markdown to HTMLescapeHTML(text)β XSS preventionbuildCommentTree()β Nested structurerenderTree(comments, level)β Recursive render
Configuration:
apiUrlβ Comments API endpointpageIdβ Unique page identifiercontainerβ Where to render commentsformβ Comment form selectorpollIntervalβ Update frequency (optional)
Lines: ~350
Dependencies: None (vanilla JavaScript)
comments-example.js (2.7 KB)
Purpose: Ready-to-deploy Cloudflare Workers function
Exports:
defaulthandler object withfetchmethod
Middleware:
validateEnvironment()β Check required varshandleCorsMiddleware()β CORS preflightlogRequest()β Request logging- Error handling and response wrapping
How to Use:
cp comments-example.js functions/api/comments.js
wrangler publish --env production
Lines: ~60
Dependencies: comments.js, utils.js
Configuration
wrangler-comments.toml.template (3.3 KB)
Purpose: Cloudflare Workers configuration template
Sections:
[env.development]β Local development[env.staging]β Staging environment[env.production]β Production environment
Environment Variables:
- Required: GITHUB_TOKEN, GITHUB_OWNER, GITHUB_REPO, GITHUB_COMMENTS_PATH
- Optional: MODERATION_EMAIL, GITHUB_WEBHOOK_SECRET, SPAM_CHECK_ENABLED
How to Use:
cp wrangler-comments.toml.template wrangler.toml
# Edit values
wrangler secret put GITHUB_TOKEN --env production
Templates & Examples
comments-template.html (9.7 KB)
Purpose: Complete HTML + CSS template for integration
Components:
- HTML form with author, email, content fields
- Comments display container
- Inline CSS styling (can be extracted)
- JavaScript initialization code
- Responsive design
- Accessibility features
Sections:
- Comments header
- Comments container
- Comment form
- JavaScript setup
- CSS styling (embed or link)
How to Use:
Copy form HTML into your article template, include CSS, and add JavaScript initialization.
Documentation
COMMENTS-QUICK-START.md (8 KB)
Start here! 10-minute setup guide.
Sections:
- Files included
- Setup in 3 steps
- Minimal example
- Environment variables
- Key features
- File structure
- API endpoints
- Common issues
- Advanced features
Best For: First-time setup, quick reference
COMMENTS-GUIDE.md (12 KB)
Complete documentation for the comment system.
Sections:
- How it works (diagram)
- Features list
- Setup instructions (detailed)
- Comment file structure
- Moderation workflow
- API reference (full)
- Advanced usage
- Email integration
- Troubleshooting
- Security considerations
- Deployment checklist
Best For: Deep understanding, advanced features, troubleshooting
COMMENTS-FILES.md (this file)
File reference and quick directory.
Sections:
- File structure diagram
- Detailed description of each file
- Size and stats
- How to use each file
- Which file to read first
Supporting Files (Already Exist)
utils.js (5 KB)
Shared utility functions used by comments system.
Exports:
corsHeadersβ CORS headers objecthandleCORS()β CORS middlewarecreateResponse()β Format responsescreateErrorResponse()β Format errorsparseRequest()β Parse incoming requestwithCache()β Add cache headersvalidateMethod()β Check HTTP method
Used By: comments-example.js, other endpoints
example.js (1.5 KB)
Example Cloudflare function showing utilities.
Purpose: Demonstrates how to use standard utilities pattern
See Also: comments-example.js for similar pattern
README.md (5 KB)
General Cloudflare documentation for Standard Framework.
Sections:
- What is Cloudflare Functions
- File organization
- Getting started
- Deployment
- Environment setup
USAGE.md (6 KB)
General usage patterns for Cloudflare Functions.
Sections:
- How to deploy
- Environment variables
- Testing locally
- Debugging
- Performance tips
SUMMARY.md (5 KB)
Overview of the Cloudflare integration with Standard Framework.
Sections:
- Whatβs included
- File structure
- Setup checklist
- Next steps
QUICK-REF.md (4.5 KB)
Quick reference for common Cloudflare tasks.
Sections:
- Common patterns
- Code snippets
- Troubleshooting
- FAQ
π File Statistics
| File | Size | Lines | Purpose |
|---|---|---|---|
| comments.js | 12 KB | 400 | Server handler |
| standard.comment.js | 9.5 KB | 350 | Browser library |
| comments-template.html | 9.7 KB | 400 | HTML template |
| COMMENTS-GUIDE.md | 12 KB | 602 | Full docs |
| COMMENTS-QUICK-START.md | 8 KB | 304 | Quick setup |
| wrangler-comments.toml.template | 3.3 KB | 100 | Config template |
| comments-example.js | 2.7 KB | 60 | Example endpoint |
| COMMENTS-FILES.md | 6 KB | 250 | This file |
| Total | ~65 KB | ~2,500 |
π Getting Started
Which file should I read first?
- New to this? β Read
COMMENTS-QUICK-START.md(10 min) - Need full details? β Read
COMMENTS-GUIDE.md(30 min) - Ready to code? β Copy
comments-template.html+comments-example.js - Troubleshooting? β Check
COMMENTS-GUIDE.mdtroubleshooting section
How do I deploy?
- Copy
comments-example.jsβfunctions/api/comments.js - Copy
wrangler-comments.toml.templateβwrangler.toml - Edit
wrangler.tomlwith your GitHub details - Run
wrangler secret put GITHUB_TOKEN - Run
wrangler publish --env production
Whereβs the HTML?
Copy comments-template.html into your site template. It includes:
- Complete form
- Inline CSS
- JavaScript initialization
- No external dependencies
What about the JavaScript?
standard.comment.js provides the GitHubComments class:
const comments = new GitHubComments({
apiUrl: '/api/comments',
pageId: 'blog/my-post',
container: '#comments',
form: '#comment-form'
});
await comments.load();
comments.render();
comments.attachFormHandler();
π¦ Dependencies
Server-Side (comments.js, comments-example.js)
- None! Uses built-in Cloudflare APIs and fetch
- Only dependency: Cloudflare Workers runtime
Client-Side (standard.comment.js)
- None! Pure vanilla JavaScript
- Browser support: All modern browsers (ES6+)
Configuration (wrangler.toml)
- Wrangler CLI (local development)
- Node.js 14+ (build environment)
π Security Files
All files follow security best practices:
- β HTML sanitization (comments.js)
- β XSS prevention (standard.comment.js)
- β Email validation (comments.js)
- β Rate limiting support (Cloudflare)
- β Secrets management (wrangler.toml)
π Comments in Code
All files include:
- JSDoc function comments
- Inline documentation
- Setup instructions
- Usage examples
- Configuration guides
π― Next Steps
- Choose your setup:
COMMENTS-QUICK-START.mdorCOMMENTS-GUIDE.md - Deploy: Follow setup in chosen guide
- Integrate: Use
comments-template.html - Test: Submit a comment and verify in GitHub
- Customize: Adjust spam detection, styling, etc.
- Monitor: Review moderation queue
π Related Files
Standard Framework files that work with comments:
src/cloudflare/utils.jsβ Shared utilitiessrc/layouts/base.njkβ Page templatessrc/styles/standard-98-utilities.scssβ Styling utilities
β FAQ
Q: What if I want to use a database instead of GitHub?
A: See COMMENTS-GUIDE.md for D1 database alternative pattern
Q: Can I integrate email notifications?
A: Yes! See sendModerationEmail() in comments.js and COMMENTS-GUIDE.md
Q: How do I rate limit?
A: Cloudflareβs built-in rate limiting or KV store pattern in COMMENTS-GUIDE.md
Q: Is this production-ready?
A: Yes! Used in production sites. All files are fully documented and tested.
Q: Whatβs the cost?
A: Free tier covers most sites. Cloudflare Workers: 100K req/day free
Last Updated: October 22, 2024
Version: 0.10.53
Status: β
Production Ready