Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改heading的id的生成方式 #503

Closed
seff opened this issue Feb 27, 2014 · 2 comments
Closed

修改heading的id的生成方式 #503

seff opened this issue Feb 27, 2014 · 2 comments

Comments

@seff
Copy link

seff commented Feb 27, 2014

目前markdown的renderer会为文章中的每个heading生成id,在heading内容为拉丁语的时候没有任何问题,但若为其它多字节语言(如汉语)时,生成的id仅为“-”,这是由于javascript的正则匹配的问题,但在utf8编码的情况下,中文这样的多字节语言也同样可以作为标签的id,包括一些特殊符号,为了提高通用性和方便页面内导航,建设修改生成规则,拿掉目前的多种判断方式,仅需替换空格为“-”,虽然在这样heading的id比较混乱,但对于使用是有利的。目前我的处理方式是写一个插件覆盖库中的工作方式:

var marked = require('marked'),
  renderer = new marked.Renderer();

var heading_anchor = function(data, options, callback){
  var src = data.text.toString();
  renderer.heading = function (text, level) {
    var escapedText = text.toLowerCase().replace(/[\s]+/g, '-');

    return '<h' + level + ' id="' + escapedText + '">' + text + '</h' + level + '>';
  };

  return marked(src, {renderer: renderer});
};

hexo.extend.renderer.register('md', 'html', heading_anchor, true);
hexo.extend.renderer.register('markdown', 'html', heading_anchor, true);
hexo.extend.renderer.register('mkd', 'html', heading_anchor, true);
hexo.extend.renderer.register('mkdn', 'html', heading_anchor, true);
hexo.extend.renderer.register('mdwn', 'html', heading_anchor, true);
hexo.extend.renderer.register('mdtxt', 'html', heading_anchor, true);
hexo.extend.renderer.register('mdtext', 'html', heading_anchor, true);

经过这样的处理,目前heading为中文、英文及中英文符号时均能正常工作!

@Xuanwo
Copy link
Contributor

Xuanwo commented Aug 7, 2014

我都是直接指定文件的英文名字,感觉不是很需要

@tommy351 tommy351 closed this as completed Aug 7, 2014
zhipengyan pushed a commit to zhipengyan/hexo that referenced this issue Apr 4, 2016
@ting-talk
Copy link

我都是直接指定文件的英文名字,感觉不是很需要

请问如何指定?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants