1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-07-20 21:59:41 +02:00

Beautiful urls (#18)

* Added uri property to Page model

* Added aliases collection

* Added routing form aliases

* Fixed redirect after page creation

* Added abiltity to support few pages with same title

* Added ability to change uri manually

* Changed hash function

* Changed uri parsing

* Removed pages controller promise

* Modified page's tests

* Added tests for alias model

* Added tests for aliases

* Escaping special characters

* Added missed files

* Fixed bugs related to translation

* Fixed parent page link

* Added server validation for uri

* Changed css properties order

* Made uri property of page be optional

* Prevented alias creation from empty uri

* Moved alias types to model
This commit is contained in:
DorofeevMark 2019-01-25 02:23:00 +03:00 committed by Peter Savchenko
parent ed69336481
commit d872e78339
28 changed files with 807 additions and 45 deletions

View file

@ -1,4 +1,5 @@
const Model = require('../models/page');
const Alias = require('../models/alias');
/**
* @class Pages
@ -85,7 +86,18 @@ class Pages {
const page = new Model(data);
return page.save();
const insertedPage = await page.save();
if (insertedPage.uri) {
const alias = new Alias({
id: insertedPage._id,
type: Alias.types.PAGE
}, insertedPage.uri);
alias.save();
}
return insertedPage;
} catch (validationError) {
throw new Error(validationError);
}
@ -132,13 +144,33 @@ class Pages {
*/
static async update(id, data) {
const page = await Model.get(id);
const previousUri = page.uri;
if (!page._id) {
throw new Error('Page with given id does not exist');
}
if (data.uri && !data.uri.match(/^[a-z0-9'-]+$/i)) {
throw new Error('Uri has unexpected characters');
}
page.data = data;
return page.save();
const updatedPage = await page.save();
if (updatedPage.uri !== previousUri) {
if (updatedPage.uri) {
const alias = new Alias({
id: updatedPage._id,
type: Alias.types.PAGE
}, updatedPage.uri);
alias.save();
}
Alias.markAsDeprecated(previousUri);
}
return updatedPage;
}
/**