config.js 6.4 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5 6
const path = require('path');
const { slugify } = require('@vuepress/shared-utils')
const highlight = require('@vuepress/markdown/lib/highlight')
const translatePlugin = require('./markdown/translate')
const headerPlugin = require('./markdown/header')
const createSidebar = require('./markdown/createSidebar')
7
const { simplifySlugText, tabs, getFormattedDate } = require('./utils')
D
DCloud_LXH 已提交
8 9
const copyOptions = require('./config/copy');

10 11 12 13 14 15
const nowString = getFormattedDate();
const changeLoaderOptions = (options, key = 'name') => {
	if (options && options[key]) options[key] = `${nowString}/${options[key]}`;
	return options;
};

D
DCloud_LXH 已提交
16 17
const base = '/uni-app-x/'

D
DCloud_LXH 已提交
18
const config = {
D
DCloud_LXH 已提交
19
  base,
D
DCloud_LXH 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  theme: 'vuepress-theme-uni-app-test',
  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://qiniu-web-assets.dcloud.net.cn/unidoc/zh/icon.png?v=1556263038788'
    }],
    ['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://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png',
    logo: 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/logo.png',
    sidebar: createSidebar(tabs),
    sidebarDepth: 0,
    nextLinks: false,
    prevLinks: false,
    repo: 'dcloudio/uni-app',
D
DCloud_LXH 已提交
53
    docsRepo: 'https://gitcode.net/dcloud/unidocs-uni-app-x-zh',
D
DCloud_LXH 已提交
54 55 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
    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'
      }
      config.options.highlight((str, lang) => {
        const extension = extensionMap[lang]
        return highlight(str, extension || lang)
      })

      config
        .plugin('translate')
        .use(translatePlugin)
        .end()
        .plugin('convert-header')
        .use(headerPlugin)
        .end()
        .plugin('normallize-link')
        .use(require('./markdown/normallizeLink'))
        .end()
				.plugin('img-add-attrs')
				.use(require('./markdown/img-add-attrs'))
        .end()
				.plugin('attrs')
        .use(require('markdown-it-attrs'),[{
          leftDelimiter: '#{',
          rightDelimiter: '}'
        }])
        .end()
        .plugin('task-lists')
        .use(require('markdown-it-task-lists'))
        .end()
        .plugin('inject-json-to-md')
        .use(require('./markdown/inject-json-to-md'))
        .end()
        .plugin('markdown-it-raw-table')
        .use(require('markdown-it-raw-table'))
D
DCloud_LXH 已提交
120 121 122
        .end()
        .plugin('add-base-to-md')
        .use(require('./markdown/add-base-to-md'), [{ base }])
D
DCloud_LXH 已提交
123 124 125 126 127 128 129
    }
  },
  chainWebpack (config, isServer) {
    config.resolve.alias.set(
      '@theme-config',
      path.resolve(process.cwd(), 'docs/.vuepress/config')
    )
D
DCloud_LXH 已提交
130 131 132 133 134
    config.output.filename(`${nowString}/${config.output.get('filename')}`); //输出文件名
    config.module.rule('images').use('url-loader').tap(changeLoaderOptions);
    config.module.rule('fonts').use('url-loader').tap(changeLoaderOptions);
    config.module.rule('media').use('url-loader').tap(changeLoaderOptions);
    config.module.rule('svg').use('file-loader').tap(changeLoaderOptions);
D
DCloud_LXH 已提交
135
    if (!isServer && process.env.NODE_ENV === 'production') {
136 137 138 139 140 141 142 143 144 145 146
      const extract_css_plugin = config.plugin('extract-css');
      const extract_css_plugin_args = extract_css_plugin.get('args');
      if (extract_css_plugin_args) {
        extract_css_plugin.set(
          'args',
          extract_css_plugin_args.map(item =>
            changeLoaderOptions(item, 'filename')
          )
        );
      }
    }
D
DCloud_LXH 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  },
  patterns: ['**/!(_sidebar).md', '**/*.vue'],
  plugins: [
    ["vuepress-plugin-juejin-style-copy", copyOptions],
    [
      'named-chunks',
      {
        layoutChunkName: (layout) => 'layout-' + layout.componentName,
        pageChunkName: page => {
          const _context = page._context
          const pageHeaders = (page.headers || []).map(item => item.title).join(',')
          if (pageHeaders) {
            const originDescription = page.frontmatter.description || ''
            page.frontmatter = {
              ...page.frontmatter,
              description: `${_context.siteConfig.description ? `${_context.siteConfig.description},` : ''}${pageHeaders}${originDescription ? `,${originDescription}` : ''}`.slice(0, 150),
            }
          }
          const pagePath = page.path.indexOf('.html') === -1 ? page.path + 'index' : page.path
          const curPath = 'docs/' + pagePath.replace('docs/', '').substring(1).replace(/\.html/g, "")
          return curPath
        }
      }
    ]
  ],
  /**
   *
   * @param {string} path path: js 资源文件路径
   * @param {string} type type: 资源文件类型,取值有 script 等
   * @returns
   */
  shouldPrefetch: (path, type) => {
    if (type === 'script') return path.includes('vendors~') || path.includes('layout-') || path.includes('index.')
    return false
  }
}

module.exports = config