feat: Upgrade editor (#1227)
* WIP * document migration * fix: Handle clashing keyboard events * fix: convert getSummary * fix: parseDocumentIds * lint * fix: Remove unused plugin * Move editor version to header Add editor version check for API endpoints * fix: Editor update auto-reload Bump RME * test * bump rme * Remove slate flow types, improve themeing, bump rme * bump rme * fix: parseDocumentIds returning duplicate ID's, improved regression tests * test * fix: Missing code styles * lint * chore: Upgrade v2 migration to use AST * Bump RME * Update welcome doc * add highlight to keyboard shortcuts ref * theming improvements * fix: Code comments show as headings, closes #1255 * loop * fix: TOC highlighting * lint * add: Automated backup of docs before migration * Update embeds to new format * fix: React warning * bump to final editor version 10.0.0 * test
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
// @flow
|
||||
import { map, find, compact, uniq } from 'lodash';
|
||||
import randomstring from 'randomstring';
|
||||
import MarkdownSerializer from 'slate-md-serializer';
|
||||
import Plain from 'slate-plain-serializer';
|
||||
import randomstring from 'randomstring';
|
||||
import Sequelize, { type Transaction } from 'sequelize';
|
||||
import removeMarkdown from '@tommoor/remove-markdown';
|
||||
|
||||
@@ -15,10 +14,10 @@ import slugify from '../utils/slugify';
|
||||
import Revision from './Revision';
|
||||
|
||||
const Op = Sequelize.Op;
|
||||
const Markdown = new MarkdownSerializer();
|
||||
const URL_REGEX = /^[0-9a-zA-Z-_~]*-([a-zA-Z0-9]{10,15})$/;
|
||||
const serializer = new MarkdownSerializer();
|
||||
|
||||
export const DOCUMENT_VERSION = 1;
|
||||
export const DOCUMENT_VERSION = 2;
|
||||
|
||||
const createRevision = (doc, options = {}) => {
|
||||
// we don't create revisions for autosaves
|
||||
@@ -52,7 +51,9 @@ const createUrlId = doc => {
|
||||
};
|
||||
|
||||
const beforeCreate = async doc => {
|
||||
doc.version = DOCUMENT_VERSION;
|
||||
if (doc.version === undefined) {
|
||||
doc.version = DOCUMENT_VERSION;
|
||||
}
|
||||
return beforeSave(doc);
|
||||
};
|
||||
|
||||
@@ -99,6 +100,11 @@ const Document = sequelize.define(
|
||||
version: DataTypes.SMALLINT,
|
||||
editorVersion: DataTypes.STRING,
|
||||
text: DataTypes.TEXT,
|
||||
|
||||
// backup contains a record of text at the moment it was converted to v2
|
||||
// this is a safety measure during deployment of new editor and will be
|
||||
// dropped in a future update
|
||||
backup: DataTypes.TEXT,
|
||||
isWelcome: { type: DataTypes.BOOLEAN, defaultValue: false },
|
||||
revisionCount: { type: DataTypes.INTEGER, defaultValue: 0 },
|
||||
archivedAt: DataTypes.DATE,
|
||||
@@ -453,11 +459,26 @@ Document.prototype.toMarkdown = function() {
|
||||
};
|
||||
|
||||
Document.prototype.migrateVersion = function() {
|
||||
// migrate from document version 0 -> 1 means removing the title from the
|
||||
// document text attribute.
|
||||
let migrated = false;
|
||||
|
||||
// migrate from document version 0 -> 1
|
||||
if (!this.version) {
|
||||
// removing the title from the document text attribute
|
||||
this.text = this.text.replace(/^#\s(.*)\n/, '');
|
||||
this.version = 1;
|
||||
migrated = true;
|
||||
}
|
||||
|
||||
// migrate from document version 1 -> 2
|
||||
if (this.version === 1) {
|
||||
const nodes = serializer.deserialize(this.text);
|
||||
this.backup = this.text;
|
||||
this.text = serializer.serialize(nodes, { version: 2 });
|
||||
this.version = 2;
|
||||
migrated = true;
|
||||
}
|
||||
|
||||
if (migrated) {
|
||||
return this.save({ silent: true, hooks: false });
|
||||
}
|
||||
};
|
||||
@@ -588,10 +609,17 @@ Document.prototype.getTimestamp = function() {
|
||||
};
|
||||
|
||||
Document.prototype.getSummary = function() {
|
||||
const value = Markdown.deserialize(this.text);
|
||||
const plain = Plain.serialize(value);
|
||||
const plain = removeMarkdown(unescape(this.text), {
|
||||
stripHTML: false,
|
||||
});
|
||||
const lines = compact(plain.split('\n'));
|
||||
return lines.length >= 1 ? lines[1] : '';
|
||||
const notEmpty = lines.length >= 1;
|
||||
|
||||
if (this.version) {
|
||||
return notEmpty ? lines[0] : '';
|
||||
}
|
||||
|
||||
return notEmpty ? lines[1] : '';
|
||||
};
|
||||
|
||||
Document.prototype.toJSON = function() {
|
||||
|
||||
Reference in New Issue
Block a user