Skip to content

Commit

Permalink
feat: Auto date format
Browse files Browse the repository at this point in the history
  • Loading branch information
brunojm committed Jun 28, 2022
1 parent 35eb106 commit a06ac41
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 32 additions & 8 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -81,14 +84,29 @@ export async function getUserAuthToken(attempt = 0) {
}
}

function processBlockContent(content: string, preferredDateFormat: string) {
const reg = new RegExp(/timestamp:\|([0-9]+)\|/i)

This comment has been minimized.

Copy link
@TristanH

TristanH Jun 29, 2022

Member

this looks good! though i do kinda wonder if we shoulda used something more unique like rwtimestamp, but whatever

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
)
}
Expand Down Expand Up @@ -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

This comment has been minimized.

Copy link
@TristanH

TristanH Jun 29, 2022

Member

only getting this once 👌

if (response && response.ok) {
const responseJSON = await response.json()
const books = responseJSON.books
Expand All @@ -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})`)
Expand All @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -505,9 +527,11 @@ function main() {
line-height: 1;
-webkit-font-smoothing: antialiased;
}
.${triggerIconName} {
font-size: 20px;
}
.${triggerIconName}:before {
content: "\e900";
}
Expand Down

0 comments on commit a06ac41

Please sign in to comment.