1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-08-09 15:35:25 +02:00

Added routing form aliases

This commit is contained in:
DorofeevMark 2018-12-12 21:48:13 +03:00
parent 40cd1f5a5e
commit db55a7a7d2
7 changed files with 117 additions and 4 deletions

View file

@ -0,0 +1,26 @@
const Model = require('../models/alias');
/**
* @class Aliases
* @classdesc Aliases controller
*/
class Aliases {
/**
* @static
* Find and return id of entity with given alias
*
* @param {string} alias - alias of entity
* @returns {Promise<string>}
*/
static async get(alias) {
const id = await Model.get(alias);
console.log('id', id);
if (!id) {
throw new Error('Entity with given alias does not exist');
}
return id;
}
}
module.exports = Aliases;

45
src/models/alias.js Normal file
View file

@ -0,0 +1,45 @@
const {aliases: aliasesDb} = require('../utils/database/index');
const getHashFromString = require('../utils/hash');
/**
* @typedef {Object} AliasData
* @property {string} type - entity type
* @property {string} hash - entity hash
* @property {string} id - entity id
*
*/
/**
* @class Alias
* @property {string} type - entity type
* @property {string} id - entity title
*/
class Alias {
/**
* Find and return alias with given alias
* @param {string} aliasName - alias of entity
* @returns {Promise<Alias>}
*/
static async get(aliasName) {
const hash = getHashFromString(aliasName.slice(1)).toString();
const data = await aliasesDb.findOne({hash});
return new Alias(data);
}
/**
* @constructor
*
* @param {AliasData} data
*/
constructor(data = {}) {
if (data === null) {
data = {};
}
this.id = data.id;
this.type = data.type;
this.hash = data.hash;
}
}
module.exports = Alias;

View file

@ -68,11 +68,11 @@ class Page {
* @param {PageData} pageData * @param {PageData} pageData
*/ */
set data(pageData) { set data(pageData) {
const {body, parent} = pageData; const {body, parent, uri} = pageData;
this.body = body || this.body; this.body = body || this.body;
this.title = this.extractTitleFromBody(); this.title = this.extractTitleFromBody();
this.uri = this._id; this.uri = uri || '';
this._parent = parent || this._parent; this._parent = parent || this._parent;
} }
@ -137,7 +137,10 @@ class Page {
*/ */
async save() { async save() {
if (!this._id) { if (!this._id) {
const insertedRow = await pagesDb.insert(this.data); const insertedRow = await pagesDb.insert({
...this.data,
uri: this.title.toLowerCase().split(' ').join('-')
});
this._id = insertedRow._id; this._id = insertedRow._id;
} else { } else {

27
src/routes/aliases.js Normal file
View file

@ -0,0 +1,27 @@
const express = require('express');
const router = express.Router();
const Aliases = require('../controllers/aliases');
/**
* GET /*
*
* Return document with given alias
*/
router.get('*', async (req, res) => {
try {
console.log('url ', req.originalUrl);
const id = await Aliases.get(req.originalUrl);
res.json({
success: true,
// result: page.data
});
} catch (err) {
res.status(400).json({
success: false,
error: err.message
});
}
});
module.exports = router;

View file

@ -3,6 +3,7 @@ const router = express.Router();
const home = require('./home'); const home = require('./home');
const pages = require('./pages'); const pages = require('./pages');
const aliases = require('./aliases');
const api = require('./api'); const api = require('./api');
const pagesMiddleware = require('./middlewares/pages'); const pagesMiddleware = require('./middlewares/pages');
@ -10,5 +11,6 @@ const pagesMiddleware = require('./middlewares/pages');
router.use('/', pagesMiddleware, home); router.use('/', pagesMiddleware, home);
router.use('/', pagesMiddleware, pages); router.use('/', pagesMiddleware, pages);
router.use('/api', api); router.use('/api', api);
router.use('/', aliases);
module.exports = router; module.exports = router;

10
src/utils/hash.js Normal file
View file

@ -0,0 +1,10 @@
/**
* Function for getting hash from stringToHash
*
* @param stringToHash - stringToHash to encode
* @returns {string} - hash index
*/
module.exports = function getHashFromString(stringToHash) {
return stringToHash.split('').reduce((previousHashValue, currentChar) =>
(((previousHashValue << 5) - previousHashValue) + currentChar.charCodeAt(0)) | 0, 0);
};

View file

@ -1,7 +1,7 @@
<div class="docs-aside"> <div class="docs-aside">
{% for firstLevelPage in menu %} {% for firstLevelPage in menu %}
<section class="docs-aside__section"> <section class="docs-aside__section">
<a class="docs-aside__section-title" href="/page/{{ firstLevelPage.uri }}"> <a class="docs-aside__section-title" href="/{{ firstLevelPage.uri }}">
{{ firstLevelPage.title }} {{ firstLevelPage.title }}
</a> </a>
{% if firstLevelPage.children is not empty %} {% if firstLevelPage.children is not empty %}