|
1 | | -const path = require('path') |
| 1 | +const {resolve, join: _join, relative} = require('path') |
| 2 | +const join = (...paths) => _join(...paths).replace(/\\/g, '/') |
2 | 3 |
|
3 | | -const DEV = process.env.NODE_ENV === 'development' |
4 | 4 | const SHOW_CONTRIBUTORS = false |
5 | 5 | const REPO = { |
6 | 6 | url: 'https://github.com/npm/documentation', |
@@ -32,7 +32,7 @@ exports.onCreateWebpackConfig = ({actions}) => { |
32 | 32 | actions.setWebpackConfig({ |
33 | 33 | resolve: { |
34 | 34 | alias: { |
35 | | - '~': path.resolve(__dirname, 'src/'), |
| 35 | + '~': resolve(__dirname, 'src/'), |
36 | 36 | }, |
37 | 37 | extensions: ['.js'], |
38 | 38 | }, |
@@ -61,8 +61,6 @@ exports.createSchemaCustomization = ({actions: {createTypes}}) => { |
61 | 61 | } |
62 | 62 |
|
63 | 63 | exports.createPages = async ({graphql, actions}) => { |
64 | | - const rootAbsolutePath = process.cwd() |
65 | | - |
66 | 64 | const {data} = await graphql(` |
67 | 65 | { |
68 | 66 | allMdx { |
@@ -96,82 +94,71 @@ exports.createPages = async ({graphql, actions}) => { |
96 | 94 | `) |
97 | 95 |
|
98 | 96 | // Turn every MDX file into a page. |
99 | | - return Promise.all( |
100 | | - data.allMdx.nodes.map(async node => { |
101 | | - const { |
102 | | - id, |
103 | | - frontmatter, |
104 | | - internal: {contentFilePath}, |
105 | | - tableOfContents = {}, |
106 | | - } = node |
| 97 | + return Promise.all(data.allMdx.nodes.map(node => createPage(node, actions))) |
| 98 | +} |
107 | 99 |
|
108 | | - const pagePath = getPath(node) |
109 | | - const relativePath = path.relative(rootAbsolutePath, contentFilePath) |
110 | | - const editUrl = getEditUrl(REPO, relativePath, frontmatter) |
| 100 | +async function createPage( |
| 101 | + { |
| 102 | + id, |
| 103 | + internal: {contentFilePath}, |
| 104 | + fields: {slug} = {}, |
| 105 | + frontmatter = {}, |
| 106 | + tableOfContents = {}, |
| 107 | + parent: {relativeDirectory, name: parentName}, |
| 108 | + }, |
| 109 | + actions, |
| 110 | +) { |
| 111 | + // sites can programmatically override slug, that takes priority |
| 112 | + // then a slug specified in frontmatter |
| 113 | + // finally, we'll just use the path on disk |
| 114 | + const pagePath = slug ?? frontmatter.slug ?? join(relativeDirectory, parentName === 'index' ? '/' : parentName) |
111 | 115 |
|
112 | | - const contributors = SHOW_CONTRIBUTORS ? await fetchContributors(REPO, relativePath, frontmatter) : {} |
| 116 | + const relativePath = relative(process.cwd(), contentFilePath) |
113 | 117 |
|
114 | | - // Fix some old CLI pages which have mismatched headings at the top level. |
115 | | - // All top level headings should be the same level. |
116 | | - const toc = tableOfContents.items?.reduce((acc, item) => { |
117 | | - if (!item.url && Array.isArray(item.items)) { |
118 | | - acc.push(...item.items) |
119 | | - } else { |
120 | | - acc.push(item) |
121 | | - } |
122 | | - return acc |
123 | | - }, []) |
| 118 | + const editUrl = getEditUrl(REPO, relativePath, frontmatter) |
124 | 119 |
|
125 | | - actions.createPage({ |
126 | | - path: pagePath, |
127 | | - component: contentFilePath, |
128 | | - context: { |
129 | | - mdxId: id, |
130 | | - editUrl, |
131 | | - contributors, |
132 | | - tableOfContents: toc, |
133 | | - repositoryUrl: REPO.url, |
134 | | - }, |
135 | | - }) |
136 | | - |
137 | | - if (!DEV) { |
138 | | - for (const from of frontmatter.redirect_from ?? []) { |
139 | | - actions.createRedirect({ |
140 | | - fromPath: from, |
141 | | - toPath: `/${pagePath}`, |
142 | | - isPermanent: true, |
143 | | - redirectInBrowser: true, |
144 | | - }) |
| 120 | + const contributors = SHOW_CONTRIBUTORS ? await fetchContributors(REPO, relativePath, frontmatter) : {} |
145 | 121 |
|
146 | | - if (pagePath.startsWith('cli/') && !from.endsWith('index')) { |
147 | | - actions.createRedirect({ |
148 | | - fromPath: `${from}.html`, |
149 | | - toPath: `/${pagePath}`, |
150 | | - isPermanent: true, |
151 | | - redirectInBrowser: true, |
152 | | - }) |
153 | | - } |
154 | | - } |
155 | | - } |
156 | | - }), |
157 | | - ) |
158 | | -} |
| 122 | + // Fix some old CLI pages which have mismatched headings at the top level. |
| 123 | + // All top level headings should be the same level. |
| 124 | + const toc = tableOfContents.items?.reduce((acc, item) => { |
| 125 | + if (!item.url && Array.isArray(item.items)) { |
| 126 | + acc.push(...item.items) |
| 127 | + } else { |
| 128 | + acc.push(item) |
| 129 | + } |
| 130 | + return acc |
| 131 | + }, []) |
| 132 | + |
| 133 | + actions.createPage({ |
| 134 | + path: pagePath, |
| 135 | + component: contentFilePath, |
| 136 | + context: { |
| 137 | + mdxId: id, |
| 138 | + editUrl, |
| 139 | + contributors, |
| 140 | + tableOfContents: toc, |
| 141 | + repositoryUrl: REPO.url, |
| 142 | + }, |
| 143 | + }) |
159 | 144 |
|
160 | | -function getPath(node) { |
161 | | - // sites can programmatically override slug, that takes priority |
162 | | - if (node.fields && node.fields.slug) { |
163 | | - return node.fields.slug |
164 | | - } |
| 145 | + for (const from of frontmatter.redirect_from ?? []) { |
| 146 | + actions.createRedirect({ |
| 147 | + fromPath: from, |
| 148 | + toPath: `/${pagePath}`, |
| 149 | + isPermanent: true, |
| 150 | + redirectInBrowser: true, |
| 151 | + }) |
165 | 152 |
|
166 | | - // then a slug specified in frontmatter |
167 | | - if (node.frontmatter && node.frontmatter.slug) { |
168 | | - return node.frontmatter.slug |
| 153 | + if (pagePath.startsWith('cli/') && !from.endsWith('index')) { |
| 154 | + actions.createRedirect({ |
| 155 | + fromPath: `${from}.html`, |
| 156 | + toPath: `/${pagePath}`, |
| 157 | + isPermanent: true, |
| 158 | + redirectInBrowser: true, |
| 159 | + }) |
| 160 | + } |
169 | 161 | } |
170 | | - |
171 | | - // finally, we'll just use the path on disk |
172 | | - return path |
173 | | - .join(node.parent.relativeDirectory, node.parent.name === 'index' ? '/' : node.parent.name) |
174 | | - .replace(/\\/g, '/') // Windows paths to forward slashes |
175 | 162 | } |
176 | 163 |
|
177 | 164 | function getGitHubData(repo, overrideData, filePath) { |
@@ -204,8 +191,8 @@ function getEditUrl(repo, filePath, overrideData = {}) { |
204 | 191 | return null |
205 | 192 | } |
206 | 193 |
|
207 | | - const gh = getGitHubData(repo, overrideData, filePath) |
208 | | - return `https://github.com/${gh.nwo}/edit/${gh.branch}/${gh.path}` |
| 194 | + const {nwo, branch, path} = getGitHubData(repo, overrideData, filePath) |
| 195 | + return `https://github.com/${nwo}/edit/${branch}/${path}` |
209 | 196 | } |
210 | 197 |
|
211 | 198 | const CONTRIBUTOR_CACHE = new Map() |
|
0 commit comments