Skip to content

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

core: Limit note subtitle/headline length. (#9732)

Chloe Oletto43b90070a9ab
diff --git a/packages/core/__tests__/notes.test.ts b/packages/core/__tests__/notes.test.ts
index 2f2dd8623..b8b7a0419 100644
--- a/packages/core/__tests__/notes.test.ts
+++ b/packages/core/__tests__/notes.test.ts
@@ -234,6 +234,18 @@ test("note should get headline from first paragraph in content", () =>
expect(note?.headline).toBe("This is a very colorful existence.");
}));
+test("note headline should be capped at 350 characters", () =>
+ noteTest({
+ content: {
+ type: TEST_NOTE.content.type,
+ data: `<p>${"a".repeat(400)}</p>`
+ }
+ }).then(async ({ db, id }) => {
+ const note = await db.notes.note(id);
+ expect(note?.headline).toHaveLength(350);
+ expect(note?.headline).toBe("a".repeat(350));
+ }));
+
test("note should not get headline if there is no p tag", () =>
noteTest({
...TEST_NOTE,
diff --git a/packages/core/src/content-types/tiptap.ts b/packages/core/src/content-types/tiptap.ts
index 0ee2ee3e8..5e32e86c3 100644
--- a/packages/core/src/content-types/tiptap.ts
+++ b/packages/core/src/content-types/tiptap.ts
@@ -90,7 +90,7 @@ export class Tiptap {
}
toHeadline() {
- return extractHeadline(this.data);
+ return extractHeadline(this.data, 350);
}
toTitle() {
diff --git a/packages/core/src/utils/html-parser.ts b/packages/core/src/utils/html-parser.ts
index 46d02e169..fddbf5bb8 100644
--- a/packages/core/src/utils/html-parser.ts
+++ b/packages/core/src/utils/html-parser.ts
@@ -44,7 +44,7 @@ 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) {
+export function extractHeadline(html: string, characterLimit: number) {
let text = "";
let start = false;
const parser = new Parser(
@@ -60,7 +60,14 @@ export function extractHeadline(html: string) {
}
},
ontext: (data) => {
- if (start) text += data;
+ if (start) {
+ text += data
+ if (text.length > characterLimit){
+ text = text.slice(0, characterLimit);
+ parser.pause()
+ parser.end()
+ }
+ }
}
},
{