58 lines
1.2 KiB
TypeScript
58 lines
1.2 KiB
TypeScript
|
import Sequelize, {DataTypes, Model} from 'sequelize';
|
||
|
import sequelize from '../dbobject';
|
||
|
|
||
|
import marked from 'marked';
|
||
|
import slugify from 'slugify';
|
||
|
import createDomPurify from 'dompurify';
|
||
|
const {JSDOM} = require('jsdom');
|
||
|
const dompurify = createDomPurify(new JSDOM().window);
|
||
|
|
||
|
export interface articleI extends Model {
|
||
|
id: string;
|
||
|
title: string;
|
||
|
markdown: string;
|
||
|
slug: string;
|
||
|
sanitizedHtml: string;
|
||
|
}
|
||
|
|
||
|
const Article = sequelize.define<articleI>('article', {
|
||
|
id: {
|
||
|
type: DataTypes.UUID,
|
||
|
allowNull: false,
|
||
|
unique: true,
|
||
|
primaryKey: true,
|
||
|
defaultValue: Sequelize.UUIDV4,
|
||
|
},
|
||
|
title: {
|
||
|
type: DataTypes.STRING,
|
||
|
allowNull: false,
|
||
|
unique: true,
|
||
|
},
|
||
|
markdown: {
|
||
|
type: DataTypes.TEXT,
|
||
|
allowNull: false,
|
||
|
},
|
||
|
slug: {
|
||
|
type: DataTypes.STRING,
|
||
|
allowNull: false,
|
||
|
unique: true,
|
||
|
},
|
||
|
sanitizedHtml: {
|
||
|
type: DataTypes.TEXT,
|
||
|
allowNull: false,
|
||
|
},
|
||
|
});
|
||
|
|
||
|
Article.addHook('beforeValidate', (article: articleI, options: any) => {
|
||
|
options.fields.push('slug');
|
||
|
options.fields.push('sanitizedHtml');
|
||
|
if (article.title) {
|
||
|
article.slug = slugify(article.title, {lower: true, strict: true});
|
||
|
}
|
||
|
if (article.markdown) {
|
||
|
article.sanitizedHtml = dompurify.sanitize(marked(article.markdown));
|
||
|
}
|
||
|
});
|
||
|
|
||
|
export default Article;
|