mirror of
https://github.com/annimon/obsidian-timelive.git
synced 2026-07-22 05:45:09 +00:00
Extracted date parser and date formatter
This commit is contained in:
parent
861df7618d
commit
dbb94d1d8e
5 changed files with 46 additions and 12 deletions
9
src/DateFormatter.ts
Normal file
9
src/DateFormatter.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
export interface DateFormatter {
|
||||
formatDate(date: Date): string;
|
||||
}
|
||||
|
||||
export class TimeliveDateFormatter implements DateFormatter {
|
||||
public formatDate(date: Date): string {
|
||||
return date.toDateString();
|
||||
}
|
||||
}
|
||||
14
src/DateParser.ts
Normal file
14
src/DateParser.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
export interface DateParser {
|
||||
parseDate(dateString: string): Date;
|
||||
}
|
||||
|
||||
const REGEX_PRESENT = /now|today|present/;
|
||||
|
||||
export class TimeliveDateParser implements DateParser {
|
||||
public parseDate(dateString: string): Date {
|
||||
if (REGEX_PRESENT.test(dateString)) {
|
||||
return new Date();
|
||||
}
|
||||
return new Date(dateString);
|
||||
}
|
||||
}
|
||||
7
src/DateTransformer.ts
Normal file
7
src/DateTransformer.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { DateParser } from "./DateParser";
|
||||
import { DateFormatter } from "./DateFormatter";
|
||||
|
||||
export class DateTransformer {
|
||||
constructor(public parser: DateParser, public formatter: DateFormatter) {
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
import { DateTransformer } from "./DateTransformer";
|
||||
|
||||
const DAYS_IN_YEAR = 365.2422;
|
||||
const MILLIS_IN_DAY = 1000 * 3600 * 24;
|
||||
const MILLIS_IN_YEAR = MILLIS_IN_DAY * DAYS_IN_YEAR;
|
||||
|
|
@ -10,6 +12,7 @@ interface TimeEvent {
|
|||
|
||||
export class Timelive {
|
||||
private root: HTMLElement;
|
||||
private transformer: DateTransformer;
|
||||
private events: TimeEvent[] = [];
|
||||
// For timeline computation
|
||||
private minDate?: Date;
|
||||
|
|
@ -17,12 +20,13 @@ export class Timelive {
|
|||
private startDateTime: number = 0;
|
||||
private totalDays: number = 0;
|
||||
|
||||
constructor(root: HTMLElement) {
|
||||
constructor(root: HTMLElement, transformer: DateTransformer) {
|
||||
this.root = root;
|
||||
this.transformer = transformer;
|
||||
}
|
||||
|
||||
public addEvent(dateString: string, content: string) {
|
||||
const date = this.parseDate(dateString.toLowerCase());
|
||||
const date = this.transformer.parser.parseDate(dateString.trim().toLowerCase());
|
||||
this.events.push({ date, content, position: 0 });
|
||||
// Recalculate min/max dates
|
||||
const time = date.getTime();
|
||||
|
|
@ -104,7 +108,7 @@ export class Timelive {
|
|||
}
|
||||
|
||||
private formatEvent(event: TimeEvent, before: string = ""): string {
|
||||
const date = event.date.toLocaleDateString();
|
||||
const date = this.transformer.formatter.formatDate(event.date);
|
||||
return before +
|
||||
`<h4 class="tlv-date-title">${date}</h4>` +
|
||||
event.content;
|
||||
|
|
@ -128,12 +132,4 @@ export class Timelive {
|
|||
}
|
||||
return years;
|
||||
}
|
||||
|
||||
private parseDate(dateString: string): Date {
|
||||
// TODO: better date parsing
|
||||
if (/now|today|present/.test(dateString)) {
|
||||
return new Date();
|
||||
}
|
||||
return new Date(dateString);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
10
src/main.ts
10
src/main.ts
|
|
@ -1,16 +1,24 @@
|
|||
import { Plugin } from "obsidian";
|
||||
import { Timelive } from "./Timelive";
|
||||
import { TimeliveDateParser } from "./DateParser";
|
||||
import { DateTransformer } from "./DateTransformer";
|
||||
import { TimeliveDateFormatter } from "./DateFormatter";
|
||||
|
||||
const REGEX_COMMON: RegExp = /^\|(.{3,30}?)\|/;
|
||||
const REGEX_COMMON_REPLACE: RegExp = /^\s*<span.*?\/span>\|(.{3,30}?)\|\s*/gm;
|
||||
|
||||
export default class TimelivePlugin extends Plugin {
|
||||
override onload() {
|
||||
const transformer = new DateTransformer(
|
||||
new TimeliveDateParser(),
|
||||
new TimeliveDateFormatter(),
|
||||
);
|
||||
|
||||
this.registerMarkdownPostProcessor((element, _context) => {
|
||||
const ul = element.find("ul");
|
||||
if (!ul) return;
|
||||
|
||||
const timelive = new Timelive(ul);
|
||||
const timelive = new Timelive(ul, transformer);
|
||||
for (const li of ul.findAll("li")) {
|
||||
// All list items must match a pattern
|
||||
const m = li.innerText.match(REGEX_COMMON);
|
||||
|
|
|
|||
Loading…
Reference in a new issue