Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit 10d165e

Browse files
improved pages service
1 parent d8d50cb commit 10d165e

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

server/hooks/create-slug.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const slug = require('slug');
33
const getUniqueSlug = require('../helper/get-unique-slug');
44
const { isEmpty } = require('lodash');
55

6-
module.exports = function (options = { field: null, overwrite: false }) {
6+
module.exports = function (options = { field: null, overwrite: false, unique: true }) {
77
return function (hook) {
88
if (!options.field || !hook.data[options.field]) return hook;
99

@@ -16,11 +16,16 @@ module.exports = function (options = { field: null, overwrite: false }) {
1616
const titleSlug = slug(hook.data[options.field], {
1717
lower: true
1818
});
19-
getUniqueSlug(hook.service, titleSlug, null, hook.id)
20-
.then((uniqueSlug) => {
21-
hook.data.slug = uniqueSlug;
22-
resolve(hook);
23-
});
19+
if (options.unique) {
20+
getUniqueSlug(hook.service, titleSlug, null, hook.id)
21+
.then((uniqueSlug) => {
22+
hook.data.slug = uniqueSlug;
23+
resolve(hook);
24+
});
25+
} else {
26+
hook.data.slug = titleSlug;
27+
resolve(hook);
28+
}
2429
});
2530
};
2631
};

server/models/pages.model.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@ module.exports = function (app) {
1717
wasSeeded: { type: Boolean }
1818
});
1919

20+
pages.index(
21+
{ slug: 1, language: 1 },
22+
{ unique: true }
23+
);
24+
2025
return mongooseClient.model('pages', pages);
2126
};

server/services/pages/pages.hooks.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ const { authenticate } = require('feathers-authentication').hooks;
44
const isAdmin = require('../../hooks/is-admin');
55
const createSlug = require('../../hooks/create-slug');
66

7+
const cleanupHTML = () => {
8+
return (hook) => {
9+
hook.data.content = hook.data.content
10+
.replace(/<[a-z]>[\s]*<\/[a-z]>/igm, '')
11+
.replace(/<p>[\s]*(<br ?\/?>)+[\s]*<\/p>/igm, '<br />')
12+
.replace(/(<br ?\/?>){2,}/igm, '<br />')
13+
.replace(/[\n]{3,}/igm, '\n\n');
14+
};
15+
};
16+
717
module.exports = {
818
before: {
919
all: [],
@@ -15,21 +25,24 @@ module.exports = {
1525
isVerified(),
1626
isAdmin()
1727
),
18-
createSlug({ field: 'title' })
28+
createSlug({ field: 'key', unique: false }),
29+
cleanupHTML()
1930
],
2031
update: [
2132
authenticate('jwt'),
2233
unless(isProvider('server'),
2334
isVerified(),
2435
isAdmin()
25-
)
36+
),
37+
cleanupHTML()
2638
],
2739
patch: [
2840
authenticate('jwt'),
2941
unless(isProvider('server'),
3042
isVerified(),
3143
isAdmin()
32-
)
44+
),
45+
cleanupHTML()
3346
],
3447
remove: [
3548
authenticate('jwt'),

0 commit comments

Comments
 (0)