Skip to content

Commit

Permalink
no need to check dateformat
Browse files Browse the repository at this point in the history
  • Loading branch information
xxchan committed Apr 7, 2022
1 parent 65d8fd6 commit eda4301
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
function renderCountdown(el, dateFormat) {
function renderCountdown(el) {
// add a countdown to the element
// change <time>2022-02-05 Sat</time> to <time>2022-02-05 Sat(x days left)</time>
const time = el.querySelector("time")
const timeText = time.textContent

const dateFormatParts = dateFormat.split(" ")
const timeTextParts = timeText.split(" ")
var timeDate
if (dateFormatParts.length == 1) {
timeDate = moment(timeTextParts[0]).toDate()
} else if (dateFormatParts.length == 2) {
if (dateFormatParts[0][0] == "E") {
timeDate = moment(timeTextParts[1]).toDate()
} else {
timeDate = moment(timeTextParts[0]).toDate()
}
} else {
console.error("Not supported dateFormat: ", dateFormat)
}
const timeDate = moment(timeTextParts[0]).toDate()

const now = new Date()
const daysDiff = daysDifference(now, timeDate)
Expand All @@ -34,15 +22,13 @@ async function main() {
const pluginId = logseq.baseInfo.id
console.info(`#${pluginId}: MAIN`)

const dateFormat = (await logseq.App.getUserConfigs()).preferredDateFormat

const observer = new MutationObserver((mutationList) => {
for (const mutation of mutationList) {
for (const node of mutation.addedNodes) {
if (node.querySelectorAll) {
const nodes = node.querySelectorAll(".timestamp")
for (const n of nodes) {
renderCountdown(n, dateFormat)
renderCountdown(n)
}
}
}
Expand Down

0 comments on commit eda4301

Please sign in to comment.