link-open.js 541 B

123456789101112131415161718
  1. // add target="_blank" to all links
  2. module.exports = function linkOpen(md) {
  3. const defaultRender =
  4. md.renderer.rules.link_open ||
  5. function(tokens, idx, options, env, self) {
  6. return self.renderToken(tokens, idx, options);
  7. };
  8. md.renderer.rules.link_open = function(tokens, idx, options, env, self) {
  9. const aIndex = tokens[idx].attrIndex('target');
  10. if (aIndex < 0) {
  11. tokens[idx].attrPush(['target', '_blank']); // add new attribute
  12. }
  13. return defaultRender(tokens, idx, options, env, self);
  14. };
  15. };