mirror of
https://github.com/oen/liquid-template.git
synced 2026-07-22 05:40:24 +00:00
restructure folders and start using date-fns for parsing dates
This commit is contained in:
parent
8187dfb364
commit
1ee36112a4
8 changed files with 66 additions and 12 deletions
|
|
@ -22,6 +22,7 @@
|
|||
"typescript": "^4.2.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"date-fns": "^2.21.3",
|
||||
"liquidjs": "^9.25.0",
|
||||
"lodash": "^4.17.21"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import commonjs from '@rollup/plugin-commonjs';
|
|||
|
||||
const isProd = (process.env.BUILD === 'production');
|
||||
|
||||
const banner =
|
||||
const banner =
|
||||
`/*
|
||||
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
|
||||
if you want to view the source visit the plugins github repository
|
||||
|
|
@ -12,7 +12,7 @@ if you want to view the source visit the plugins github repository
|
|||
`;
|
||||
|
||||
export default {
|
||||
input: 'main.ts',
|
||||
input: 'src/main.ts',
|
||||
output: {
|
||||
dir: '.',
|
||||
sourcemap: 'inline',
|
||||
|
|
@ -27,4 +27,4 @@ export default {
|
|||
nodeResolve({browser: true}),
|
||||
commonjs(),
|
||||
]
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { get, isEmpty } from "lodash";
|
||||
import { Plugin } from "obsidian";
|
||||
|
||||
import { LiquidTemplatesSettings, DEFAULT_SETTINGS, LiquidTemplatesSettingsTab } from "./settings";
|
||||
|
|
@ -40,11 +41,29 @@ export default class LiquidTemplates extends Plugin {
|
|||
};
|
||||
|
||||
async loadSettings(): Promise<void> {
|
||||
this.settings = Object.assign({ ...DEFAULT_SETTINGS }, await this.loadData());
|
||||
this.settings = this.settingsWithDefault(await this.loadData());
|
||||
}
|
||||
|
||||
async saveSettings(): Promise<void> {
|
||||
// the templates folder can change
|
||||
this.setupAutosuggest();
|
||||
await this.saveData(this.settings);
|
||||
}
|
||||
|
||||
settingsWithDefault(savedSettings: LiquidTemplatesSettings): LiquidTemplatesSettings {
|
||||
// TODO: improve me
|
||||
return {
|
||||
templatesFolder: this.getSettingOrDefault(savedSettings, 'templatesFolder'),
|
||||
excludeFolders: this.getSettingOrDefault(savedSettings, 'excludeFolders'),
|
||||
dateFormat: this.getSettingOrDefault(savedSettings, 'dateFormat'),
|
||||
timeFormat: this.getSettingOrDefault(savedSettings, 'timeFormat'),
|
||||
autocompleteTrigger: this.getSettingOrDefault(savedSettings, 'autocompleteTrigger'),
|
||||
}
|
||||
}
|
||||
|
||||
getSettingOrDefault(savedSettings: LiquidTemplatesSettings, settingKey: string, defaultValue?: any): any {
|
||||
let currentValue = get(savedSettings, settingKey)
|
||||
if (!isEmpty(currentValue)) return currentValue;
|
||||
return get(DEFAULT_SETTINGS, settingKey, defaultValue);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
import { get, isEmpty } from "lodash";
|
||||
import { App, PluginSettingTab, Setting } from "obsidian";
|
||||
import LiquidTemplates from "./main";
|
||||
|
||||
|
|
@ -2,7 +2,8 @@ import { App, TFile } from "obsidian";
|
|||
import { Liquid } from "liquidjs";
|
||||
import type PoweredTemplates from "../main";
|
||||
import CodeMirrorSuggest from "./codemirror-suggest";
|
||||
import { getTFilesFromFolder } from "utils";
|
||||
import { getTFilesFromFolder, customizeEngine } from "../utils";
|
||||
import { format } from "date-fns";
|
||||
|
||||
interface ITemplateCompletion {
|
||||
label: string;
|
||||
|
|
@ -21,7 +22,7 @@ export default class TemplateSuggest extends CodeMirrorSuggest<ITemplateCompleti
|
|||
|
||||
this.plugin = plugin;
|
||||
// TODO: move me in a separate class
|
||||
this.engine = new Liquid({
|
||||
this.engine = customizeEngine(new Liquid({
|
||||
fs: {
|
||||
readFileSync: (file) => {
|
||||
// TODO: implement me
|
||||
|
|
@ -47,7 +48,7 @@ export default class TemplateSuggest extends CodeMirrorSuggest<ITemplateCompleti
|
|||
},
|
||||
extname: '.md',
|
||||
greedy: true,
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
open(): void {
|
||||
|
|
@ -83,13 +84,18 @@ export default class TemplateSuggest extends CodeMirrorSuggest<ITemplateCompleti
|
|||
}
|
||||
|
||||
async generateContext() {
|
||||
const {
|
||||
dateFormat,
|
||||
timeFormat
|
||||
} = this.plugin.settings;
|
||||
|
||||
return {
|
||||
// TODO: improve me
|
||||
date: await this.engine.parseAndRender(`{{ "now" | date: "${this.plugin.settings.dateFormat}"}}`),
|
||||
time: await this.engine.parseAndRender(`{{ "now" | date: "${this.plugin.settings.timeFormat}"}}`),
|
||||
date: format(Date.now(), dateFormat),
|
||||
time: format(Date.now(), timeFormat),
|
||||
title: await this.app.workspace.getActiveFile().basename,
|
||||
default_date_format: this.plugin.settings.dateFormat,
|
||||
default_time_format: this.plugin.settings.timeFormat
|
||||
default_date_format: dateFormat,
|
||||
default_time_format: timeFormat
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -99,6 +105,7 @@ export default class TemplateSuggest extends CodeMirrorSuggest<ITemplateCompleti
|
|||
): Promise<void> {
|
||||
const head = this.getStartPos();
|
||||
const anchor = this.cmEditor.getCursor();
|
||||
console.log('--- context', await this.generateContext());
|
||||
|
||||
if (suggestion.file) {
|
||||
const templateString = await this.app.vault.read(suggestion.file)
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
import { App, normalizePath, TAbstractFile, TFile, TFolder, Vault } from "obsidian";
|
||||
import { compact } from 'lodash';
|
||||
import { Liquid } from "liquidjs";
|
||||
import { format, parse, subDays } from 'date-fns';
|
||||
|
||||
const ALLOWED_EXTENSIONS = ['md']
|
||||
|
||||
|
|
@ -28,3 +30,29 @@ export function getTFilesFromFolder(app: App, folderName: string, subfoldersToEx
|
|||
|
||||
return files;
|
||||
}
|
||||
|
||||
function parseDate(stringToParse: string): Date {
|
||||
if(['now', 'today'].includes(stringToParse)) return new Date;
|
||||
if(stringToParse === 'yesterday') return subDays(Date.now(), 1);
|
||||
// try to parse the given string
|
||||
return new Date(Date.parse(stringToParse));
|
||||
}
|
||||
|
||||
export function customizeEngine(engine: Liquid, ): Liquid {
|
||||
engine.registerFilter('date', (valueToParse, dateFormat) => {
|
||||
const dateToFormat = (typeof valueToParse === 'number')
|
||||
? valueToParse
|
||||
: parseDate(valueToParse)
|
||||
try {
|
||||
return format(dateToFormat, dateFormat)
|
||||
} catch (e) {
|
||||
if (e instanceof RangeError) return valueToParse;
|
||||
}
|
||||
});
|
||||
|
||||
engine.registerFilter('days_ago', (daysToSub) => {
|
||||
return subDays(Date.now(), daysToSub);
|
||||
})
|
||||
|
||||
return engine;
|
||||
}
|
||||
Loading…
Reference in a new issue