当前位置:网站首页>Markdown < a > tag new page open link

Markdown < a > tag new page open link

2022-04-23 20:16:00 _ Carpediem

markdown-it The version is 11.0.0 No, ’target,_blank’ attribute , The default a The link of the tag is this page , The previous page data will be lost , You need to create a new page in the browser to open , Add the following code to solve .

// Remember old renderer, if overridden, or proxy to default renderer
var md=require('markdown-it')
var defaultRender = md.renderer.rules.link_open || function(tokens, idx, options, env, self) {
    
  return self.renderToken(tokens, idx, options);
};

md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
    
  // If you are sure other plugins can't add `target` - drop check below
  var aIndex = tokens[idx].attrIndex('target');

  if (aIndex < 0) {
    
    tokens[idx].attrPush(['target', '_blank']); // add new attribute
  } else {
    
    tokens[idx].attrs[aIndex][1] = '_blank';    // replace value of existing attr
  }

  // pass token to default renderer.
  return defaultRender(tokens, idx, options, env, self);
};

Reference link

版权声明
本文为[_ Carpediem]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210553547118.html

随机推荐