Action

Gostatic post header

Posted by piranha, Last update 12 days ago

This adds a Jekyll-like header to a post in format gostatic understands.

Finds first h1 header (starting with #) and converts this to a title. Plus adds a date.

If there was a header already, just updates a date with current date.

Feel free to modify for your needs. Read more about it.

Steps

  • script

    /*
      Add gostatic-compatible header to post (or update date if it's already there)
    */
    
    function format(s, data) {
      return s.replace(/\{([^\}]+)\}/g, function(match, k) {
        return data[k];
      });
    }
    
    function pad(i) {
      return (i < 10 ? '0' : '') + i;
    }
    
    function getDate() {
      var d = new Date();
      return format('{year}-{month}-{day}', {
        year: d.getFullYear(),
        month: pad(d.getMonth() + 1),
        day: pad(d.getDate())
      });
    }
    
    function addHeader(content) {
      var title = content.match(/^# (.*)$/m);
      content = content.replace(title[0] + '\n', '');
      var header = format('title: {title}\ndate: {date}\ntags: \ndraft: true\n----\n',
                          {title: title[1],
                           date: getDate()});
      return header + content;
    }
    
    function updateHeader(content) {
      return content.replace(/^date: (.*)/m, 'date: ' + getDate());
    }
    
    function main() {
      var content = draft.content;
      var m = content.match(/^----$/m);
      if (m) {
        draft.content = updateHeader(content);
      } else {
        draft.content = addHeader(content);
      }
    }
    
    main();
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.