feat: simplify note title & headline extraction

This commit is contained in:
thecodrr
2021-12-16 10:10:00 +05:00
parent ad83938fa4
commit 3309b3d643
4 changed files with 29 additions and 22 deletions

View File

@@ -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", () =>

View File

@@ -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",
})}`;
}

View File

@@ -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() {

View File

@@ -9,3 +9,7 @@ export function getDummyDocument() {
const doc = parseHTML("<div></div>");
return typeof DOMParser === "undefined" ? doc : doc;
}
export function getInnerText(element) {
return element.innerText || element.textContent;
}