I’d also suggest using something like app.search("kind: markdown")
. If needed, add {in: app.databases['YOUR DATABASE']}
after the search string.
Rationale:
- That way, you can search through all databases at once. Which is more difficult if you use the
records
collection of a database, since you’ll have to do that for every database in turn. search
with “kind” is presumably a lot faster thandatabase.records().filter(r => r.type === "markdown")
since DT organizes its imported records by type on disk: all MD records are already collected underneath the same folder. First getting all the records and then filtering them will incur higher disk and CPU costs, aka “time”.
Some suggestions regarding your script:
- The function
dateFromString
is, in my opinion, overly complex and unneeded. What _should_work with a string like “2018-01-06 09:46:08 -0800” is this:
return new Date(string.replace(/(\S+) (\S+) (\S+)/,"$1T$2$3"));
That simply adds a “T” between date and time and removes the space before the time zone. It seems to do what it’s supposed to, though I’m not sure about the time zone. But since you ignore that in your original code, too, I don’t really bother. - You could try a simple
app.selectedRecords.filter(r => r.type() === "markdown"))
instead of usingwhose
here. Easier to write and understand, and perhaps faster. - Your
getDateText
is a bit too complicated (for my taste, that is). Instead of introducing a lot of local variables (that are in factconst
s!) you could use a simple RE like soconst RE = new RegExp(`^${dateHeader}(.*)$`,"m"); return richText.match[1];
- If you’re processing a lot of records, some optimizations might be in order, such as defining these regular expressions only once at the top of the script, saving
r.richText()
as a constant (and usingplainText()
, asrichText()
is a misnomer here, though it works). - Don’t use
let
norvar
for constants. It’s confusing.