Skip to content

Build your personal knowledge base with Trilium Notes

fix(clipper): keep the day a page states in its date labels

Elian Doran803db9fcb1c9
diff --git a/apps/web-clipper/entrypoints/readable-page/index.spec.ts b/apps/web-clipper/entrypoints/readable-page/index.spec.ts
index 9c85df2b5c..0ac6a31718 100644
--- a/apps/web-clipper/entrypoints/readable-page/index.spec.ts
+++ b/apps/web-clipper/entrypoints/readable-page/index.spec.ts
@@ -80,6 +80,20 @@ describe("readable page", () => {
expect(readPage()?.labels).toEqual({});
});
+ it("keeps the day the page states, whatever its offset from UTC", () => {
+ document.head.innerHTML = `
+ <title>Post title</title>
+ <meta property="article:published_time" content="2022-02-09T00:30:00+01:00">
+ <meta property="article:modified_time" content="2022-02-10T23:30:00-05:00">`;
+ document.body.innerHTML = `<article><p>${PARAGRAPH.repeat(5)}</p></article>`;
+ expect(readPage()?.labels).toEqual({ publishedDate: "2022-02-09", modifiedDate: "2022-02-10" });
+
+ document.head.innerHTML = "<title>Post title</title>";
+ document.body.innerHTML = `<article><p>${PARAGRAPH.repeat(5)}</p>
+ <p>Published <time>February 8, 2022</time></p></article>`;
+ expect(readPage()?.labels).toEqual({});
+ });
+
it("keeps an article that is only a picture, and returns nothing for a page without one", () => {
document.body.innerHTML = `<article><img src="photo.jpg" width="800" height="600"></article>`;
expect(readPage()?.images.map(({ src }) => src)).toEqual([ "https://example.com/blog/photo.jpg" ]);
diff --git a/apps/web-clipper/entrypoints/readable-page/index.ts b/apps/web-clipper/entrypoints/readable-page/index.ts
index 99f362dfba..7083781e10 100644
--- a/apps/web-clipper/entrypoints/readable-page/index.ts
+++ b/apps/web-clipper/entrypoints/readable-page/index.ts
@@ -46,8 +46,10 @@ function metaContent(property: string) {
return document.querySelector(`meta[property='${property}']`)?.getAttribute("content") ?? undefined;
}
-/** The day of `value` as `YYYY-MM-DD`, or `undefined` when it is missing or not a date. */
+/**
+ * The `YYYY-MM-DD` that `value` starts with, or `undefined` when it does not start with one. The day
+ * is the one the page states: converting a timestamp to UTC can move it to the day before or after.
+ */
function isoDate(value: string | undefined) {
- const date = new Date(value ?? "");
- return Number.isNaN(date.getTime()) ? undefined : date.toISOString().substring(0, 10);
+ return /^\d{4}-\d{2}-\d{2}/.exec(value?.trim() ?? "")?.[0];
}