-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.ts
46 lines (44 loc) · 1.92 KB
/
date.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { runAppleScript } from "@raycast/utils";
import { Placeholder, PlaceholderCategory, PlaceholderType } from "../types";
/**
* Placeholder for the current date supporting an optional format argument. Defaults to "Month Day, Year". Barring any issues, this should always be replaced.
*
* Syntax: `{{date format="..."}}` or `{{currentDate format="..."}}`, where `...` specifies a valid Unicode date format string. The format argument is optional; if not provided, the default is "MMMM d, yyyy" (e.g. "January 1, 2024").
*/
const DatePlaceholder: Placeholder = {
name: "date",
regex: /{{(date|currentDate)( format=("|').*?("|'))?}}/g,
apply: async (str: string, context?: { [key: string]: unknown }) => {
const format =
str.match(/(?<=format=("|')).*?(?=("|'))/)?.[0] || "MMMM d, yyyy";
const dateStr =
context && "date" in context
? (context["date"] as string)
: await runAppleScript(`use framework "Foundation"
set currentDate to current application's NSDate's alloc()'s init()
try
set formatter to current application's NSDateFormatter's alloc()'s init()
set format to "${format}"
formatter's setAMSymbol:"AM"
formatter's setPMSymbol:"PM"
formatter's setDateFormat:format
return (formatter's stringFromDate:currentDate) as string
end try`);
return { result: dateStr, date: dateStr };
},
result_keys: ["date"],
constant: false,
fn: async (format: string) =>
(
await DatePlaceholder.apply(
`{{date${format?.toString().length ? ` format="${format}"` : ""}}`
)
).result,
example: "What happened on {{date format='MMMM d'}} in history?",
description: "Replaced with the current date in the specified format.",
hintRepresentation: "{{date}}",
fullRepresentation: "Current Date",
type: PlaceholderType.Informational,
categories: [PlaceholderCategory.Calendar],
};
export default DatePlaceholder;