Skip to content

Commit

Permalink
Merge pull request #61 from omnivore-app/fix/date-parsing-template-urls
Browse files Browse the repository at this point in the history
Fix date/time parsing + URLs in default template
  • Loading branch information
sywhb authored Dec 21, 2022
2 parents 5f9a1da + b433966 commit ef7bec3
Show file tree
Hide file tree
Showing 4 changed files with 341 additions and 19 deletions.
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
},
"scripts": {
"dev": "parcel public/index.html --public-url ./",
"build": "parcel build --public-url . --no-source-maps public/index.html"
"build": "parcel build --public-url . --no-source-maps public/index.html",
"test": "yarn mocha -r ts-node/register tests/*.ts"
},
"license": "MIT",
"logseq": {
Expand All @@ -35,15 +36,21 @@
"@semantic-release/exec": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"@tsconfig/node16": "^1.0.1",
"@types/chai": "^4.3.4",
"@types/chai-string": "^1.4.2",
"@types/diff-match-patch": "^1.0.32",
"@types/luxon": "^2.3.2",
"@types/markdown-escape": "^1.1.0",
"@types/mocha": "^10.0.1",
"@types/mustache": "^4.2.1",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"chai": "^4.3.7",
"chai-string": "^1.5.0",
"eslint": "^8.6.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"mocha": "^10.2.0",
"parcel": "^2.0.0",
"prettier": "^2.5.1",
"semantic-release": "^19.0.3",
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
loadArticles,
loadDeletedArticleSlugs,
PageType,
parseDateTime,
DATE_FORMAT,
} from './util'
import { DateTime } from 'luxon'
import { render } from 'mustache'
Expand Down Expand Up @@ -52,7 +54,6 @@ const siteNameFromUrl = (originalArticleUrl: string): string => {
}

const delay = (t = 100) => new Promise((r) => setTimeout(r, t))
const DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"
let loading = false

const getQueryFromFilter = (filter: Filter, customQuery: string): string => {
Expand Down Expand Up @@ -101,6 +102,8 @@ const fetchOmnivore = async (inBackground = false) => {
const preferredDateFormat: string = userConfigs.preferredDateFormat

try {
console.log(`logseq-omnivore starting sync since: '${syncAt}`)

!inBackground && (await logseq.UI.showMsg('🚀 Fetching articles ...'))

let omnivorePage = await logseq.Editor.getPage(pageName)
Expand Down Expand Up @@ -135,7 +138,7 @@ const fetchOmnivore = async (inBackground = false) => {
apiKey,
after,
size,
DateTime.fromFormat(syncAt, DATE_FORMAT).toISO(),
parseDateTime(syncAt).toISO(),
getQueryFromFilter(filter, customQuery)
)

Expand Down Expand Up @@ -301,7 +304,7 @@ const fetchOmnivore = async (inBackground = false) => {
apiKey,
after,
size,
DateTime.fromFormat(syncAt, DATE_FORMAT).toISO()
parseDateTime(syncAt).toISO()
)

for (const slug of deletedArticleSlugs) {
Expand Down
12 changes: 12 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { diff_match_patch } from 'diff-match-patch'
import { DateTime } from 'luxon'
import escape from 'markdown-escape'

export const DATE_FORMAT_W_OUT_SECONDS = "yyyy-MM-dd'T'HH:mm"
export const DATE_FORMAT = `${DATE_FORMAT_W_OUT_SECONDS}:ss`

export interface GetArticleResponse {
data: {
article: {
Expand Down Expand Up @@ -180,3 +184,11 @@ export const markdownEscape = (text: string): string => {
export const escapeQuotationMarks = (text: string): string => {
return text.replace(/"/g, '\\"')
}

export const parseDateTime = (str: string): DateTime => {
const res = DateTime.fromFormat(str, DATE_FORMAT)
if (res.isValid) {
return res
}
return DateTime.fromFormat(str, DATE_FORMAT_W_OUT_SECONDS)
}
Loading

0 comments on commit ef7bec3

Please sign in to comment.