From a06ac419e0a7203d412b30afea9694ed961b7b66 Mon Sep 17 00:00:00 2001 From: Bruno Melo Date: Tue, 28 Jun 2022 19:30:03 -0300 Subject: [PATCH] feat: Auto date format --- package.json | 1 + pnpm-lock.yaml | 7 +++++++ src/main.tsx | 40 ++++++++++++++++++++++++++++++++-------- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index c52f32b..dc43a1b 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "license": "MIT", "dependencies": { "@logseq/libs": "^0.0.6", + "date-fns": "^2.28.0", "react": "^18.0.0", "react-dom": "^18.0.0", "react-use": "^17.3.2" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3432d44..a946194 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13,6 +13,7 @@ specifiers: '@typescript-eslint/parser': 5.28.0 '@vitejs/plugin-react': 1.3.2 conventional-changelog-conventionalcommits: 5.0.0 + date-fns: ^2.28.0 eslint: 8.17.0 eslint-plugin-react: 7.30.0 eslint-plugin-react-hooks: 4.6.0 @@ -29,6 +30,7 @@ specifiers: dependencies: '@logseq/libs': 0.0.6 + date-fns: 2.28.0 react: 18.1.0 react-dom: 18.1.0_react@18.1.0 react-use: 17.3.2_ef5jwxihqo6n7gxfmzogljlgcm @@ -1323,6 +1325,11 @@ packages: engines: {node: '>= 12'} dev: true + /date-fns/2.28.0: + resolution: {integrity: sha512-8d35hViGYx/QH0icHYCeLmsLmMUheMmTyV9Fcm6gvNwdw31yXXH+O85sOBJ+OLnLQMKZowvpKb6FgMIQjcpvQw==} + engines: {node: '>=0.11'} + dev: false + /dateformat/3.0.3: resolution: {integrity: sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q==} dev: true diff --git a/src/main.tsx b/src/main.tsx index 8ab5f26..a6a656b 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -4,6 +4,7 @@ import "virtual:windi.css" import React from "react" import App from "./App" import Font from "./icomoon.woff"; +import {format} from 'date-fns' import {logseq as PL} from "../package.json" import {triggerIconName} from "./utils" @@ -13,6 +14,8 @@ import {createRoot} from "react-dom/client"; // @ts-expect-error const css = (t, ...args) => String.raw(t, ...args) const magicKey = `__${PL.id}__loaded__` +// @ts-ignore +const partial = (func, ...args) => (...rest) => func(...args, ...rest); export const isDev = process.env.NODE_ENV === "development" export const baseURL = isDev ? "https://local.readwise.io:8000" : "https://readwise.io" @@ -81,14 +84,29 @@ export async function getUserAuthToken(attempt = 0) { } } +function processBlockContent(content: string, preferredDateFormat: string) { + const reg = new RegExp(/timestamp:\|([0-9]+)\|/i) + if (content !== undefined) { + return content.replace(reg, function (match, timestamp) { + try { + return format(new Date(parseInt(timestamp)), preferredDateFormat) + } catch (e) { + return "" + } + + }) + } else { + return content + } +} -function convertReadwiseToIBatchBlock(obj: ReadwiseBlock) { +function convertReadwiseToIBatchBlock(preferredDateFormat: string, obj: ReadwiseBlock) { // we ignore the first one (which we can consider as the block title) const block: IBatchBlock = { - content: obj.string, + content: processBlockContent(obj.string, preferredDateFormat)!, } if (obj.children !== undefined) { - block.children = obj.children.map(convertReadwiseToIBatchBlock).filter( + block.children = obj.children.map(partial(convertReadwiseToIBatchBlock, preferredDateFormat)).filter( (b): b is IBatchBlock => b !== undefined ) } @@ -278,6 +296,7 @@ async function downloadArchive(exportID: number, setNotification?, setIsSyncing? setIsSyncing(false) } const booksIDsMap = logseq.settings!.booksIDsMap || {} + const preferredDateFormat = (await logseq.App.getUserConfigs()).preferredDateFormat if (response && response.ok) { const responseJSON = await response.json() const books = responseJSON.books @@ -292,7 +311,7 @@ async function downloadArchive(exportID: number, setNotification?, setIsSyncing? if (page !== null) { // page exists if (bookIsUpdate) { - const convertedUpdateBook = convertReadwiseToIBatchBlock(bookData) + const convertedUpdateBook = convertReadwiseToIBatchBlock(preferredDateFormat, bookData) if (convertedUpdateBook !== undefined) { await updatePage(page, convertedUpdateBook!.children!) setNotification(`Updating "${bookData.title}" completed (${index}/${books.length})`) @@ -303,7 +322,7 @@ async function downloadArchive(exportID: number, setNotification?, setIsSyncing? } } else { - const convertedNewBook = convertReadwiseToIBatchBlock(bookData) + const convertedNewBook = convertReadwiseToIBatchBlock(preferredDateFormat, bookData) if (convertedNewBook !== undefined) { const created = await createPage(bookData.title, convertedNewBook!.children!) if (created) { @@ -318,7 +337,9 @@ async function downloadArchive(exportID: number, setNotification?, setIsSyncing? }) const readwisePage = await logseq.Editor.getPage("Readwise") if (readwisePage) { - await updatePage(readwisePage, convertReadwiseToIBatchBlock(responseJSON.syncNotification!).children!) + await updatePage(readwisePage, convertReadwiseToIBatchBlock( + preferredDateFormat, responseJSON.syncNotification! + ).children!) } setIsSyncing(false) setNotification(null) @@ -488,13 +509,14 @@ function main() { top[magicKey] = true } logseq.provideStyle(css` - @font-face { + @font-face { font-family: 'readwise'; - src: url(${Font}) format('woff'); + src: url(${Font}) format('woff'); font-weight: normal; font-style: normal; font-display: block; } + [class^="rw-"], [class*=" rw-"] { font-family: 'readwise' !important; speak: never; @@ -505,9 +527,11 @@ function main() { line-height: 1; -webkit-font-smoothing: antialiased; } + .${triggerIconName} { font-size: 20px; } + .${triggerIconName}:before { content: "\e900"; }