const path = require('path');
const { slugify } = require('@vuepress/shared-utils')
const highlight = require('@vuepress/markdown/lib/highlight')
const createSidebar = require('./markdown/createSidebar')
const { simplifySlugText } = require('./utils')
const copyOptions = require('./config/copy');

const base = '/uni-app-x/'

const config = {
  base,
  theme: 'vuepress-theme-uniapp-official',
  title: 'uni-app-x',
  description: 'uni-app-x Document',
  evergreen: process.env.NODE_ENV === 'development',
  head: [
    ['link', {
      rel: 'shortcut icon',
      type: 'image/x-icon',
      href: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/logo.ico'
    }],
    ['meta', {
      name: 'viewport', content: 'width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0'
    }],
    ['meta', {
      name: 'keywords', content: '数字天堂,前端开发,web开发,小程序开发,跨平台,跨平台开发,跨端开发,混合开发,app开发,多端开发,开发工具,HTML5,vue,react,native,rn,flutter,weex,cordova,微信小程序,阿里小程序,支付宝小程序,百度小程序,头条小程序,抖音小程序,QQ小程序,快应用,流应用,云函数'
    }],
    ['script', { src: 'https://hm.baidu.com/hm.js?fe3b7a223fc08c795f0f4b6350703e6f' }],
    ['script', { src: '/miku-delivery-1.2.1.js' }],
    ['script', { src: `/js/miku.js?${Date.now()}&v=${Date.now()}&version=${Date.now()}` }]
  ],
  locales: {
    '/': {
      lang: 'zh-CN',
    }
  },
  themeConfig: {
    titleLogo: 'https://web-ext-storage.dcloud.net.cn/uni-app-x/logo.ico',
    logo: 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/logo.png',
    sidebar: createSidebar(),
    sidebarDepth: 0,
    nextLinks: false,
    prevLinks: false,
    repo: 'dcloudio/uni-app',
    docsRepo: 'https://gitcode.net/dcloud/unidocs-uni-app-x-zh',
    docsBranch: 'master',
    docsDir: 'docs',
    editLinks: true,
    editLinkText: '帮助我们改善此页面！',
    lastUpdated: '上次更新',
    // smoothScroll: true,
    search: false,
    algolia: {
      apiKey: '2fdcc4e76c8e260671ad70065e60b2e7',
      indexName: 'zh-uniapp',
      appId: 'PQIR5NL8CZ',
      searchParameters: { hitsPerPage: 50 }
    },
    isDevelopment: process.env.NODE_ENV === 'development'
  },
  markdown: {
    // toc: { includeLevel: [1, 2, 3, 4] },
    slugify (str) {
      if (typeof str !== 'string') return ''

      let slug = str
      if (slug.includes('@')) {
        let array = slug.split('@')
        slug = array.length > 1 ? array[array.length - 1] : str
      } else {
        slug = simplifySlugText(slug.toLowerCase()).trim()
      }
      return slugify(slug)
    },
    extractHeaders: ['h1', 'h2', 'h3', 'h4'],
    chainMarkdown (config) {
      const extensionMap = {
        uts: 'ts',
        uvue: 'vue'
      }
      config.options.highlight((str, lang) => {
        const extension = extensionMap[lang]
        return highlight(str, extension || lang)
      })

      config
        .plugin('translate')
        .use(require('./markdown/translate'))
        .end()
        .plugin('convert-header')
        .use( require('./markdown/header'))
        .end()
        .plugin('normalize-link')
        .use(require('./markdown/normalizeLink'))
        .end()
				.plugin('img-add-attrs')
				.use(require('./markdown/img-add-attrs'))
        .end()
        .plugin('inject-json-to-md')
        .use(require('./markdown/inject-json-to-md'))
        .end()
        .plugin('add-base-to-md')
        .use(require('./markdown/add-base-to-md'), [{ base }])
    }
  },
  chainWebpack (config, isServer) {
    config.resolve.alias.set(
      '@theme-config',
      path.resolve(process.cwd(), 'docs/.vuepress/config')
    )
  },
  plugins: [
    ["vuepress-plugin-juejin-style-copy", copyOptions]
  ]
}

module.exports = config
