Skip to content

Instantly share code, notes, and snippets.

@balaji-dutt
Created May 10, 2020 02:51
Show Gist options
  • Save balaji-dutt/97fb8dde1ff0cbc8701c7d6fdda1ff80 to your computer and use it in GitHub Desktop.
Save balaji-dutt/97fb8dde1ff0cbc8701c7d6fdda1ff80 to your computer and use it in GitHub Desktop.
Create Hugo front-matter in TOML format for 1Writer.app
// Inserts TOML-formatted frontmatter for Hugo as a heading at the beginning of your document.
// Warning: Only works correctly if you turn off "Smart Punctuation" in iOS settings.
// get filename
var filename = editor.getFileName().slice(0,-3);
//Get Timezone offset
// String such as +5:30 or -6:00 or Z
var timezone_offset_min = new Date().getTimezoneOffset(),
offset_hrs = parseInt(Math.abs(timezone_offset_min/60)),
offset_min = Math.abs(timezone_offset_min%60),
timezone_standard;
if(offset_hrs < 10)
offset_hrs = '0' + offset_hrs;
if(offset_min < 10)
offset_min = '0' + offset_min;
// Add an opposite sign to the offset
// If offset is 0, it means timezone is UTC
if(timezone_offset_min < 0)
timezone_standard = '+' + offset_hrs + ':' + offset_min;
else if(timezone_offset_min > 0)
timezone_standard = '-' + offset_hrs + ':' + offset_min;
else if(timezone_offset_min == 0)
timezone_standard = 'Z';
//Generate ISO8601 formatted timestamp
var timestamp = function() {
function pad2(n) { return n < 10 ? '0' + n : n };
var date = new Date();
var timestamp = date.getFullYear().toString() + '-' + pad2(date.getMonth() + 1)
+ '-' + pad2( date.getDate()) + 'T' + pad2( date.getHours() ) + ':' + pad2( date.getMinutes() ) + ':' + pad2( date.getSeconds() );
return( timestamp );
}
// set datetime
var datetime = "date = \"" + timestamp() + timezone_standard + "\"";
// set title for Hugo front matter
var title = "title = \"" + filename + "\""
//set draft false for Hugo front matter
var draftstatus = "draft = false"
// set front matter
var frontMatter = "\n+++\n" + title + "\n" + datetime + "\n" + draftstatus + "\n+++\n\n"
editor.replaceTextInRange(0, 0, frontMatter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment