Added slugified urls to documents

This commit is contained in:
Jori Lallo 2016-05-25 22:38:45 -07:00
parent 4c4a78c559
commit 1b7dff92d9
3 changed files with 22 additions and 0 deletions

View File

@ -77,6 +77,7 @@
"pg": "^4.5.3",
"pg-hstore": "^2.3.2",
"querystring": "^0.2.0",
"randomstring": "^1.1.5",
"react": "^0.14.7",
"react-codemirror": "^0.2.5",
"react-dom": "^0.14.7",
@ -98,6 +99,7 @@
"sequelize": "^3.21.0",
"sequelize-cli": "^2.3.1",
"sequelize-encrypted": "^0.1.0",
"slug": "^0.9.1",
"style-loader": "^0.13.0",
"to-markdown": "^2.0.1",
"truncate-html": "0.0.6",

View File

@ -1,3 +1,5 @@
import slug from 'slug';
import randomstring from 'randomstring';
import {
DataTypes,
sequelize,
@ -10,14 +12,25 @@ import Atlas from './Atlas';
import Team from './Team';
import User from './User';
slug.defaults.mode ='rfc3986';
const generateSlug = (title, urlId) => {
const slugifiedTitle = slug(title);
return `${slugifiedTitle}-${urlId}`;
};
const Document = sequelize.define('document', {
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
urlId: { type: DataTypes.STRING, primaryKey: true },
title: DataTypes.STRING,
text: DataTypes.TEXT,
html: DataTypes.TEXT,
preview: DataTypes.TEXT,
}, {
hooks: {
beforeValidate: (doc) => {
doc.urlId = randomstring.generate(15);
},
beforeCreate: (doc) => {
doc.html = convertToMarkdown(doc.text);
doc.preview = truncateMarkdown(doc.text, 160);
@ -26,6 +39,12 @@ const Document = sequelize.define('document', {
doc.html = convertToMarkdown(doc.text);
doc.preview = truncateMarkdown(doc.text, 160);
},
},
instanceMethods: {
buildUrl() {
const slugifiedTitle = slug(this.title);
return `${slugifiedTitle}-${this.urlId}`;
}
}
});

View File

@ -56,6 +56,7 @@ export function presentAtlas(atlas, includeRecentDocuments=false) {
export async function presentDocument(document, includeAtlas=false) {
const data = {
id: document.id,
url: document.buildUrl(),
title: document.title,
text: document.text,
html: document.html,