Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: migrate from tiny -> tiptap

thecodrr3f54f36d50a0
diff --git a/packages/core/__tests__/backup.test.js b/packages/core/__tests__/backup.test.js
index 13ec34ea5..0816c825c 100644
--- a/packages/core/__tests__/backup.test.js
+++ b/packages/core/__tests__/backup.test.js
@@ -104,7 +104,7 @@ describe.each([
const allContent = await db.content.all();
expect(
- allContent.every((v) => v.type === "tiny" || v.deleted)
+ allContent.every((v) => v.type === "tiptap" || v.deleted)
).toBeTruthy();
expect(allContent.every((v) => !v.persistDateEdited)).toBeTruthy();
expect(allContent.every((v) => v.dateModified > 0)).toBeTruthy();
diff --git a/packages/core/__tests__/lookup.test.js b/packages/core/__tests__/lookup.test.js
index 26e2441d1..ced10147e 100644
--- a/packages/core/__tests__/lookup.test.js
+++ b/packages/core/__tests__/lookup.test.js
@@ -42,7 +42,7 @@ test("search notes with an empty note", () =>
}).then(async ({ db }) => {
await db.notes.add({
title: "world is a heavy tune",
- content: { type: "tiny", data: "<p><br></p>" },
+ content: { type: "tiptap", data: "<p><br></p>" },
});
let filtered = await db.lookup.notes(db.notes.all, "heavy tune");
expect(filtered).toHaveLength(1);
diff --git a/packages/core/__tests__/note-history.test.js b/packages/core/__tests__/note-history.test.js
index 091dcc4b7..2e08d5e6b 100644
--- a/packages/core/__tests__/note-history.test.js
+++ b/packages/core/__tests__/note-history.test.js
@@ -8,7 +8,7 @@ beforeEach(async () => {
// let note = await db.notes.note(noteId).data;
// let content = {
// data: await db.notes.note(noteId).content(),
-// type: "tiny",
+// type: "tiptap",
// };
// let session = await db.noteHistory.add(noteId, note.dateEdited, content);
@@ -28,7 +28,7 @@ test("editing the same note should create multiple history sessions", () =>
noteTest({ ...TEST_NOTE, sessionId: Date.now() }).then(async ({ db, id }) => {
let editedContent = {
data: TEST_NOTE.content.data + "<p>Some new content</p>",
- type: "tiny",
+ type: "tiptap",
};
await db.notes.add({
@@ -52,7 +52,7 @@ test("restoring an old session should replace note's content", () =>
noteTest({ ...TEST_NOTE, sessionId: Date.now() }).then(async ({ db, id }) => {
let editedContent = {
data: TEST_NOTE.content.data + "<p>Some new content</p>",
- type: "tiny",
+ type: "tiptap",
};
await db.notes.add({
@@ -75,7 +75,7 @@ test("date created of session should not change on edit", () =>
let editedContent = {
data: TEST_NOTE.content.data + "<p>Some new content</p>",
- type: "tiny",
+ type: "tiptap",
};
await delay(1000);
@@ -141,7 +141,7 @@ test("auto clear sessions if they exceed the limit", () =>
noteTest({ ...TEST_NOTE, sessionId: Date.now() }).then(async ({ db, id }) => {
let editedContent = {
data: TEST_NOTE.content.data + "<p>Some new content</p>",
- type: "tiny",
+ type: "tiptap",
};
await db.notes.add({
@@ -168,7 +168,7 @@ test("save a locked note should add a locked session to note history", () =>
await db.vault.add(id);
const note = db.notes.note(id).data;
- const editedContent = { type: "tiny", data: "<p>hello world</p>" };
+ const editedContent = { type: "tiptap", data: "<p>hello world</p>" };
await db.vault.save({
...note,
content: editedContent,
diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.js
index 527a27c22..c5d84dcc1 100644
--- a/packages/core/__tests__/notes.test.js
+++ b/packages/core/__tests__/notes.test.js
@@ -318,7 +318,7 @@ test("note content should not contain image base64 data after save", () =>
id
);
- await db.notes.add({ id, content: { type: "tiny", data: IMG_CONTENT } });
+ await db.notes.add({ id, content: { type: "tiptap", data: IMG_CONTENT } });
const note = db.notes.note(id);
const content = await note.content();
expect(content).not.toContain(`src="data:image/png;`);
diff --git a/packages/core/__tests__/utils/index.js b/packages/core/__tests__/utils/index.js
index 3681d5b70..47bfd00d8 100644
--- a/packages/core/__tests__/utils/index.js
+++ b/packages/core/__tests__/utils/index.js
@@ -29,7 +29,7 @@ const notebookTest = (notebook = TEST_NOTEBOOK) =>
var TEST_NOTE = {
content: {
- type: "tiny",
+ type: "tiptap",
data: `<p>Hello<br><span style="color:#f00">This is colorful</span></p>`,
},
};
diff --git a/packages/core/__tests__/vault.test.js b/packages/core/__tests__/vault.test.js
index fec287d11..4a79dfe4c 100644
--- a/packages/core/__tests__/vault.test.js
+++ b/packages/core/__tests__/vault.test.js
@@ -108,7 +108,7 @@ test("save an edited locked note", () =>
const note = db.notes.note(id).data;
await db.vault.save({
...note,
- content: { type: "tiny", data: "<p>hello world</p>" },
+ content: { type: "tiptap", data: "<p>hello world</p>" },
});
const content = await db.content.raw(note.contentId);
diff --git a/packages/core/api/migrations.js b/packages/core/api/migrations.js
index a1a9d30d1..70b3d14c8 100644
--- a/packages/core/api/migrations.js
+++ b/packages/core/api/migrations.js
@@ -1,4 +1,4 @@
-import { CURRENT_DATABASE_VERSION, EV, EVENTS } from "../common";
+import { CURRENT_DATABASE_VERSION, EVENTS } from "../common";
import Migrator from "../database/migrator";
class Migrations {
@@ -9,6 +9,7 @@ class Migrations {
constructor(db) {
this._db = db;
this._migrator = new Migrator();
+ this._isMigrating = false;
}
async init() {
@@ -18,7 +19,13 @@ class Migrations {
}
async migrate() {
- if (this.dbVersion >= CURRENT_DATABASE_VERSION) return;
+ if (this.dbVersion >= CURRENT_DATABASE_VERSION || this._isMigrating) return;
+
+ this._isMigrating = true;
+ this._db.eventManager.publish(EVENTS.databaseMigrating, {
+ from: this.dbVersion,
+ to: CURRENT_DATABASE_VERSION,
+ });
await this._db.notes.init();
const content = await this._db.content.all();
@@ -58,10 +65,7 @@ class Migrations {
await this._migrator.migrate(collections, (item) => item, this.dbVersion);
await this._db.storage.write("v", CURRENT_DATABASE_VERSION);
- EV.publish(EVENTS.databaseMigrated, {
- prev: this.dbVersion,
- current: CURRENT_DATABASE_VERSION,
- });
+ this._db.eventManager.publish(EVENTS.databaseMigrated);
this.dbVersion = CURRENT_DATABASE_VERSION;
}
diff --git a/packages/core/api/sync/__tests__/sync.test.js b/packages/core/api/sync/__tests__/sync.test.js
index 45cf1bd84..642e2b338 100644
--- a/packages/core/api/sync/__tests__/sync.test.js
+++ b/packages/core/api/sync/__tests__/sync.test.js
@@ -122,7 +122,7 @@ test.skip(
for (let i = 0; i < 10; ++i) {
const id = await deviceA.notes.add({
content: {
- type: "tiny",
+ type: "tiptap",
data: `<p>deviceA=true</p>`,
},
});
@@ -137,7 +137,7 @@ test.skip(
const noteId = await deviceA.notes.add({
id,
content: {
- type: "tiny",
+ type: "tiptap",
data: `<p>deviceA=true+changed=true</p>`,
},
});
@@ -162,7 +162,7 @@ test.skip(
await deviceB.notes.add({
id,
content: {
- type: "tiny",
+ type: "tiptap",
data: "<p>changes from device B</p>",
},
});
@@ -211,7 +211,7 @@ test.skip(
for (let i = 0; i < 3; ++i) {
const id = await deviceA.notes.add({
content: {
- type: "tiny",
+ type: "tiptap",
data: `<p>deviceA=true</p>`,
},
});
diff --git a/packages/core/api/sync/merger.js b/packages/core/api/sync/merger.js
index 4c6e4b33d..bda75b465 100644
--- a/packages/core/api/sync/merger.js
+++ b/packages/core/api/sync/merger.js
@@ -99,7 +99,7 @@ class Merger {
if (deserialized.alg && deserialized.cipher) return deserialized;
let type = deserialized.type;
- if (!type && deserialized.data) type = "tiny";
+ if (!type && deserialized.data) type = "tiptap";
if (!migrations[version]) {
throw new Error(
@@ -110,7 +110,10 @@ class Merger {
}
const migrate = migrations[version][type];
- if (migrate) return migrate(deserialized);
+ if (migrate) {
+ console.log(migrate, version, type);
+ return migrate(deserialized);
+ }
return deserialized;
}
diff --git a/packages/core/api/vault.js b/packages/core/api/vault.js
index b6d83cf7a..af2fb4693 100644
--- a/packages/core/api/vault.js
+++ b/packages/core/api/vault.js
@@ -1,4 +1,5 @@
import { CHECK_IDS, EV, EVENTS, checkIsUserPremium } from "../common";
+import { tinyToTiptap } from "../migrations";
const ERASE_TIME = 1000 * 60 * 30;
var ERASER_TIMEOUT = null;
@@ -228,7 +229,18 @@ export default class Vault {
encryptedContent.data
);
- return { type: encryptedContent.type, data: JSON.parse(decryptedContent) };
+ const content = {
+ type: encryptedContent.type,
+ data: JSON.parse(decryptedContent),
+ };
+
+ // #MIGRATION: convert tiny to tiptap
+ if (content.type === "tiny") {
+ content.type = "tiptap";
+ content.data = tinyToTiptap(content.data);
+ }
+
+ return content;
}
/** @private */
diff --git a/packages/core/common.js b/packages/core/common.js
index 5d82fb523..ef3302c26 100644
--- a/packages/core/common.js
+++ b/packages/core/common.js
@@ -52,6 +52,7 @@ export const EVENTS = {
syncProgress: "sync:progress",
syncCompleted: "sync:completed",
databaseMigrated: "db:migrated",
+ databaseMigrating: "db:migrating",
databaseUpdated: "db:updated",
databaseCollectionInitiated: "db:collectionInitiated",
appRefreshRequested: "app:refreshRequested",
@@ -65,7 +66,7 @@ export const EVENTS = {
systemTimeInvalid: "system:invalidTime",
};
-export const CURRENT_DATABASE_VERSION = 5.4;
+export const CURRENT_DATABASE_VERSION = 5.5;
export function setUserPersonalizationBytes(userSalt) {
USER_PERSONALIZATION_HASH = new Uint8Array(
diff --git a/packages/core/content-types/__tests__/tiny.test.js b/packages/core/content-types/__tests__/tiptap.test.js
similarity index 96%
rename from packages/core/content-types/__tests__/tiny.test.js
rename to packages/core/content-types/__tests__/tiptap.test.js
index 02f9046d8..7f65370a8 100644
--- a/packages/core/content-types/__tests__/tiny.test.js
+++ b/packages/core/content-types/__tests__/tiptap.test.js
@@ -1,9 +1,9 @@
import { IMG_CONTENT, IMG_CONTENT_WITHOUT_HASH } from "../../__tests__/utils";
-import Tiny from "../tiny";
+import { Tiptap } from "../tiptap";
test("img src is empty after extract attachments", async () => {
- const tiny = new Tiny(IMG_CONTENT_WITHOUT_HASH);
- const result = await tiny.extractAttachments(async () => {
+ const tiptap = new Tiptap(IMG_CONTENT_WITHOUT_HASH);
+ const result = await tiptap.extractAttachments(async () => {
return { key: "hello", metadata: { hash: "helloworld" } };
});
expect(result.attachments).toHaveLength(1);
@@ -13,25 +13,25 @@ test("img src is empty after extract attachments", async () => {
});
test("img src is present after insert attachments", async () => {
- const tiny = new Tiny(IMG_CONTENT);
- const result = await tiny.extractAttachments(async () => {
+ const tiptap = new Tiptap(IMG_CONTENT);
+ const result = await tiptap.extractAttachments(async () => {
return { key: "hello", metadata: {} };
});
- const tiny2 = new Tiny(result.data);
- const result2 = await tiny2.insertMedia(() => "i am a data");
+ const tiptap2 = new Tiptap(result.data);
+ const result2 = await tiptap2.insertMedia(() => "i am a data");
expect(result2).toContain(`src="i am a data"`);
});
test("convert HTML to markdown with tables", async () => {
- const tiny = new Tiny(
+ const tiptap = new Tiptap(
`<div><div class="table-container" contenteditable="false"><table style="border-collapse: collapse; width: 811px;" contenteditable="true" border="1" data-mce-selected="1"><tbody><tr data-mce-active="1"><td style="width: 81.375px;" data-mce-active="1">Goal</td><td style="width: 708.625px;">To introduce various features of the app to the user and to convert a user on trial or basic plan to upgrade.</td></tr><tr><td style="width: 81.375px;">Frequency</td><td style="width: 708.625px;">1/week or 2/week</td></tr><tr><td style="width: 81.375px;">Types</td><td style="width: 708.625px;">Feature intro, upgrade promo, one time emails</td></tr><tr><td style="width: 81.375px;"><br></td><td style="width: 708.625px;"><br></td></tr></tbody></table></div><h2>Emails</h2><h3>Feature intro</h3><p>Features:</p><ol style="list-style-type: decimal;"><li>Web clipper on mobile</li><li>Pin any note to notification</li><li>Take notes from notifications</li><li>App lock</li><li>Importer</li><li>Encrypted attachments</li><li>Session history &amp; automatic backups</li><li>Note publishing</li><li>Note exports<br data-mce-bogus="1"></li><li>Collapsible headers</li></ol><h3>Promos</h3><ol style="list-style-type: decimal;"><li>Trial about to end</li><li>Trial ending (with option to request an extension)</li><li>Try free for 14 days<br data-mce-bogus="1"></li></ol><h3>One time</h3><ol style="list-style-type: decimal;"><li>End-of-month progress report</li><li>What's coming/roadmap</li><li>What we are working on</li><li>Join the community<br data-mce-bogus="1"></li></ol><div id="mceResizeHandlenw" data-mce-bogus="all" class="mce-resizehandle" unselectable="true" data-mce-style="cursor: nw-resize; margin: 0px; padding: 0px; left: -6.5px; top: -6.5px;" style="cursor: nw-resize; margin: 0px; padding: 0px; left: -6.5px; top: -6.5px;"></div><div id="mceResizeHandlene" data-mce-bogus="all" class="mce-resizehandle" unselectable="true" data-mce-style="cursor: ne-resize; margin: 0px; padding: 0px; left: 856.5px; top: -6.5px;" style="cursor: ne-resize; margin: 0px; padding: 0px; left: 856.5px; top: -6.5px;"></div><div id="mceResizeHandlese" data-mce-bogus="all" class="mce-resizehandle" unselectable="true" data-mce-style="cursor: se-resize; margin: 0px; padding: 0px; left: 856.5px; top: 117.9px;" style="cursor: se-resize; margin: 0px; padding: 0px; left: 856.5px; top: 117.9px;"></div><div id="mceResizeHandlesw" data-mce-bogus="all" class="mce-resizehandle" unselectable="true" data-mce-style="cursor: sw-resize; margin: 0px; padding: 0px; left: -6.5px; top: 117.9px;" style="cursor: sw-resize; margin: 0px; padding: 0px; left: -6.5px; top: 117.9px;"></div></div>`
);
- expect(tiny.toMD()).toMatchSnapshot("html-to-md-tables.md");
+ expect(tiptap.toMD()).toMatchSnapshot("html-to-md-tables.md");
});
test("convert HTML to markdown with code blocks", async () => {
- const tiny = new Tiny(
+ const tiptap = new Tiptap(
`<div id="a94ae3f0aa12ec57d06ed845" class="mce-content-body mce-edit-focus" contenteditable="true" style="position: relative;"><p>Typescript is one of those languages that appear to be very simple. It's often described as "Javascript with types" and it fits that name very well. However, what many don't realize starting out with Typescript is that Typescript&nbsp;<em>is </em>a language and like all other languages it has it's own "secrets", it's own set of quirks.</p><p>When I started out with Typescript a few years back, I absolutely hated it. It was unnecessary, a box of clutter, making me write code that would never actually run. I hated defining interfaces, typing out all my functions, and thinking in terms I was not used to as a Javascript developer. Before Javascript, I had coded in C# and I had never really liked C# (I still don't) mostly for the huge amounts of boilerplate and magic involved. Typescript is heavily inspired by C# and seeing that contaminate the Javascript ecosystem irked me no end.</p><p>That was all just fluff. Typescript is a great language solving a considerable amount of problems for a huge amount of developers. And generics is the hidden weapon behind it all.</p><h2 class="">Understanding Typescript generics</h2><p class="">For all points and purposes, generics shouldn't exist. They are one of the main factors behind abysmal code readability. But in the right hands, generics turn into a super-weapon.</p><p class="">The main problem generics try to solve is how to take in multiple types of parameters. This is a solved problem but requires duplicating your functions or using conditions to separate out different logic for different types. That is essentially what generics do as well but hidden from human eyes.</p><p class="">Think of a container that can take any type of item as long as it is not a circle. In Javascript, this becomes a problem you have to solve at runtime with checks &amp; conditions:</p><pre class="hljs language-javascript" spellcheck="false"><span class="hljs-keyword">var</span> container = [];<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">putIntoContainer</span>(<span class="hljs-params">item</span>) {<br>&nbsp; &nbsp; <span class="hljs-keyword">if</span> (item.<span class="hljs-property">type</span> === <span class="hljs-string">"round"</span>) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">"Rounded items not supported."</span>)<br>&nbsp; &nbsp; container.<span class="hljs-title function_">push</span>(item);<br>}<br><br><span class="hljs-keyword">var</span> square = {<span class="hljs-attr">type</span>: <span class="hljs-string">"square"</span>}<br><span class="hljs-keyword">var</span> circle = {<span class="hljs-attr">type</span>: <span class="hljs-string">"round"</span>}<br><span class="hljs-title function_">putIntoContainer</span>(square)<br><span class="hljs-title function_">putIntoContainer</span>(circle) <span class="hljs-comment">// ERROR! Rounded items not supported!</span><br></pre><p class="">There are many ways to solve this problem and some are even practical. The issue here isn't of repetition but of doing unnecessary work. Type safe languages would automatically give an error if there was a wrong type but Javascript knows nothing about the <code spellcheck="false">item</code>.</p><p class="">In Typescript, this will be solved much more succinctly:</p><pre class="hljs language-typescript" spellcheck="false"><span class="hljs-comment">// first define the types of items we'll handle</span><br><span class="hljs-comment">// i.e. we don't want to handle any item other</span><br><span class="hljs-comment">// than square or round.</span><br><span class="hljs-comment">// This gives us nice auto completion and safety</span><br><span class="hljs-comment">// against typos.</span><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">ItemTypes</span> = <span class="hljs-string">"square"</span> | <span class="hljs-string">"round"</span>;<br><br><span class="hljs-comment">// Define a generic item that can be of any type</span><br><span class="hljs-comment">// defined in ItemTypes.</span><br><span class="hljs-comment">// i.e. Item&lt;"triangle"&gt; will give an error.</span><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">Item</span>&lt;TItemType <span class="hljs-keyword">extends</span> <span class="hljs-title class_">ItemTypes</span>&gt; = {<br>&nbsp;<span class="hljs-keyword">type</span>?: TItemType;<br>&nbsp;<span class="hljs-attr">width</span>: <span class="hljs-built_in">number</span>;<br>&nbsp;<span class="hljs-attr">height</span>: <span class="hljs-built_in">number</span>;<br>};<br><br><span class="hljs-comment">// This is just syntax sugar to increase readability.</span><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">Square</span> = <span class="hljs-title class_">Item</span>&lt;<span class="hljs-string">"square"</span>&gt;;<br><span class="hljs-keyword">type</span> <span class="hljs-title class_">Circle</span> = <span class="hljs-title class_">Item</span>&lt;<span class="hljs-string">"round"</span>&gt;;<br><br><span class="hljs-comment">// Our container is just a simple wrapper around an array</span><br><span class="hljs-comment">// that accepts items of only a specific type.</span><br><span class="hljs-keyword">type</span> <span class="hljs-title class_">Container</span>&lt;TItemType <span class="hljs-keyword">extends</span> <span class="hljs-title class_">ItemTypes</span>&gt; = <span class="hljs-title class_">Array</span>&lt;<span class="hljs-title class_">Item</span>&lt;TItemType&gt;&gt;;<br><br><span class="hljs-keyword">var</span> <span class="hljs-attr">squareContainer</span>: <span class="hljs-title class_">Container</span>&lt;<span class="hljs-string">"square"</span>&gt; = [];<br><span class="hljs-keyword">var</span> <span class="hljs-attr">roundContainer</span>: <span class="hljs-title class_">Container</span>&lt;<span class="hljs-string">"round"</span>&gt; = [];<br><br><span class="hljs-comment">// This wrapper is unnecessary, of course, because array.push</span><br><span class="hljs-comment">// already does this. Only for demonstration purposes.</span><br><span class="hljs-keyword">function</span> putIntoContainer&lt;<br>&nbsp;TItemType <span class="hljs-keyword">extends</span> <span class="hljs-title class_">ItemTypes</span>,<br>&nbsp;TItem <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Item</span>&lt;TItemType&gt;<br>&gt;(<span class="hljs-attr">container</span>: <span class="hljs-title class_">Container</span>&lt;TItemType&gt;, <span class="hljs-attr">item</span>: TItem) {<br>&nbsp;container.<span class="hljs-title function_">push</span>(item);<br>}<br><br><span class="hljs-keyword">var</span> <span class="hljs-attr">square</span>: <span class="hljs-title class_">Square</span> = { <span class="hljs-attr">width</span>: <span class="hljs-number">100</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">200</span> };<br><span class="hljs-keyword">var</span> <span class="hljs-attr">circle</span>: <span class="hljs-title class_">Circle</span> = { <span class="hljs-attr">width</span>: <span class="hljs-number">200</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">500</span> };<br><br><span class="hljs-title function_">putIntoContainer</span>(squareContainer, square);<br><span class="hljs-title function_">putIntoContainer</span>(roundContainer, circle);<br><span class="hljs-title function_">putIntoContainer</span>(roundContainer, square); <span class="hljs-comment">// Error: Argument of type 'Square' is not assignable to parameter of type 'Item&lt;"round"&gt;'.</span><br><span class="hljs-title function_">putIntoContainer</span>(squareContainer, circle); <span class="hljs-comment">// Error: Argument of type 'Circle' is not assignable to parameter of type 'Item&lt;"square"&gt;'.</span><br></pre><p class="">A lot more code, I know, and if you don't know how generics work that blob of code is utter nonsense. One of the main reasons I avoided Typescript for a long time. But look at the benefits:</p><ol style="list-style-type: decimal;" class=""><li>You have 100% compile-time type safety.</li><li>You can't put a round item in a square container (you will get compiler error).<br></li><li>You didn't write any extra runtime code.</li></ol><p class="">Expanding on point #3, after transpilation the above code will turn more-or-less into:</p><pre class="hljs language-javascript" spellcheck="false"><span class="hljs-keyword">var</span> squareContainer = [];<br><span class="hljs-keyword">var</span> roundContainer = [];<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">putIntoContainer</span>(<span class="hljs-params">container, item</span>) {<br>&nbsp;container.<span class="hljs-title function_">push</span>(item);<br>}<br><br><span class="hljs-keyword">var</span> square = { <span class="hljs-attr">width</span>: <span class="hljs-number">100</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">200</span> };<br><span class="hljs-keyword">var</span> circle = { <span class="hljs-attr">width</span>: <span class="hljs-number">200</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">500</span> };<br><br><span class="hljs-title function_">putIntoContainer</span>(squareContainer, square);<br><span class="hljs-title function_">putIntoContainer</span>(roundContainer, circle);<br></pre><p class="">This is the power of generics. More specifically, this is Typescript generics at a glance.</p><p class="">But this post was supposed to be about the "Secrets" of Typescript Generics, right? Well, let's get into that.</p><h2 class="">1. Type filters using ternary operators</h2><h2>2. Deeply recursive types</h2><h2>3. Type functions</h2><h2>4. Type inference using interface properties</h2><p><br></p></div>`
);
- expect(tiny.toMD()).toMatchSnapshot("html-to-md-codeblock.md");
+ expect(tiptap.toMD()).toMatchSnapshot("html-to-md-codeblock.md");
});
diff --git a/packages/core/content-types/index.js b/packages/core/content-types/index.js
index 3d8441400..2f6f4f1e4 100644
--- a/packages/core/content-types/index.js
+++ b/packages/core/content-types/index.js
@@ -1,9 +1,9 @@
-import Tiny from "./tiny";
+import { Tiptap } from "./tiptap";
export function getContentFromData(type, data) {
switch (type) {
- case "tiny":
- return new Tiny(data);
+ case "tiptap":
+ return new Tiptap(data);
default:
return null;
}
diff --git a/packages/core/content-types/tiny.js b/packages/core/content-types/tiptap.js
similarity index 99%
rename from packages/core/content-types/tiny.js
rename to packages/core/content-types/tiptap.js
index ee661dd6f..a52e97be5 100644
--- a/packages/core/content-types/tiny.js
+++ b/packages/core/content-types/tiptap.js
@@ -6,7 +6,7 @@ var converter = new showdown.Converter();
converter.setFlavor("original");
const splitter = /\W+/gm;
-class Tiny {
+export class Tiptap {
constructor(data) {
this.data = data;
this.text;
@@ -144,7 +144,6 @@ class Tiny {
};
}
}
-export default Tiny;
function getDatasetAttribute(element, attribute) {
return element.getAttribute(`data-${attribute}`);
diff --git a/packages/core/database/backup.js b/packages/core/database/backup.js
index e7694f843..9936a5325 100644
--- a/packages/core/database/backup.js
+++ b/packages/core/database/backup.js
@@ -109,6 +109,7 @@ export default class Backup {
switch (version) {
case CURRENT_DATABASE_VERSION:
+ case 5.4:
case 5.3:
case 5.2:
case 5.1:
diff --git a/packages/core/migrations.js b/packages/core/migrations.js
index 4f9014b9f..5a829a259 100644
--- a/packages/core/migrations.js
+++ b/packages/core/migrations.js
@@ -13,27 +13,36 @@ export const migrations = {
tiny: (item) => {
item = replaceDateEditedWithDateModified()(item);
- if (!item.data || item.data.iv) return item;
+ if (!item.data || item.data.iv) return migrations["5.3"].tiny(item);
item.data = removeToxClassFromChecklist(wrapTablesWithDiv(item.data));
- return item;
+ return migrations["5.3"].tiny(item);
},
settings: replaceDateEditedWithDateModified(true),
},
5.3: {
tiny: (item) => {
- if (!item.data || item.data.iv) return item;
+ if (!item.data || item.data.iv) return migrations["5.4"].tiny(item);
item.data = decodeWrappedTableHtml(item.data);
- return item;
+ return migrations["5.4"].tiny(item);
},
},
5.4: {
+ tiny: (item) => {
+ if (!item.data || item.data.iv) return item;
+ item.type = "tiptap";
+ item.data = tinyToTiptap(item.data);
+ return item;
+ },
+ },
+ 5.5: {
note: false,
notebook: false,
tag: false,
attachment: false,
trash: false,
tiny: false,
+ tiptap: false,
settings: false,
},
};
@@ -83,3 +92,54 @@ function decodeWrappedTableHtml(html) {
return html;
});
}
+
+export function tinyToTiptap(html) {
+ const document = parseHTML(html);
+
+ const tables = document.querySelectorAll("table");
+ for (const table of tables) {
+ table.removeAttribute("contenteditable");
+ if (table.parentElement?.nodeName.toLowerCase() === "div") {
+ table.parentElement.replaceWith(table);
+ }
+ }
+
+ const images = document.querySelectorAll("p > img");
+ for (const image of images) {
+ image.parentElement.replaceWith(image.cloneNode());
+ }
+
+ const breaks = document.querySelectorAll("br");
+ for (const br of breaks) {
+ br.remove();
+ }
+
+ const paragraphs = document.querySelectorAll("p");
+ for (const p of paragraphs) {
+ if (!p.childNodes.length) p.remove();
+ }
+
+ const listItems = document.querySelectorAll("li");
+ for (const li of listItems) {
+ if (li.firstChild) {
+ const p = document.createElement("p");
+ p.appendChild(li.firstChild.cloneNode());
+ li.firstChild.replaceWith(p);
+ }
+ }
+
+ const bogus = document.querySelectorAll("[data-mce-bogus]");
+ for (const element of bogus) {
+ element.remove();
+ }
+
+ const attributes = document.querySelectorAll(
+ "[data-mce-href], [data-mce-flag]"
+ );
+ for (const element of attributes) {
+ element.removeAttribute("data-mce-href");
+ element.removeAttribute("data-mce-flag");
+ }
+
+ return document.body.innerHTML;
+}