hexojs / hexo-renderer-markdown-it

Markdown-it is a Markdown parser, done right. A faster and CommonMark compliant alternative for Hexo.
MIT License
343 stars 60 forks source link

Cannot render headings with ids? #40

Closed pujiaxun closed 6 years ago

pujiaxun commented 8 years ago

This renderer cannot generate ids in headings because of Markdown-it plugin not implement it. So I tried a solution Just use a plugin markdown-it-named-headings

npm install markdown-it-named-headings --save

And then add one line to render it with this plugin.

parser.use(require('markdown-it-named-headings'))

Just like this.

'use strict';

module.exports = function (data, options) {
  var MdIt = require('markdown-it');
  var cfg = this.config.markdown;
  var opt = (cfg) ? cfg : 'default';
  var parser = (opt === 'default' || opt === 'commonmark' || opt === 'zero') ?
    new MdIt(opt) :
    new MdIt(opt.render);

  parser.use(require('markdown-it-named-headings'))

 if (opt.plugins) {
    parser = opt.plugins.reduce(function (parser, pugs) {
      return parser.use(require(pugs));
    }, parser);
  }

  if (opt.anchors) {
    parser = parser.use(require('./anchors'), opt.anchors);
  }

  return parser.render(data.text);
};

Could I make a PR for this?

khiav223577 commented 7 years ago

Thanks. It works!

onlyfly34 commented 6 years ago

Is this merged into the project? I had the same issues and this works for me to add parser.use(require('markdown-it-named-headings'))

HanchengZhao commented 6 years ago

Hi, I wonder what else do you need to do after adding this line? I still have the problem after doing that. Thanks

pujiaxun commented 6 years ago

I think that’s all I did. Maybe dependencies had updated?

On Wed, 13 Dec 2017 at 11:48 Henry Zhao notifications@github.com wrote:

Hi, I wonder what else do you need to do after adding this line? I still have the problem after doing that. Thanks

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hexojs/hexo-renderer-markdown-it/issues/40#issuecomment-351274278, or mute the thread https://github.com/notifications/unsubscribe-auth/AMkUdl2gPGJQE7METdOJuacYkEZUmrwrks5s_0kmgaJpZM4Ji8ez .

pujiaxun commented 6 years ago

@HanchengZhao Maybe you can reference this issue. If it doesn't work, I cannot solve it neither.

https://github.com/pujiaxun/pujiaxun.github.io/issues/1

HanchengZhao commented 6 years ago

@pujiaxun Thanks for the comment. My issue actually has something to do with 'hexo-toc' package. For those who might have the same problem, check out https://github.com/YenYuHsuan/hexo-theme-beantech/issues/11

noraj commented 6 years ago

hexo-renderer-markdown-it is not compatible with hexo-toc yet even if hexo-toc claims it.

See https://github.com/bubkoo/hexo-toc/issues/20

noraj commented 6 years ago

Is this issue still up to date or it can be closed ?

pujiaxun commented 6 years ago

@noraj So you mean it's not a bug but a kind of feature or nonsupport? Feel free to close it if you want.

noraj commented 6 years ago

@pujiaxun hexo-renderer-markdown-it works perfectly, it's hexo-toc that is a mess.