How to use the hexo-front-matter.stringify function in hexo-front-matter

To help you get started, we’ve selected a few hexo-front-matter examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github xu-song / hexo-auto-category / lib / logic.js View on Github external
// 3. generate categories from directory
        // var categories = data.slug.split('/');
        var categories = data.source.split('/');
        // 3.1 handle depth
        var depth = this.config.auto_category.depth || categories.length-2;
        if (depth==0) { // Uncategorized
            //tmpPost.categories = ["Uncategorized"];
            return data;
        }
        var newCategories = categories.slice(1, 1+Math.min(depth, categories.length-2));
        // 3.2 prevents duplicate file changes
        if (Array.isArray(tmpPost.categories) && (tmpPost.categories.join("_") == newCategories.join("_"))) return data;
        tmpPost.categories = newCategories

        // 4. process post
        postStr = front.stringify(tmpPost);
        postStr = '---\n' + postStr;
        fs.writeFile(data.full_source, postStr, 'utf-8');
        log.i("Generated: categories [%s] for post [%s]", tmpPost.categories, categories[categories.length-1]);
    }
    return data
}
github LouisBarranqueiro / hexo-algoliasearch / lib / initPost.js View on Github external
var fs = require('fs');
  var frontMatter = require('hexo-front-matter');
  var id = cuid();
  /* eslint-disable camelcase */
  var filepath = post.full_source || post.path;
  /* eslint-enable camelcase */
  var file = fs.readFileSync(filepath, 'utf-8');
  var data = frontMatter.parse(file);
  
  /* eslint-disable camelcase */
  data.algolia_object_id = id;
  post.algolia_object_id = id;
  /* eslint-enable camelcase */

  try {
    fs.writeFileSync(filepath, frontMatter.stringify(data), 'utf-8');
    console.log(green('INFO  ') + 'Initialized: ' + magenta(post.path));
    return post;
  }
  catch (err) {
    console.log(red('ERROR  ') + 'Can\'t initialize ' + post.path + ' - ' + err);
  }
}
github LouisBarranqueiro / hexo-algoliasearch / lib / initPosts.js View on Github external
async.forEach(filenames, function(filename, cb) {
      try {
        file = fs.readFileSync(filename, 'utf-8');
        data = frontMatter.parse(file);
        if (data.algolia_object_id) {
          cb();
        }
        else {
          /* eslint-disable camelcase */
          data.algolia_object_id = cuid();
          /* eslint-enable camelcase */
          fs.writeFileSync(filename, frontMatter.stringify(data));
          console.log(chalk.green('Initialized: ') + chalk.magenta(path.basename(filename)));
          modifiedFilesCount++;
          cb();
        }
      }
      catch (err) {
        console.log(chalk.red('Can\'t process ' + filename + ' file : ' + err));
      }
    }, function(err) {
      if (err) {
github 52cik / hexo-admin-cn / update.js View on Github external
full_source = prev_full;
  if (update.source && update.source !== post.source) {
    // post.full_source only readable ~ see: /hexo/lib/models/post.js
    full_source = hexo.source_dir + update.source
  }

  preservedKeys.forEach(function (attr) {
    if (attr in update) {
      compiled[attr] = update[attr]
    }
  });
  compiled.date = moment(compiled.date).toDate()

  delete update._content

  var raw = hfm.stringify(compiled, {prefixSeparator: true}); // md 开头添加 ---
  update.raw = raw
  update.updated = moment()

  // tags and cats are only getters now. ~ see: /hexo/lib/models/post.js
  if ( typeof update.tags !== 'undefined' ) {
    post.setTags(update.tags)
    delete update.tags
  }
  if ( typeof update.categories !== 'undefined' ) {
    post.setCategories(update.categories)
    delete update.categories
  }

  extend(post, update)

  post.save(function () {
github jaredly / hexo-admin / update.js View on Github external
var prev_full = post.full_source,
    full_source = prev_full;
  if (update.source && update.source !== post.source) {
    // post.full_source only readable ~ see: /hexo/lib/models/post.js
    full_source = hexo.source_dir + update.source
  }

  preservedKeys.forEach(function (attr) {
    if (attr in update) {
      compiled[attr] = update[attr]
    }
  });
  compiled.date = moment(compiled.date).toDate()

  delete update._content
  var raw = hfm.stringify(compiled);
  update.raw = raw
  update.updated = moment()

  // tags and cats are only getters now. ~ see: /hexo/lib/models/post.js
  if ( typeof update.tags !== 'undefined' ) {
    post.setTags(update.tags)
    delete update.tags
  }
  if ( typeof update.categories !== 'undefined' ) {
    post.setCategories(update.categories)
    delete update.categories
  }

  extend(post, update)

  post.save(function () {
github hexojs / hexo / lib / hexo / post.js View on Github external
// Parse front-matter
    const obj = jsonMode ? JSON.parse(`{${frontMatter}}`) : yaml.load(frontMatter);

    // Add data which are not in the front-matter
    for (const key of Object.keys(data)) {
      if (!preservedKeys.includes(key) && obj[key] == null) {
        obj[key] = data[key];
      }
    }

    let content = '';
    // Prepend the separator
    if (yfmSplit.prefixSeparator) content += `${separator}\n`;

    content += yfm.stringify(obj, {
      mode: jsonMode ? 'json' : ''
    });

    // Concat content
    content += yfmSplit.content;

    if (data.content) {
      content += `\n${data.content}`;
    }

    return content;
  });
};
github rozbo / hexo-abbrlink / lib / logic.js View on Github external
var opt_rep = ((this.config.abbrlink && this.config.abbrlink.rep) ? this.config.abbrlink.rep : 'dec')
			
			let res = (opt_alg == 'crc32' ? crc32.str(data.title) >>> 0 : crc16(data.title) >>> 0);
			//check this abbrlink is already exist then get a different one
			abbrlink = model.check(res);
			//set abbrlink to hex or dec
			abbrlink = opt_rep == 'hex' ? abbrlink.toString(16) : abbrlink;
            data.abbrlink = abbrlink;
            let postStr;
            if (!/.*\.org/.test(data.source)){
            //re parse front matter
            var tmpPost = front.parse(data.raw);
            //add new generated link
            tmpPost.abbrlink = abbrlink;
            //process post
                postStr = front.stringify(tmpPost);
            postStr = '---\n' + postStr;
            fs.writeFileSync(data.full_source, postStr, 'utf-8');
            }
            else
            {
                postStr = data.raw.split("\n")
                postStr.splice(2,0,'#+ABBRLINK: ' + abbrlink)
                fs.writeFileSync(data.full_source, postStr.join('\n'), 'utf-8');
            }
            if(data.title.length==0)
                log.i("No title found for post [%s]", data.slug);
            log.i("Generate link %s for post [%s]", abbrlink, data.title);
        }
        model.add(abbrlink);
    }
    return data
github thesadabc / hexo-myadmin / lib / service / post.js View on Github external
update(id, { meta, content }) {
        let post = this.detail(id);

        let compiled = hfm.parse(['---', meta, '---', content].join('\n'));
        compiled.updated = compiled.updated || new Date();
        compiled.date = compiled.date || new Date(post.date.valueOf());
        compiled.author = compiled.author || post.author || this.hexo.config.author;
        if (this.type === "Post") {
            compiled.categories = compiled.categories || [this.hexo.config.default_category]
        }

        return fs.writeFile(post.full_source, hfm.stringify(compiled))
            .then(() => this.updateDB())
            .then(() => this.getSource(post.full_source));

    }

hexo-front-matter

Front-matter parser.

MIT
Latest version published 6 months ago

Package Health Score

73 / 100
Full package analysis