inject-json-to-md.js 4.8 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
} catch (error) {}

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

50 51 52
/**
 *
 * @param {string} text
D
DCloud_LXH 已提交
53
 * @returns {{match: RegExpMatchArray | null, json: {}, regExp: RegExp | null}
54
 */
D
DCloud_LXH 已提交
55
const getJSON = text => {
D
DCloud_LXH 已提交
56 57 58
  const CSSJSONRegExp = getRegExp('CSSJSON')
  let match = text.match(CSSJSONRegExp)
  CSSJSONRegExp.lastIndex = 0
59 60 61 62
  if (match) {
    return {
      match,
      json: cssJson,
D
DCloud_LXH 已提交
63
      regExp: CSSJSONRegExp,
64 65 66
    }
  }

D
DCloud_LXH 已提交
67 68 69 70

  const UTSJSONRegExp = getRegExp('UTSJSON')
  match = text.match(UTSJSONRegExp)
  UTSJSONRegExp.lastIndex = 0
71 72 73 74
  if (match) {
    return {
      match,
      json: utsJson,
D
DCloud_LXH 已提交
75
      regExp: UTSJSONRegExp,
76 77 78
    }
  }

D
DCloud_LXH 已提交
79 80 81
  const UTSAPIJSONRegExp = getRegExp('UTSAPIJSON')
  match = text.match(UTSAPIJSONRegExp)
  UTSAPIJSONRegExp.lastIndex = 0
82 83 84 85
  if (match) {
    return {
      match,
      json: utsApiJson,
D
DCloud_LXH 已提交
86
      regExp: UTSAPIJSONRegExp,
87 88 89
    }
  }

D
DCloud_LXH 已提交
90 91 92
  const UTSCOMJSONRegExp = getRegExp('UTSCOMJSON')
  match = text.match(UTSCOMJSONRegExp)
  UTSCOMJSONRegExp.lastIndex = 0
93 94 95 96
  if (match) {
    return {
      match,
      json: utsComJson,
D
DCloud_LXH 已提交
97
      regExp: UTSCOMJSONRegExp,
98 99 100
    }
  }

D
DCloud_LXH 已提交
101 102 103
  const UTSUNICLOUDAPIJSONRegExp = getRegExp('UTSUNICLOUDAPIJSON')
  match = text.match(UTSUNICLOUDAPIJSONRegExp)
  UTSUNICLOUDAPIJSONRegExp.lastIndex = 0
104 105 106 107
  if (match) {
    return {
      match,
      json: utsUnicloudApiJson,
D
DCloud_LXH 已提交
108
      regExp: UTSUNICLOUDAPIJSONRegExp,
109 110 111
    }
  }

D
DCloud_LXH 已提交
112 113 114
  const CUSTOMTYPEJSONRegExp = getRegExp('CUSTOMTYPEJSON')
  match = text.match(CUSTOMTYPEJSONRegExp)
  CUSTOMTYPEJSONRegExp.lastIndex = 0
115 116 117 118
  if (match) {
    return {
      match,
      json: customTypeJson,
D
DCloud_LXH 已提交
119
      regExp: CUSTOMTYPEJSONRegExp,
120 121 122
    }
  }

D
DCloud_LXH 已提交
123 124 125
  const VUEJSONRegExp = getRegExp('VUEJSON')
  match = text.match(VUEJSONRegExp)
  VUEJSONRegExp.lastIndex = 0
126 127 128 129
  if (match) {
    return {
      match,
      json: vueJson,
D
DCloud_LXH 已提交
130
      regExp: VUEJSONRegExp,
131 132 133
    }
  }

D
DCloud_LXH 已提交
134 135 136
  const MANIFESTJSONRegExp = getRegExp('MANIFESTJSON')
  match = text.match(MANIFESTJSONRegExp)
  MANIFESTJSONRegExp.lastIndex = 0
137 138 139 140
  if (match) {
    return {
      match,
      json: manifestJson,
D
DCloud_LXH 已提交
141
      regExp: MANIFESTJSONRegExp,
142 143 144
    }
  }

D
DCloud_LXH 已提交
145 146 147
  const PAGESJSONRegExp = getRegExp('PAGESJSON')
  match = text.match(PAGESJSONRegExp)
  PAGESJSONRegExp.lastIndex = 0
148 149 150 151
  if (match) {
    return {
      match,
      json: pagesJson,
D
DCloud_LXH 已提交
152
      regExp: PAGESJSONRegExp,
153 154 155
    }
  }

D
DCloud_LXH 已提交
156 157 158
  const SPECIALSTRINGJSONRegExp = getRegExp('SPECIALSTRINGJSON')
  match = text.match(SPECIALSTRINGJSONRegExp)
  SPECIALSTRINGJSONRegExp.lastIndex = 0
159 160 161 162
  if (match) {
    return {
      match,
      json: specialStringJson,
D
DCloud_LXH 已提交
163
      regExp: SPECIALSTRINGJSONRegExp,
164 165 166
    }
  }

D
DCloud_LXH 已提交
167 168 169
  const PAGEINSTANCERegExp = getRegExp('PAGEINSTANCE')
  match = text.match(PAGEINSTANCERegExp)
  PAGEINSTANCERegExp.lastIndex = 0
170 171 172 173
  if (match) {
    return {
      match,
      json: pageInstanceJson,
D
DCloud_LXH 已提交
174
      regExp: PAGEINSTANCERegExp,
175 176 177 178 179 180
    }
  }

  return {
    match: null,
    json: {},
D
DCloud_LXH 已提交
181
    regExp: null,
182 183 184 185 186 187 188 189 190 191 192 193 194
  }
}

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]

D
DCloud_LXH 已提交
195 196
          const { match, json, regExp } = getJSON(line)
          if (match && regExp) {
197 198 199 200 201 202 203
            const jsonPath = match[1]
            const path = jsonPath.split('.')
            let temp = json
            path.forEach(key => {
              if (!temp) return false
              temp = temp[key]
            })
204
            if (typeof temp === 'undefined') continue
205 206 207 208 209 210 211 212 213 214
            lines[index] = temp
          }
        }

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