1 import path from 'path'
2 import fs from 'fs'
3 import remarkFm from 'remark-frontmatter'
4
5 const {NODE_ENV, GATSBY_CONTENT_ALLOW, GATSBY_CONTENT_IGNORE, GATSBY_CONTENT_DIR = 'content'} = process.env
6 const DEV = NODE_ENV === 'development'
7 const CONTENT_DIR = path.resolve(GATSBY_CONTENT_DIR)
8
9 const walkDirs = dir => {
10 const dirs = fs
11 .readdirSync(dir)
12 .filter(d => fs.statSync(path.join(dir, d)).isDirectory())
13 .map(p => path.join(dir, p))
14 const nested = dirs.flatMap(d => walkDirs(d))
15 return [...dirs, ...nested]
16 }
17
18 const getContentOptions = () => {
19 if (!DEV || (!GATSBY_CONTENT_ALLOW && !GATSBY_CONTENT_IGNORE)) {
20 return
21 }
22
23 const allowContent = (GATSBY_CONTENT_ALLOW ?? '').split(',').filter(Boolean)
24 const ignoreContent = (GATSBY_CONTENT_IGNORE ?? '').split(',').filter(Boolean)
25
26 const paths = walkDirs(CONTENT_DIR)
27 .map(p => path.relative(CONTENT_DIR, p))
28 .sort()
29 .reduce(
30 (acc, p) => {
31 const allow = allowContent.length ? allowContent.includes(p) : null
32 const ignore = ignoreContent.length ? ignoreContent.includes(p) : null
33 if (ignore === true || allow === false) {
34 acc.ignore.push(p)
35 } else {
36 acc.include.push(p)
37 }
38 return acc
39 },
40 {include: [], ignore: []},
41 )
42
43 const ignoreGlobs = paths.ignore.map(p => path.join('**', p, '**'))
44
45 console.log(`Only including the following partial content in dev mode`)
46 console.log(`Allow:\n - ${paths.include.join('\n - ')}`)
47 console.log(`Ignore:\n - ${ignoreGlobs.join('\n - ')}`)
48
49 return {
50 ignore: ignoreGlobs,
51 }
52 }
53
54 const config = {
55 trailingSlash: 'never',
56 siteMetadata: {
57 title: 'npm Docs',
58 shortName: 'npm',
59 description: 'Documentation for the npm registry, website, and command-line interface',
60 lang: 'en',
61 imageUrl: 'https://user-images.githubusercontent.com/29712634/81721690-e2fb5d80-9445-11ea-8602-4b2294c964f3.png',
62 repositoryUrl: 'https://github.com/npm/documentation',
63 },
64 flags: {
65 DEV_SSR: !!process.env.GATSBY_DEV_SSR,
66 },
67 plugins: [
68 'gatsby-plugin-postcss',
69 'gatsby-plugin-styled-components',
70 'gatsby-transformer-yaml',
71 {
72 resolve: 'gatsby-plugin-mdx',
73 options: {
74 mdxOptions: {
75 remarkPlugins: [remarkFm],
76 },
77 },
78 },
79 {
80 resolve: 'gatsby-source-filesystem',
81 options: {
82 name: 'content',
83 path: CONTENT_DIR,
84 ...getContentOptions(),
85 },
86 },
87 {
88 resolve: 'gatsby-plugin-manifest',
89 options: {
90 icon: path.resolve('./src/favicon.png'),
91 },
92 },
93 'gatsby-plugin-meta-redirect',
94 ],
95 }
96
97 export default config