mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 18:31:57 +03:00
df7e64fafa
refs #10790 - Moved /core/apps into core/frontend - Moved /core/server/helpers to /core/frontend/helpers along with /core/server/services/themes - Changed helper location in overrides - Moved /core/server/services/routing to /core/frontend/services - Moved /core/server/services/url to /core/frontend/services - Moved /core/server/data/meta to /core/frontend/meta - Moved /core/server/services/rss to /core/frontend/services - Moved /core/server/data/xml to /core/frontend/services
94 lines
2.5 KiB
JavaScript
94 lines
2.5 KiB
JavaScript
const debug = require('ghost-ignition').debug('services:routing:static-pages-router');
|
|
const urlUtils = require('../../../server/lib/url-utils');
|
|
const ParentRouter = require('./ParentRouter');
|
|
const controllers = require('./controllers');
|
|
const common = require('../../../server/lib/common');
|
|
|
|
/**
|
|
* @description Resource: pages
|
|
*/
|
|
class StaticPagesRouter extends ParentRouter {
|
|
constructor(RESOURCE_CONFIG) {
|
|
super('StaticPagesRouter');
|
|
|
|
this.RESOURCE_CONFIG = RESOURCE_CONFIG.QUERY.page;
|
|
|
|
// @NOTE: Permalink is always /:slug, not configure able
|
|
this.permalinks = {
|
|
value: '/:slug/'
|
|
};
|
|
|
|
this.permalinks.getValue = (options = {}) => {
|
|
options = options || {};
|
|
|
|
// @NOTE: url options are only required when registering urls in express.
|
|
// e.g. the UrlService will access the routes and doesn't want to know about possible url options
|
|
if (options.withUrlOptions) {
|
|
return urlUtils.urlJoin(this.permalinks.value, '/:options(edit)?/');
|
|
}
|
|
|
|
return this.permalinks.value;
|
|
};
|
|
|
|
debug(this.permalinks);
|
|
|
|
this._registerRoutes();
|
|
}
|
|
|
|
/**
|
|
* @description Register all routes of this router.
|
|
* @private
|
|
*/
|
|
_registerRoutes() {
|
|
// REGISTER: prepare context
|
|
this.router().use(this._prepareContext.bind(this));
|
|
|
|
this.router().param('slug', this._respectDominantRouter.bind(this));
|
|
|
|
// REGISTER: permalink for static pages
|
|
this.mountRoute(this.permalinks.getValue({withUrlOptions: true}), controllers.entry);
|
|
|
|
common.events.emit('router.created', this);
|
|
}
|
|
|
|
/**
|
|
* @description Prepare context for futher middlewares/controllers.
|
|
* @param {Object} req
|
|
* @param {Object} res
|
|
* @param {Function} next
|
|
* @private
|
|
*/
|
|
_prepareContext(req, res, next) {
|
|
res.routerOptions = {
|
|
type: 'entry',
|
|
filter: this.filter,
|
|
permalinks: this.permalinks.getValue({withUrlOptions: true}),
|
|
resourceType: this.getResourceType(),
|
|
query: this.RESOURCE_CONFIG,
|
|
context: ['page']
|
|
};
|
|
|
|
next();
|
|
}
|
|
|
|
/**
|
|
* @description Resource type.
|
|
* @returns {string}
|
|
*/
|
|
getResourceType() {
|
|
return 'pages';
|
|
}
|
|
|
|
/**
|
|
* @description This router has no index/default route. "/:slug/" is dynamic.
|
|
* @returns {null}
|
|
*/
|
|
getRoute() {
|
|
return null;
|
|
}
|
|
|
|
reset() {}
|
|
}
|
|
|
|
module.exports = StaticPagesRouter;
|