mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
feat: simplify note title & headline extraction
This commit is contained in:
@@ -60,10 +60,10 @@ test("get all notes", () =>
|
|||||||
expect(db.notes.all.length).toBeGreaterThan(0);
|
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 }) => {
|
noteTest().then(async ({ db, id }) => {
|
||||||
let note = db.notes.note(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", () =>
|
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.");
|
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({
|
noteTest({
|
||||||
...TEST_NOTE,
|
...TEST_NOTE,
|
||||||
content: {
|
content: {
|
||||||
@@ -87,7 +87,7 @@ test("note should get headline from content containing only lists", () =>
|
|||||||
},
|
},
|
||||||
}).then(async ({ db, id }) => {
|
}).then(async ({ db, id }) => {
|
||||||
let note = db.notes.note(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", () =>
|
test("note title should allow trailing space", () =>
|
||||||
|
|||||||
@@ -72,7 +72,6 @@ export default class Notes extends Collection {
|
|||||||
|
|
||||||
let content = getContentFromData(type, data);
|
let content = getContentFromData(type, data);
|
||||||
if (!content) throw new Error("Invalid content type.");
|
if (!content) throw new Error("Invalid content type.");
|
||||||
note.title = getNoteTitle(note, content);
|
|
||||||
note.headline = getNoteHeadline(note, content);
|
note.headline = getNoteHeadline(note, content);
|
||||||
|
|
||||||
note.contentId = await this._db.content.add({
|
note.contentId = await this._db.content.add({
|
||||||
@@ -92,7 +91,7 @@ export default class Notes extends Collection {
|
|||||||
id,
|
id,
|
||||||
contentId: note.contentId,
|
contentId: note.contentId,
|
||||||
type: "note",
|
type: "note",
|
||||||
title: note.title,
|
title: getNoteTitle(note, oldNote).replace(/[\r\n\t]+/g, " "),
|
||||||
headline: note.headline,
|
headline: note.headline,
|
||||||
pinned: !!note.pinned,
|
pinned: !!note.pinned,
|
||||||
locked: !!note.locked,
|
locked: !!note.locked,
|
||||||
@@ -274,8 +273,19 @@ function getNoteHeadline(note, content) {
|
|||||||
return content.toHeadline();
|
return content.toHeadline();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNoteTitle(note, content) {
|
function getNoteTitle(note, oldNote) {
|
||||||
if (note.title && note.title.trim().length > 0)
|
if (note.title && note.title.trim().length > 0) return note.title;
|
||||||
return note.title.replace(/\r?\n/g, " ");
|
else if (oldNote && oldNote.title && oldNote.title.trim().length > 0) {
|
||||||
return content.toTitle();
|
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",
|
||||||
|
})}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import showdown from "showdown/dist/showdown";
|
import showdown from "showdown/dist/showdown";
|
||||||
import dataurl from "../utils/dataurl";
|
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();
|
var converter = new showdown.Converter();
|
||||||
converter.setFlavor("original");
|
converter.setFlavor("original");
|
||||||
@@ -27,18 +27,11 @@ class Tiny {
|
|||||||
return converter.makeMarkdown(this.data, getDummyDocument());
|
return converter.makeMarkdown(this.data, getDummyDocument());
|
||||||
}
|
}
|
||||||
|
|
||||||
toTitle() {
|
|
||||||
if (!this.text) {
|
|
||||||
this.text = this.toTXT();
|
|
||||||
}
|
|
||||||
return getTitleFromText(this.text);
|
|
||||||
}
|
|
||||||
|
|
||||||
toHeadline() {
|
toHeadline() {
|
||||||
if (!this.text) {
|
const paragraph = this.document.querySelector("p");
|
||||||
this.text = this.toTXT();
|
if (!paragraph) return;
|
||||||
}
|
|
||||||
return getHeadlineFromText(this.text);
|
return getInnerText(paragraph);
|
||||||
}
|
}
|
||||||
|
|
||||||
isEmpty() {
|
isEmpty() {
|
||||||
|
|||||||
@@ -9,3 +9,7 @@ export function getDummyDocument() {
|
|||||||
const doc = parseHTML("<div></div>");
|
const doc = parseHTML("<div></div>");
|
||||||
return typeof DOMParser === "undefined" ? doc : doc;
|
return typeof DOMParser === "undefined" ? doc : doc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getInnerText(element) {
|
||||||
|
return element.innerText || element.textContent;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user