| 
 | 1 | +'use strict'  | 
 | 2 | + | 
 | 3 | +// Adapted from https://github.com/markdown-it/markdown-it/blob/fbc6b0fed563ba7c00557ab638fd19752f8e759d/docs/architecture.md  | 
 | 4 | + | 
 | 5 | +function findFirstMatchingConfig(link, configs) {  | 
 | 6 | +  var i, config  | 
 | 7 | +  if (!link.attrIndex) {  | 
 | 8 | +    return  | 
 | 9 | +  }  | 
 | 10 | +  var href = link.attrs[link.attrIndex('href')][1]  | 
 | 11 | + | 
 | 12 | +  for (i = 0; i < configs.length; ++i) {  | 
 | 13 | +    config = configs[i]  | 
 | 14 | + | 
 | 15 | +    // if there is no pattern, config matches for all links  | 
 | 16 | +    // otherwise, only return config if href matches the pattern set  | 
 | 17 | +    if (!config.pattern || new RegExp(config.pattern).test(href)) {  | 
 | 18 | +      return config  | 
 | 19 | +    }  | 
 | 20 | +  }  | 
 | 21 | +}  | 
 | 22 | + | 
 | 23 | +function applyAttributes(idx, tokens, attributes) {  | 
 | 24 | +  Object.keys(attributes).forEach(function(attr) {  | 
 | 25 | +    var attrIndex  | 
 | 26 | +    var value = attributes[attr]  | 
 | 27 | + | 
 | 28 | +    if (attr === 'className') {  | 
 | 29 | +      // when dealing with applying classes  | 
 | 30 | +      // programatically, some programmers  | 
 | 31 | +      // may prefer to use the className syntax  | 
 | 32 | +      attr = 'class'  | 
 | 33 | +    }  | 
 | 34 | + | 
 | 35 | +    attrIndex = tokens[idx].attrIndex(attr)  | 
 | 36 | + | 
 | 37 | +    if (attrIndex < 0) {  | 
 | 38 | +      // attr doesn't exist, add new attribute  | 
 | 39 | +      tokens[idx].attrPush([attr, value])  | 
 | 40 | +    } else {  | 
 | 41 | +      // attr already exists, overwrite it  | 
 | 42 | +      tokens[idx].attrs[attrIndex][1] = value // replace value of existing attr  | 
 | 43 | +    }  | 
 | 44 | +  })  | 
 | 45 | +}  | 
 | 46 | + | 
 | 47 | +function markdownitLinkAttributes(md, configs) {  | 
 | 48 | +  if (!configs) {  | 
 | 49 | +    configs = []  | 
 | 50 | +  } else {  | 
 | 51 | +    configs = Array.isArray(configs) ? configs : [configs]  | 
 | 52 | +  }  | 
 | 53 | + | 
 | 54 | +  Object.freeze(configs)  | 
 | 55 | + | 
 | 56 | +  var defaultRender = md.renderer.rules.link_open || this.defaultRender  | 
 | 57 | + | 
 | 58 | +  md.renderer.rules.link_open = function(tokens, idx, options, env, self) {  | 
 | 59 | +    var config = findFirstMatchingConfig(tokens[idx], configs)  | 
 | 60 | +    var attributes = config && config.attrs  | 
 | 61 | + | 
 | 62 | +    if (attributes) {  | 
 | 63 | +      applyAttributes(idx, tokens, attributes)  | 
 | 64 | +    }  | 
 | 65 | + | 
 | 66 | +    // pass token to default renderer.  | 
 | 67 | +    return defaultRender(tokens, idx, options, env, self)  | 
 | 68 | +  }  | 
 | 69 | +}  | 
 | 70 | + | 
 | 71 | +markdownitLinkAttributes.defaultRender = function(  | 
 | 72 | +  tokens,  | 
 | 73 | +  idx,  | 
 | 74 | +  options,  | 
 | 75 | +  env,  | 
 | 76 | +  self  | 
 | 77 | +) {  | 
 | 78 | +  return self.renderToken(tokens, idx, options)  | 
 | 79 | +}  | 
 | 80 | + | 
 | 81 | +export default markdownitLinkAttributes  | 
0 commit comments