add-base-to-md.js 1021 字节
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5
module.exports = function (md, { base = '/' }) {
  if (base !== '/') {
    md.core.ruler.after('inline', 'add-base-to-md', function (state) {
      state.tokens.forEach(function (blockToken) {
        if (blockToken.type === 'inline' && blockToken.content && blockToken.children && blockToken.children.length) {
D
DCloud_LXH 已提交
6
          if (blockToken.content.indexOf('](') > -1) {
D
DCloud_LXH 已提交
7 8 9
            blockToken.children.forEach(function (inlineToken) {
              if (inlineToken.type === 'link_open' && inlineToken.attrs && inlineToken.attrs.length) {
                inlineToken.attrs.forEach(function (attr) {
D
DCloud_LXH 已提交
10 11 12 13
                  if (attr[0] === 'href') {
                    // /abc/def.md => ${base}/abc/def.md
                    if (attr[1].indexOf('/') === 0 && attr[1].indexOf(base) !== 0) attr[1] = base + attr[1].slice(1)
                    if (/^\w/.test(attr[1])) attr[1] = './' + attr[1]
D
DCloud_LXH 已提交
14 15 16 17 18 19 20 21 22 23 24
                  }
                });
              }
            })
          }
        }
      });
      return false;
    });
  }
};