From c0e78aa5c92ee1dacbfa6f954173faefcb130e28 Mon Sep 17 00:00:00 2001 From: thecodrr Date: Thu, 25 Feb 2021 21:30:57 +0500 Subject: [PATCH] fix: trim text extracted from html to get correct headline --- packages/core/__tests__/notes.test.js | 12 ++++++++++++ packages/core/content-types/tiny.js | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.js index e6e60fa87..c84a1992c 100644 --- a/packages/core/__tests__/notes.test.js +++ b/packages/core/__tests__/notes.test.js @@ -75,6 +75,18 @@ 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", () => + noteTest({ + ...TEST_NOTE, + content: { + type: TEST_NOTE.content.type, + data: `
  1. Hello I won't be a headline :(
  2. Me too.
  3. Gold.
`, + }, + }).then(async ({ db, id }) => { + let note = db.notes.note(id); + expect(note.headline).toBe("Hello I won't be a headline :(Me too.Gold."); + })); + test("note title should allow trailing space", () => noteTest({ title: "Hello ", content: TEST_NOTE.content }).then( async ({ db, id }) => { diff --git a/packages/core/content-types/tiny.js b/packages/core/content-types/tiny.js index 6516aba21..ca3bf5847 100644 --- a/packages/core/content-types/tiny.js +++ b/packages/core/content-types/tiny.js @@ -15,9 +15,9 @@ class Tiny { } toTXT() { - if (window.DOMParser) { + if ("DOMParser" in window || "DOMParser" in global) { let doc = new DOMParser().parseFromString(this.data, "text/html"); - return (doc.body || doc.firstElementChild).textContent || ""; + return doc.body.textContent.trim(); } else { return decode( this.data.replace(/]*>/gi, "\n").replace(/<[^>]+>/g, "")