inject-json-to-md.js 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
let cssJson = {}
let utsJson = {}
let utsApiJson = {}
let utsComJson = {}
let utsUnicloudApiJson = {}
let customTypeJson = {}
let vueJson = {}
let manifestJson = {}
let pagesJson = {}
let specialStringJson = {}
let pageInstanceJson = {}
D
DCloud_LXH 已提交
12
try {
13
  cssJson = require('../utils/cssJson.json')
D
DCloud_LXH 已提交
14 15
} catch (error) {}
try {
16
  utsJson = require('../utils/utsJson.json')
D
DCloud_LXH 已提交
17 18
} catch (error) {}
try {
19
  utsApiJson = require('../utils/utsApiJson.json')
D
DCloud_LXH 已提交
20 21
} catch (error) {}
try {
22
  utsComJson = require('../utils/utsComJson.json')
D
DCloud_LXH 已提交
23 24
} catch (error) {}
try {
25
  utsUnicloudApiJson = require('../utils/utsUnicloudApiJson.json')
D
DCloud_LXH 已提交
26 27
} catch (error) {}
try {
28
  customTypeJson = require('../utils/customTypeJson.json')
D
DCloud_LXH 已提交
29 30
} catch (error) {}
try {
31
  vueJson = require('../utils/vueJson.json')
D
DCloud_LXH 已提交
32 33
} catch (error) {}
try {
34
  manifestJson = require('../utils/manifestJson.json')
D
DCloud_LXH 已提交
35 36
} catch (error) {}
try {
37
  pagesJson = require('../utils/pagesJson.json')
D
DCloud_LXH 已提交
38 39
} catch (error) {}
try {
40
  specialStringJson = require('../utils/specialStringJson.json')
D
DCloud_LXH 已提交
41 42
} catch (error) {}
try {
43
  pageInstanceJson = require('../utils/pageInstanceJson.json')
D
DCloud_LXH 已提交
44 45 46
} catch (error) {}

function getRegExp(key) {
47
  return new RegExp(`<!--\\s*${key}.([\\w\\W]+[^\\s])\\s*-->`)
D
DCloud_LXH 已提交
48 49
}

50 51 52 53 54
/**
 *
 * @param {string} text
 * @returns {{match: RegExpMatchArray | null, json: {}}
 */
D
DCloud_LXH 已提交
55
const getJSON = text => {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  let match = text.match(getRegExp('CSSJSON'))
  if (match) {
    return {
      match,
      json: cssJson,
    }
  }

  match = text.match(getRegExp('UTSJSON'))
  if (match) {
    return {
      match,
      json: utsJson,
    }
  }

  match = text.match(getRegExp('UTSAPIJSON'))
  if (match) {
    return {
      match,
      json: utsApiJson,
    }
  }

  match = text.match(getRegExp('UTSCOMJSON'))
  if (match) {
    return {
      match,
      json: utsComJson,
    }
  }

  match = text.match(getRegExp('UTSUNICLOUDAPIJSON'))
  if (match) {
    return {
      match,
      json: utsUnicloudApiJson,
    }
  }

  match = text.match(getRegExp('CUSTOMTYPEJSON'))
  if (match) {
    return {
      match,
      json: customTypeJson,
    }
  }

  match = text.match(getRegExp('VUEJSON'))
  if (match) {
    return {
      match,
      json: vueJson,
    }
  }

  match = text.match(getRegExp('MANIFESTJSON'))
  if (match) {
    return {
      match,
      json: manifestJson,
    }
  }

  match = text.match(getRegExp('PAGESJSON'))
  if (match) {
    return {
      match,
      json: pagesJson,
    }
  }

  match = text.match(getRegExp('SPECIALSTRINGJSON'))
  if (match) {
    return {
      match,
      json: specialStringJson,
    }
  }

  match = text.match(getRegExp('PAGEINSTANCE'))
  if (match) {
    return {
      match,
      json: pageInstanceJson,
    }
  }

  return {
    match: null,
    json: {},
  }
}

const NEWLINE_CHARACTER = /\r?\n/

module.exports = md => {
  md.parse = (function (MD_PARSE) {
    return function (src, ...args) {
      if (src && getJSON(src).match) {
        const lines = src.split(NEWLINE_CHARACTER)
        for (let index = 0; index < lines.length; index++) {
          const line = lines[index]

          const { match, json } = getJSON(line)
          if (match) {
            const jsonPath = match[1]
            const path = jsonPath.split('.')
            let temp = json
            path.forEach(key => {
              if (!temp) return false
              temp = temp[key]
            })
169
            if (typeof temp === 'undefined') continue
170 171 172 173 174 175 176 177 178 179
            lines[index] = temp
          }
        }

        return MD_PARSE.bind(this)(lines.join('\n'), ...args)
      }
      return MD_PARSE.bind(this)(src, ...args)
    }
  })(md.parse)
}