Skip to content

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

core: segregate logic for note headline and note headline title generation (#7968)

01zulfibb5d5a573165
diff --git a/packages/core/__tests__/notes.test.ts b/packages/core/__tests__/notes.test.ts
index c2fe7a278..e6e2a39b9 100644
--- a/packages/core/__tests__/notes.test.ts
+++ b/packages/core/__tests__/notes.test.ts
@@ -134,7 +134,7 @@ test("changing content shouldn't reset the note title ", () =>
expect(note?.title).toBe("I am a note");
}));
-test("note title with headline format should keep generating headline until title is edited", () =>
+test("note title with headline format should keep generating headline title until title is edited", () =>
noteTest().then(async ({ db }) => {
await db.settings.setTitleFormat("$headline$");
const id = await db.notes.add({
@@ -191,19 +191,44 @@ test("note title with headline format should keep generating headline until titl
"headlineLorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam rutrum ex ac eros egestas, ut rhoncus felis faucibus. Mauris tempor orci nisl,"
]
].forEach(([testCase, content, expectedHeadline]) => {
- test(`note should generate headline up to HEADLINE_CHARACTER_LIMIT characters - ${testCase}`, () =>
- noteTest({
- ...TEST_NOTE,
- content: {
- type: TEST_NOTE.content.type,
- data: content
- }
- }).then(async ({ db, id }) => {
+ test(`note title with headline format should generate headline title up to 150 characters - ${testCase}`, () =>
+ noteTest().then(async ({ db }) => {
+ await db.settings.setTitleFormat("$headline$");
+ const id = await db.notes.add({
+ content: {
+ type: TEST_NOTE.content.type,
+ data: content
+ }
+ });
const note = await db.notes.note(id);
- expect(note?.headline).toBe(expectedHeadline);
+ expect(note?.title).toBe(expectedHeadline);
}));
});
+test("note should get headline from first paragraph in content", () =>
+ noteTest({
+ ...TEST_NOTE,
+ content: {
+ type: TEST_NOTE.content.type,
+ data: "<p>This is a very colorful existence.</p>"
+ }
+ }).then(async ({ db, id }) => {
+ const note = await db.notes.note(id);
+ expect(note?.headline).toBe("This is a very colorful existence.");
+ }));
+
+test("note should not get headline if there is no p tag", () =>
+ noteTest({
+ ...TEST_NOTE,
+ content: {
+ type: TEST_NOTE.content.type,
+ data: `<ol style="list-style-type: decimal;" data-mce-style="list-style-type: decimal;"><li>Hello I won't be a headline :(</li><li>Me too.</li><li>Gold.</li></ol>`
+ }
+ }).then(async ({ db, id }) => {
+ const note = await db.notes.note(id);
+ expect(note?.headline).toBe("");
+ }));
+
test("note title should allow trailing space", () =>
noteTest({ title: "Hello ", content: TEST_NOTE.content }).then(
async ({ db, id }) => {
diff --git a/packages/core/src/collections/notes.ts b/packages/core/src/collections/notes.ts
index ff1833fca..d8f1db0aa 100644
--- a/packages/core/src/collections/notes.ts
+++ b/packages/core/src/collections/notes.ts
@@ -87,6 +87,7 @@ export class Notes implements ICollection {
let contentId = item.contentId;
let dateEdited = item.dateEdited;
let headline = item.headline;
+ let headlineTitle = "";
if (item.content && item.content.data && item.content.type) {
logger.debug("saving content", { id });
@@ -95,7 +96,8 @@ export class Notes implements ICollection {
const content = await getContentFromData(type, data);
if (!content) throw new Error("Invalid content type.");
- headline = getNoteHeadline(content);
+ headline = content.toHeadline();
+ headlineTitle = content.toTitle();
dateEdited = Date.now();
contentId = await this.db.content.add({
noteId: id,
@@ -125,7 +127,7 @@ export class Notes implements ICollection {
this.db.settings.getTitleFormat(),
this.db.settings.getDateFormat(),
this.db.settings.getTimeFormat(),
- headline ? headlineToTitle(headline) : "",
+ headlineTitle,
this.totalNotes
);
item.isGeneratedTitle = true;
@@ -147,13 +149,10 @@ export class Notes implements ICollection {
if (
item.isGeneratedTitle &&
HEADLINE_REGEX.test(titleFormat) &&
- headline &&
- currentNoteTitleFields?.title !== headlineToTitle(headline)
+ headlineTitle &&
+ currentNoteTitleFields?.title !== headlineTitle
) {
- item.title = titleFormat.replace(
- HEADLINE_REGEX,
- headlineToTitle(headline)
- );
+ item.title = titleFormat.replace(HEADLINE_REGEX, headlineTitle);
}
}
@@ -488,11 +487,3 @@ export class Notes implements ICollection {
).internalLinks;
}
}
-
-function getNoteHeadline(content: Tiptap) {
- return content.toHeadline();
-}
-
-function headlineToTitle(headline: string) {
- return headline.split(" ").splice(0, 10).join(" ");
-}
diff --git a/packages/core/src/content-types/tiptap.ts b/packages/core/src/content-types/tiptap.ts
index 33fef04d7..a276973a8 100644
--- a/packages/core/src/content-types/tiptap.ts
+++ b/packages/core/src/content-types/tiptap.ts
@@ -31,6 +31,7 @@ import dataurl from "../utils/dataurl.js";
import {
HTMLParser,
extractHeadline,
+ extractTitle,
getDummyDocument
} from "../utils/html-parser.js";
import { HTMLRewriter } from "../utils/html-rewriter.js";
@@ -89,7 +90,11 @@ export class Tiptap {
}
toHeadline() {
- return extractHeadline(this.data, 150);
+ return extractHeadline(this.data);
+ }
+
+ toTitle() {
+ return extractTitle(this.data, 150);
}
// isEmpty() {
diff --git a/packages/core/src/utils/html-parser.ts b/packages/core/src/utils/html-parser.ts
index fc53c1bbf..47a80a7eb 100644
--- a/packages/core/src/utils/html-parser.ts
+++ b/packages/core/src/utils/html-parser.ts
@@ -44,14 +44,42 @@ function wrapIntoHTMLDocument(input: string) {
return `<!doctype html><html lang="en"><head><title>Document Fragment</title></head><body>${input}</body></html>`;
}
-export function extractHeadline(html: string, headlineCharacterLimit: number) {
+export function extractHeadline(html: string) {
+ let text = "";
+ let start = false;
+ const parser = new Parser(
+ {
+ onopentag: (name) => {
+ if (name === "p") start = true;
+ },
+ onclosetag: (name) => {
+ if (name === "p") {
+ start = false;
+ parser.pause();
+ parser.end();
+ }
+ },
+ ontext: (data) => {
+ if (start) text += data;
+ }
+ },
+ {
+ lowerCaseTags: false,
+ decodeEntities: true
+ }
+ );
+ parser.end(html);
+ return text;
+}
+
+export function extractTitle(html: string, characterLimit: number) {
let text = "";
const parser = new Parser(
{
ontext: (data) => {
text += data;
- if (text.length > headlineCharacterLimit) {
- text = text.slice(0, headlineCharacterLimit);
+ if (text.length > characterLimit) {
+ text = text.slice(0, characterLimit);
parser.pause();
parser.end();
}