From 3309b3d6430a296bd8045ed30b501128ff9448ce Mon Sep 17 00:00:00 2001 From: thecodrr Date: Thu, 16 Dec 2021 10:10:00 +0500 Subject: [PATCH] feat: simplify note title & headline extraction --- packages/core/__tests__/notes.test.js | 8 ++++---- packages/core/collections/notes.js | 22 ++++++++++++++++------ packages/core/content-types/tiny.js | 17 +++++------------ packages/core/utils/html-parser.js | 4 ++++ 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.js index eac9d967e..2ccb3928e 100644 --- a/packages/core/__tests__/notes.test.js +++ b/packages/core/__tests__/notes.test.js @@ -60,10 +60,10 @@ test("get all notes", () => expect(db.notes.all.length).toBeGreaterThan(0); })); -test("note without a title should get title from content", () => +test("note without a title should get a premade title", () => noteTest().then(async ({ db, id }) => { let note = db.notes.note(id); - expect(note.title).toBe("HelloThis is colorful"); + expect(note.title.startsWith("Note ")).toBe(true); })); test("note should get headline from content", () => @@ -78,7 +78,7 @@ test("note should get headline from content", () => expect(note.headline).toBe("This is a very colorful existence."); })); -test("note should get headline from content containing only lists", () => +test("note should not get headline if there is no p tag", () => noteTest({ ...TEST_NOTE, content: { @@ -87,7 +87,7 @@ test("note should get headline from content containing only lists", () => }, }).then(async ({ db, id }) => { let note = db.notes.note(id); - expect(note.headline).toBe("Hello I won't be a headline :(Me too."); + expect(note.headline).toBeUndefined(); })); test("note title should allow trailing space", () => diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js index 6ac49290b..e3c3242c1 100644 --- a/packages/core/collections/notes.js +++ b/packages/core/collections/notes.js @@ -72,7 +72,6 @@ export default class Notes extends Collection { let content = getContentFromData(type, data); if (!content) throw new Error("Invalid content type."); - note.title = getNoteTitle(note, content); note.headline = getNoteHeadline(note, content); note.contentId = await this._db.content.add({ @@ -92,7 +91,7 @@ export default class Notes extends Collection { id, contentId: note.contentId, type: "note", - title: note.title, + title: getNoteTitle(note, oldNote).replace(/[\r\n\t]+/g, " "), headline: note.headline, pinned: !!note.pinned, locked: !!note.locked, @@ -274,8 +273,19 @@ function getNoteHeadline(note, content) { return content.toHeadline(); } -function getNoteTitle(note, content) { - if (note.title && note.title.trim().length > 0) - return note.title.replace(/\r?\n/g, " "); - return content.toTitle(); +function getNoteTitle(note, oldNote) { + if (note.title && note.title.trim().length > 0) return note.title; + else if (oldNote && oldNote.title && oldNote.title.trim().length > 0) { + return oldNote.title; + } + + return `Note ${new Date().toLocaleString(undefined, { + year: "2-digit", + month: "2-digit", + day: "2-digit", + hour12: true, + hour: "2-digit", + minute: "2-digit", + second: "2-digit", + })}`; } diff --git a/packages/core/content-types/tiny.js b/packages/core/content-types/tiny.js index 1f9fe6bc5..d1870951f 100644 --- a/packages/core/content-types/tiny.js +++ b/packages/core/content-types/tiny.js @@ -1,6 +1,6 @@ import showdown from "showdown/dist/showdown"; import dataurl from "../utils/dataurl"; -import { getDummyDocument, parseHTML } from "../utils/html-parser"; +import { getDummyDocument, getInnerText, parseHTML } from "../utils/html-parser"; var converter = new showdown.Converter(); converter.setFlavor("original"); @@ -27,18 +27,11 @@ class Tiny { return converter.makeMarkdown(this.data, getDummyDocument()); } - toTitle() { - if (!this.text) { - this.text = this.toTXT(); - } - return getTitleFromText(this.text); - } - toHeadline() { - if (!this.text) { - this.text = this.toTXT(); - } - return getHeadlineFromText(this.text); + const paragraph = this.document.querySelector("p"); + if (!paragraph) return; + + return getInnerText(paragraph); } isEmpty() { diff --git a/packages/core/utils/html-parser.js b/packages/core/utils/html-parser.js index c449fa9f0..574c4ddb0 100644 --- a/packages/core/utils/html-parser.js +++ b/packages/core/utils/html-parser.js @@ -9,3 +9,7 @@ export function getDummyDocument() { const doc = parseHTML("
"); return typeof DOMParser === "undefined" ? doc : doc; } + +export function getInnerText(element) { + return element.innerText || element.textContent; +}