如何防止我的github页面制作帖子标题为“ title-cased”

时间:2018-11-18 15:51:44

标签: jekyll github-pages jekyll-theme

我创建了一个github页面,并选择了一个拟议的Jekyll主题,称为minima。要添加帖子,我创建了一个名为2018-11-16-My-first-post-on-github.md的文件。但是,显示的帖子标题是转换为标题大小写的文本:My First Post On Github,因此每个单词中的每个首字母都大写。我该如何预防?这是主题相关的吗?

2 个答案:

答案 0 :(得分:0)

exports.toTitleCase = function(str){
  var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;

  return (str+'').replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
    if (index > 0 && index + match.length !== title.length &&
      match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
      (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
      title.charAt(index - 1).search(/[^\s-]/) < 0) {
      return match.toLowerCase();
    }

    if (match === 'tus') {
      return match;
    }

    if (match.substr(1).search(/[A-Z]|\../) > -1) {
      return match;
    }
    // Avoid uppercasing 'mod_deflate', apt-file - kvz
    if (match.match(/.[\_\-\/\d]./)) {
      return match;
    }
    // Avoid uppercasing '`frame`', '/sftp/import' - kvz
    if (match.match(/(^[`\/]|[`]$)/)) {
      return match;
    }
    // Avoid uppercasing: 'tmpfs' or anything that doesn't have a vowel - kvz
    if (!match.match(/[aeiou]/)) {
      return match;
    }

    return match.charAt(0).toUpperCase() + match.substr(1);
  });
};

exports.newPost = function (content, opts, cb){
  var self        = this;
  var matches     = [];
  var oldTitle    = '';
  var newTitle    = '';
  var oldLine     = '';
  var heading     = '';
  var newLine     = '';
  var changes     = [];
  var words       = [];
  var frontMatter = content.split('---')[1];

  if (frontMatter) {
    matches     = frontMatter.match(/^(title\s*:\s*)\"?(.+?)\"?[\ \t]*$/im);
    oldTitle    = matches[2];
    newTitle    = self.toTitleCase(oldTitle).trim();
    oldLine     = matches[0];
    newLine     = matches[1] + '"' + newTitle + '"';
    if (oldLine !== newLine) {
      changes.push({oldTitle: oldTitle, newTitle: newTitle});
      content = content.replace(oldLine, newLine);
    }
  }

  if (opts.body === true) {
    matches = content.match(/^\#{1,6} ([a-zA-Z0-9\-\;\!\?\%\&\;\:\.\/\(\)\ ]+)$/mg)
    for (var i in matches) {
      words       = matches[i].split(' ')
      heading     = words.shift();
      oldTitle    = words.join(' ');
      newTitle    = self.toTitleCase(oldTitle).trim();
      oldLine     = heading + ' ' + oldTitle;
      newLine     = heading + ' ' + newTitle;
      if (oldLine !== newLine) {
        changes.push({oldTitle: oldTitle, newTitle: newTitle});
        content = content.replace(oldLine, newLine);
      }
    }
  }


  if (changes.length === 0) {
    content = null;
  }

  return cb(null, content, changes);
};

答案 1 :(得分:0)

Jekyll 自动在导入时将您的帖子标题转换为大写。您可以对此by using a plugin进行修复,也可以通过using a YAML fileFront matter修复为specify your title value directly