Quantcast
Channel: DEVONtechnologies Community - Latest posts
Viewing all articles
Browse latest Browse all 17201

Setting created/modified dates from markdown front matter

$
0
0

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 than database.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 using whose 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 fact consts!) you could use a simple RE like so const 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 using plainText(), as richText() is a misnomer here, though it works).
  • Don’t use let nor var for constants. It’s confusing.

Viewing all articles
Browse latest Browse all 17201

Trending Articles