All files / koa/util tools.js

15.79% Statements 3/19
0% Branches 0/10
0% Functions 0/3
15.79% Lines 3/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32    5x     5x                       5x                            
'use strict'
 
const _ = require('lodash')
 
// 生成随机字符串
exports.randomString = function (len) {
  len = len || 12
  const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
  const maxPos = $chars.length
  let pwd = ''
  for (let i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
  }
  return pwd
}
 
// 从markdown中提取图片
exports.extractImage = function (content) {
  let results = []
  const images = content.match(/(!\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g)
  if (_.isArray(images) && images.length > 0) {
    for (let i = 0, j = images.length; i < j; i++) {
      var url = images[i].replace(/(!\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/, function ($1, m1, m2, m3, m4) {
        return m4 || ''
      })
      if (url !== '') {
        results.push({ url: url })
      }
    }
  }
  return results
}