Repository: marktext/marktext
File: src/muya/lib/utils/index.js (JavaScript)

Code before:
    const contentType = res.headers['content-type']
    if (res.status === 200 && /^image\/(?:jpeg|png|gif|svg\+xml|webp)$/.test(contentType)) {
      return true
    }
    return false
  } catch (err) {
    return false
  }
}

export const collectImportantComments = css => {
  const once = new Set()
  const cleaned = css.replace(/(\/\*![\s\S]*?\*\/)\n*/gm, (match, p1) => {
    once.add(p1)
    return ''
  })
  const combined = Array.from(once)
  combined.push(cleaned)
  return combined.join('\n')
}

export const getImageInfo = src => {
  const EXT_REG = /\.(jpeg|jpg|png|gif|svg|webp)(?=\?|$)/i
  // http[s] (domain or IPv4 or localhost or IPv6) [port] /not-white-space
  const URL_REG = /^http(s)?:\/\/([a-z0-9\-._~]+\.[a-z]{2,}|[0-9.]+|localhost|\[[a-f0-9.:]+\])(:[0-9]{1,5})?\/[\S]+/i
  const DATA_URL_REG = /data:([\w/+]+);(charset=[\w-]+|base64).*,([a-zA-Z0-9+/]+={0,2})/
  const imageExtension = EXT_REG.test(src)
  const isUrl = URL_REG.test(src)
  if (imageExtension) {
    if (isUrl || !window.DIRNAME) {
      return {
        isUnknownType: false,
        src
      }
    } else if (window && window.process && window.process.type === 'renderer') {
      return {
        isUnknownType: false,
        src: 'file://' + require('path').resolve(window.DIRNAME, src)
      }
    }
  } else if (isUrl && !imageExtension) {
    return {
      isUnknownType: true,
      src
    }
  } else {
    const isDataUrl = DATA_URL_REG.test(src)
    if (isDataUrl) {
      return {
        isUnknownType: false,
        src: src

Review comment (suggestion):
I would recommend the following RegEx:

```js
/^data:image\/[\w+-]+(;[\w-]+=[\w-]+|;base64)*,[a-zA-Z0-9+/]+={0,2}$/
```
so it's compatible with [RFC 2397](https://en.wikipedia.org/wiki/Data_URI_scheme#Syntax): `data:[<media type>][;base64],<data>`.

Code after:
    const contentType = res.headers['content-type']
    if (res.status === 200 && /^image\/(?:jpeg|png|gif|svg\+xml|webp)$/.test(contentType)) {
      return true
    }
    return false
  } catch (err) {
    return false
  }
}

export const collectImportantComments = css => {
  const once = new Set()
  const cleaned = css.replace(/(\/\*![\s\S]*?\*\/)\n*/gm, (match, p1) => {
    once.add(p1)
    return ''
  })
  const combined = Array.from(once)
  combined.push(cleaned)
  return combined.join('\n')
}

export const getImageInfo = src => {
  const EXT_REG = /\.(jpeg|jpg|png|gif|svg|webp)(?=\?|$)/i
  // http[s] (domain or IPv4 or localhost or IPv6) [port] /not-white-space
  const URL_REG = /^http(s)?:\/\/([a-z0-9\-._~]+\.[a-z]{2,}|[0-9.]+|localhost|\[[a-f0-9.:]+\])(:[0-9]{1,5})?\/[\S]+/i
  const DATA_URL_REG = /^data:image\/[\w+-]+(;[\w-]+=[\w-]+|;base64)*,[a-zA-Z0-9+/]+={0,2}$/
  const imageExtension = EXT_REG.test(src)
  const isUrl = URL_REG.test(src)
  if (imageExtension) {
    if (isUrl || !window.DIRNAME) {
      return {
        isUnknownType: false,
        src
      }
    } else if (window && window.process && window.process.type === 'renderer') {
      return {
        isUnknownType: false,
        src: 'file://' + require('path').resolve(window.DIRNAME, src)
      }
    }
  } else if (isUrl && !imageExtension) {
    return {
      isUnknownType: true,
      src
    }
  } else {
    const isDataUrl = DATA_URL_REG.test(src)
    if (isDataUrl) {
      return {
        isUnknownType: false,
        src