Improving the reference date parsing from a title when using tp.date.now()
/moment()
? 馃
#1293
Labels
enhancement
New feature or request
Is your feature request related to a problem?
Well, kind of 馃槆 ... or at least, if not a problem, some type of inconsistency 馃槉 ...
When using either
tp.date.now()
ormoment()
within a Templater template, one can use a date sitting in the title of the note (i.e.:tp.file.title
) as a reference date and manipulate it, in a way or another, while explicitly mentioning where needed, the MomentJS format the reference date follows so it can be used and parsed correctly by Templater when the template is applied to the note.Example 1: A
tp.date.now()
templateConsider the following template applied to a note titled
2024-01-10
:It outputs, in the note
2024-01-10
:Screen-recording
2024-01-10.-.tpdatenow.mov
Example 2: A
moment()
templateThe same result can be obtained by relying directly on
moment()
using a template such as:... which, when applied to a note titled
2024-01-10
also outputs:Screen-recording
2024-01-10.-.moment.mov
So far, so good 馃槃 !
In both cases, the parsing of the date in
tp.file.title
works as expected, outputting correct results as I applied both templates on a note following exactly the format I saidtp.file.title
would be using (i.e.:YYYY-MM-DD
).This also means that:
if one applies the template using
tp.date.now()
from above to a note which title doesn't contain any reference date to parse (e.g.: a note titledMy reference date
), Templater throw an alert stating:Screen-recording
My.reference.date.-.tpdatenow.mov
if one applies the template using
moment()
from above to a note which title doesn't contain any reference date to parse (e.g.: a note titledMy reference date
once more), Templater doesn't throw an alert butInvalid date
is returned instead of the expected date 馃槉.Screen-recording
My.reference.date.-.moment.mov
In any cases, we're clearly being told, in a way or another, that something's wrong with the template used 馃槉 ...
Now, what I've accidentally stumbled upon recently and got me bit confused (馃槄) was:
Applying the templates from above to a note titled
My reference date 1
Clearly,
My reference date 1
doesn't follow the formattp.file.title
is said to use in the template (YYYY-MM-DD
).And yet, what happens is that the
1
at the end of the title seems to be automatically interpreted as theyear
part of the date when applying either thetp.date.now()
or themoment()
template toMy reference date 1
, without failing or alerting me that something could be wrong (as Templater does if I apply the template to a note titledMy reference date
)...I mean, the full date format
tp.file.title
should follow (YYYY-MM-DD
) while being explicitly declared where needed, appears to be ignored... which, if one is not attentive enough, could lead to unexpected results 馃槆 .E.g.: Applying the
tp.date.now()
template onMy reference date 1
My.reference.date.1.-.tpdatenow.mov
Markdown output
E.g.: Applying the
moment()
template onMy reference date 1
My.reference.date.1.-.moment.mov
Markdown output
I was running "template" tests on a note titled
Untitled 24
which because of the24
suffix, gave me, by pure luck 馃崁 , my desired results and it took me some time to realise that maybe, it shouldn't have happened this way 馃 ? ...(Considering the
24
was there because I mindlessly createdUntitled
notes and didn't bother to rename them)So, I asked in Templater channel on the Obsidian Discord Server and it turns out that at the moment, date parsing with MomentJS can be done using a strict mode or a forgiving mode, Templater currently using the latter 馃槉.
The "forgiving mode" has the advantage that when a user is trying to parse a
tp.file.title
date refernce in a template applied to a note titledMy reference date is 2024-01-10
, while setting the reference date format toYYYY-MM-DD
(instead of the escaped moment format[My reference date is ]YYYY-MM-DD
) it still works and produces the right results... Which is probably easier for a lot of folks out there 馃榿 !E.g.: Applying the
tp.date.now()
template onMy reference date is 2024-01-10
My.reference.date.is.2024-01-10.-.tpdatenow.mov
Markdown output
E.g.: Applying the
moment()
template onMy reference date is 2024-01-10
My.reference.date.is.2024-01-10.-.moment.mov
Markdown output
On the other hand, I can't deny that I also see as potentially a bit problematic that chance (馃崁) was involved when I applied my template to the note titled
Untitled 24
and it output the correct results, ignoring the fact that the reference date format used in the template and the format of the title didn't match.Describe the solution you'd like:
As this didn't seem to be mentioned a lot (as a potential issue when it comes to date manipulations), I honestly feel like I'm nit-picking here 馃槄 ...
I mean, now that I know that a "forgiving mode" exists and is used for date parsing within Templater, I am perfectly fine with things as they currently are 馃榿.
It's probably easier for everybody and I can keep that info in mind (and in a note in Obsidian) when writing/testing my templates 馃槉.
I can absolutely live with it 馃槃 .
I think though, that maybe, something could be said about the "forgiving mode" Templater uses in the documentation.
(As it could potentially lead to undesired results in some cases)
Describe alternatives you've considered:
Well, as I asked on Discord, it appears that the options to solve this "issue" were:
MomentJS strict mode
for dates parsing which would probably mean that some would have to update their current templates and escape what would need to be escaped (in the MomentJS fashion, using square brackets ([]
)).This would represent a breaking change which considering the lack of demand (馃檲) is clearly overkill.
MomentJS strict mode
dates parsing, leaving the user more in control in how they would like to see this working (and what it could require when writing a template).Which is an idea I like 馃槆 .
But again, considering the lack of demand (馃檲) , that would probably add some unnecessary weight on the shoulders of the maintainers of the plugin, at the moment 馃槉 .
Additional context:
I'm filing this FR more to document all this and so others could provide some feedback/thoughts regarding the date parsing behaviour ("Forgiving mode" vs. "Strict mode") if they want to.
It's really just in case 馃槃 ... and also so the whole thing doesn't get lost on Discord 馃槉.
Speaking of Discord, I'm leaving here the links to the various messages from the conversation I had with Zachatoo about this:
I am also profoundly sorry for the wall of text this FR turned out to be as I tried to be as precise as I could 馃槆 !
And I can't thank Zachatoo enough for taking the time to look at this 馃檶 !
The text was updated successfully, but these errors were encountered: