-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
162 lines (149 loc) · 5.87 KB
/
Copy pathindex.js
File metadata and controls
162 lines (149 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const BlogCache = require('./lib/blogCache');
const BlogParser = require('./lib/blogParser');
const createBlogRouter = require('./lib/blogRouter');
const path = require('path');
/**
* Express Simple Static Blog
* @class ExpressStaticBlog
*/
class ExpressStaticBlog {
/**
* Create a new blog instance
* @param {Object} options - Configuration options
* @param {string} options.blogsDir - Directory containing blog HTML files
* @param {boolean} [options.cache=true] - Enable in-memory caching
* @param {string} [options.dateFormat='YYYY-MM-DD'] - URL date format
* @param {string} [options.metadataFormat='html-comment'] - Metadata format ('html-comment' or 'front-matter')
* @param {string} [options.routePrefix='/blogs'] - URL prefix for blog routes
* @param {string} [options.sortOrder='desc'] - Sort order ('desc' or 'asc')
* @param {boolean} [options.paginate=false] - Enable pagination
* @param {number} [options.perPage=10] - Items per page when paginating
* @param {string} [options.feedTitle] - RSS feed title
* @param {string} [options.feedDescription] - RSS feed description
* @param {string} [options.feedLanguage] - RSS feed language
* @param {string} [options.listTemplate='blogs'] - Template name for the blog list page
* @param {string} [options.contentFormat='full-html'] - Content format ('full-html', 'body-only', 'markdown')
* @param {string} [options.detailTemplate] - Template name for individual blog posts (not used with 'full-html')
*/
constructor(options = {}) {
// Default configuration
this.config = {
blogsDir: options.blogsDir || './blogs',
cache: options.cache !== false,
dateFormat: options.dateFormat || 'YYYY-MM-DD',
metadataFormat: options.metadataFormat || 'html-comment',
routePrefix: options.routePrefix || '/blogs',
sortOrder: options.sortOrder || 'desc',
paginate: options.paginate || false,
perPage: options.perPage || 10,
listTemplate: options.listTemplate || 'blogs',
contentFormat: options.contentFormat || 'full-html',
detailTemplate: options.detailTemplate || null,
feedTitle: options.feedTitle,
feedDescription: options.feedDescription,
feedLanguage: options.feedLanguage
};
// Resolve absolute paths
this.config.blogsDir = path.resolve(this.config.blogsDir);
// Initialize components
this.parser = new BlogParser(this.config);
this.cache = new BlogCache(this.config, this.parser);
// Initialize cache if enabled
if (this.config.cache) {
this.cache.refresh();
}
}
/**
* Get Express router with all blog routes
* @returns {express.Router} Express router instance
* @example
* const blog = expressStaticBlog({ blogsDir: './blogs' });
* app.use(blog.router());
*/
router() {
return createBlogRouter(this);
}
/**
* Get all blog posts
* @returns {Array<Object>} Array of blog objects sorted by date
* @returns {string} returns[].date - Post date (YYYY-MM-DD)
* @returns {string} returns[].title - Post title
* @returns {string} returns[].desc - Post description
* @returns {string} returns[].content - Full HTML content
*/
getAll() {
return this.cache.getAll();
}
/**
* Get blog by date
* @param {string} date - Date in configured format (default: YYYY-MM-DD)
* @returns {Object|null} Blog object or null if not found
* @example
* const post = blog.getByDate('2025-01-20');
*/
getByDate(date) {
return this.cache.getByDate(date);
}
/**
* Get recent blog posts
* @param {number} [count=5] - Number of posts to return
* @returns {Array<Object>} Array of most recent blog objects
* @example
* const recentPosts = blog.getRecent(3);
*/
getRecent(count = 5) {
return this.cache.getRecent(count);
}
/**
* Get blog by URL slug
* @param {string} slug - URL slug
* @returns {Object|null} Blog object or null if not found
* @example
* const post = blog.getBySlug('my-first-post');
*/
getBySlug(slug) {
return this.cache.getBySlug(slug);
}
/**
* Manually refresh the blog cache
* @returns {void}
* @example
* blog.refresh(); // Re-read all blog files
*/
refresh() {
this.cache.refresh();
}
/**
* Get paginated blog posts
* @param {number} [page=1] - Page number (1-based)
* @param {number} [perPage] - Items per page (defaults to config.perPage)
* @returns {Object} Paginated result
* @returns {Array<Object>} returns.items - Blog posts for this page
* @returns {number} returns.currentPage - Current page number
* @returns {number} returns.totalPages - Total number of pages
* @returns {number} returns.totalItems - Total number of posts
* @returns {boolean} returns.hasNext - Whether there is a next page
* @returns {boolean} returns.hasPrev - Whether there is a previous page
* @example
* const page = blog.getPaginated(1, 10);
*/
getPaginated(page = 1, perPage = null) {
return this.cache.getPaginated(page, perPage || this.config.perPage);
}
}
/**
* Factory function to create blog system
* @param {Object} options - Configuration options (see ExpressStaticBlog constructor)
* @returns {ExpressStaticBlog} Blog system instance
* @example
* const expressStaticBlog = require('express-simple-static-blog');
* const blog = expressStaticBlog({
* blogsDir: './views/blogs',
* routePrefix: '/blog'
* });
*/
module.exports = function createBlogSystem(options) {
return new ExpressStaticBlog(options);
};
// Export class for advanced usage
module.exports.ExpressStaticBlog = ExpressStaticBlog;