Added slugified urls to documents
This commit is contained in:
@ -77,6 +77,7 @@
|
|||||||
"pg": "^4.5.3",
|
"pg": "^4.5.3",
|
||||||
"pg-hstore": "^2.3.2",
|
"pg-hstore": "^2.3.2",
|
||||||
"querystring": "^0.2.0",
|
"querystring": "^0.2.0",
|
||||||
|
"randomstring": "^1.1.5",
|
||||||
"react": "^0.14.7",
|
"react": "^0.14.7",
|
||||||
"react-codemirror": "^0.2.5",
|
"react-codemirror": "^0.2.5",
|
||||||
"react-dom": "^0.14.7",
|
"react-dom": "^0.14.7",
|
||||||
@ -98,6 +99,7 @@
|
|||||||
"sequelize": "^3.21.0",
|
"sequelize": "^3.21.0",
|
||||||
"sequelize-cli": "^2.3.1",
|
"sequelize-cli": "^2.3.1",
|
||||||
"sequelize-encrypted": "^0.1.0",
|
"sequelize-encrypted": "^0.1.0",
|
||||||
|
"slug": "^0.9.1",
|
||||||
"style-loader": "^0.13.0",
|
"style-loader": "^0.13.0",
|
||||||
"to-markdown": "^2.0.1",
|
"to-markdown": "^2.0.1",
|
||||||
"truncate-html": "0.0.6",
|
"truncate-html": "0.0.6",
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import slug from 'slug';
|
||||||
|
import randomstring from 'randomstring';
|
||||||
import {
|
import {
|
||||||
DataTypes,
|
DataTypes,
|
||||||
sequelize,
|
sequelize,
|
||||||
@ -10,14 +12,25 @@ import Atlas from './Atlas';
|
|||||||
import Team from './Team';
|
import Team from './Team';
|
||||||
import User from './User';
|
import User from './User';
|
||||||
|
|
||||||
|
slug.defaults.mode ='rfc3986';
|
||||||
|
|
||||||
|
const generateSlug = (title, urlId) => {
|
||||||
|
const slugifiedTitle = slug(title);
|
||||||
|
return `${slugifiedTitle}-${urlId}`;
|
||||||
|
};
|
||||||
|
|
||||||
const Document = sequelize.define('document', {
|
const Document = sequelize.define('document', {
|
||||||
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
|
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
|
||||||
|
urlId: { type: DataTypes.STRING, primaryKey: true },
|
||||||
title: DataTypes.STRING,
|
title: DataTypes.STRING,
|
||||||
text: DataTypes.TEXT,
|
text: DataTypes.TEXT,
|
||||||
html: DataTypes.TEXT,
|
html: DataTypes.TEXT,
|
||||||
preview: DataTypes.TEXT,
|
preview: DataTypes.TEXT,
|
||||||
}, {
|
}, {
|
||||||
hooks: {
|
hooks: {
|
||||||
|
beforeValidate: (doc) => {
|
||||||
|
doc.urlId = randomstring.generate(15);
|
||||||
|
},
|
||||||
beforeCreate: (doc) => {
|
beforeCreate: (doc) => {
|
||||||
doc.html = convertToMarkdown(doc.text);
|
doc.html = convertToMarkdown(doc.text);
|
||||||
doc.preview = truncateMarkdown(doc.text, 160);
|
doc.preview = truncateMarkdown(doc.text, 160);
|
||||||
@ -26,6 +39,12 @@ const Document = sequelize.define('document', {
|
|||||||
doc.html = convertToMarkdown(doc.text);
|
doc.html = convertToMarkdown(doc.text);
|
||||||
doc.preview = truncateMarkdown(doc.text, 160);
|
doc.preview = truncateMarkdown(doc.text, 160);
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
instanceMethods: {
|
||||||
|
buildUrl() {
|
||||||
|
const slugifiedTitle = slug(this.title);
|
||||||
|
return `${slugifiedTitle}-${this.urlId}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -56,6 +56,7 @@ export function presentAtlas(atlas, includeRecentDocuments=false) {
|
|||||||
export async function presentDocument(document, includeAtlas=false) {
|
export async function presentDocument(document, includeAtlas=false) {
|
||||||
const data = {
|
const data = {
|
||||||
id: document.id,
|
id: document.id,
|
||||||
|
url: document.buildUrl(),
|
||||||
title: document.title,
|
title: document.title,
|
||||||
text: document.text,
|
text: document.text,
|
||||||
html: document.html,
|
html: document.html,
|
||||||
|
Reference in New Issue
Block a user