diff --git a/package.json b/package.json index 52833a1b..1148fb01 100644 --- a/package.json +++ b/package.json @@ -22,19 +22,35 @@ "precommit": "lint-staged" }, "lint-staged": { - "*.js": ["eslint --fix", "git add"] + "*.js": [ + "eslint --fix", + "git add" + ] }, "jest": { "verbose": false, - "roots": ["frontend"], + "roots": [ + "frontend" + ], "moduleNameMapper": { "^.*[.](s?css|css)$": "/__mocks__/styleMock.js", "^.*[.](gif|ttf|eot|svg)$": "/__test__/fileMock.js" }, - "moduleFileExtensions": ["js", "jsx", "json"], - "moduleDirectories": ["node_modules"], - "modulePaths": ["frontend"], - "setupFiles": ["/setupJest.js", "/__mocks__/window.js"] + "moduleFileExtensions": [ + "js", + "jsx", + "json" + ], + "moduleDirectories": [ + "node_modules" + ], + "modulePaths": [ + "frontend" + ], + "setupFiles": [ + "/setupJest.js", + "/__mocks__/window.js" + ] }, "engines": { "node": ">= 7.6" @@ -69,6 +85,7 @@ "debug": "2.2.0", "dotenv": "^4.0.0", "emoji-name-map": "1.1.2", + "emoji-regex": "^6.5.1", "eslint": "^3.19.0", "eslint-config-react-app": "^0.6.2", "eslint-import-resolver-webpack": "^0.3.1", diff --git a/server/migrations/20170729215619-emoji.js b/server/migrations/20170729215619-emoji.js new file mode 100644 index 00000000..492a4b2e --- /dev/null +++ b/server/migrations/20170729215619-emoji.js @@ -0,0 +1,12 @@ +module.exports = { + up: (queryInterface, Sequelize) => { + queryInterface.addColumn('documents', 'emoji', { + type: Sequelize.STRING, + allowNull: true, + }); + }, + + down: (queryInterface, _Sequelize) => { + queryInterface.removeColumn('documents', 'emoji'); + }, +}; diff --git a/server/models/Document.js b/server/models/Document.js index b912bc30..8a125221 100644 --- a/server/models/Document.js +++ b/server/models/Document.js @@ -2,6 +2,7 @@ import slug from 'slug'; import _ from 'lodash'; import randomstring from 'randomstring'; +import emojiRegex from 'emoji-regex'; import isUUID from 'validator/lib/isUUID'; import { DataTypes, sequelize } from '../sequelize'; @@ -34,6 +35,14 @@ const createUrlId = doc => { return (doc.urlId = doc.urlId || randomstring.generate(10)); }; +const extractEmoji = doc => { + const regex = emojiRegex(); + const match = regex.exec(doc.title); + + if (match.length) return match[0]; + return null; +}; + const beforeSave = async doc => { doc.html = convertToMarkdown(doc.text); doc.preview = truncateMarkdown(doc.text, 160); @@ -53,6 +62,7 @@ const beforeSave = async doc => { // We'll add the current user as revision hasn't been generated yet ids.push(doc.lastModifiedById); doc.collaboratorIds = _.uniq(ids); + doc.emoji = extractEmoji(doc); return doc; }; diff --git a/server/presenters/document.js b/server/presenters/document.js index f26df3fc..4dce75bb 100644 --- a/server/presenters/document.js +++ b/server/presenters/document.js @@ -22,6 +22,7 @@ async function present(ctx: Object, document: Document, options: ?Options) { text: document.text, html: document.html, preview: document.preview, + emoji: document.emoji, createdAt: document.createdAt, createdBy: presentUser(ctx, document.createdBy), updatedAt: document.updatedAt,