Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.

Commit 330769d

Browse files
feat: terraform content split (#2228)
* feat: terraform content-spli * chore: remove terraform submodule * chore: NAV_DATA_DIRNAME + CONTENT_DIRNAME * chore: update cdktf * chore: temporarily disable pagesDirs pruning * chore: update content-repo-preview script * chore: update preview images * chore: update error message * add `remarkRewriteAssets` * rename arg field to `getAssetPathParts` * chore: update `start` to use `next dev` * chore: remark-rewrite-assets logOnce
1 parent ec7a22c commit 330769d

24 files changed

+306
-232
lines changed

content/cli

-1
This file was deleted.

content/configuration

-1
This file was deleted.

content/guides

-1
This file was deleted.

content/internals

-1
This file was deleted.

content/intro

-1
This file was deleted.

content/language

-1
This file was deleted.

data/cli-nav-data.json

-1
This file was deleted.

data/configuration-nav-data.json

-1
This file was deleted.

data/guides-nav-data.json

-1
This file was deleted.

data/internals-nav-data.json

-1
This file was deleted.

data/intro-nav-data.json

-1
This file was deleted.

data/language-nav-data.json

-1
This file was deleted.

ext/terraform

Submodule terraform deleted from b022c38

lib/remark-rewrite-assets.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as path from 'path'
2+
import visit from 'unist-util-visit'
3+
4+
import type { Plugin } from 'unified'
5+
import type { Image } from 'mdast'
6+
7+
/**
8+
* This is a generator function that returns a remark plugin
9+
* to rewrite asset urls in markdown files.
10+
*/
11+
export function remarkRewriteAssets(args: {
12+
product: string
13+
version: string
14+
getAssetPathParts?: (nodeUrl: string) => string[]
15+
}): Plugin {
16+
const { product, version, getAssetPathParts = (nodeUrl) => [nodeUrl] } = args
17+
18+
return function plugin() {
19+
return function transform(tree) {
20+
visit<Image>(tree, 'image', (node) => {
21+
const originalUrl = node.url
22+
const asset = path.posix.join(...getAssetPathParts(originalUrl))
23+
24+
const url = new URL('https://mktg-content-api.vercel.app/api/assets')
25+
url.searchParams.append('asset', asset)
26+
url.searchParams.append('version', version)
27+
url.searchParams.append('product', product)
28+
29+
node.url = url.toString()
30+
logOnce(
31+
node.url,
32+
`Rewriting asset url for local preview:
33+
- Found: ${originalUrl}
34+
- Replaced with: ${node.url}
35+
36+
If this is a net-new asset, you'll need to commit and push it to GitHub.\n`
37+
)
38+
})
39+
}
40+
}
41+
}
42+
43+
// A simple cache & util to prevent logging the same message multiple times
44+
const cache = new Map<string, boolean>()
45+
const logOnce = (id: string, message: string) => {
46+
if (cache.get(id)) return
47+
cache.set(id, true)
48+
console.log(message)
49+
}

next.config.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ module.exports = withHashicorp({
2121
BUGSNAG_CLIENT_KEY: 'eab8d5350ab3b12d77b7498b23f9a89a',
2222
BUGSNAG_SERVER_KEY: '1f55a49019f70f94a17dd6d93210f09d',
2323
IS_CONTENT_PREVIEW: process.env.IS_CONTENT_PREVIEW || false,
24-
NAV_DATA_PATH: process.env.NAV_DATA_PATH,
25-
CONTENT_DIR: process.env.CONTENT_DIR,
24+
PREVIEW_FROM_REPO: process.env.PREVIEW_FROM_REPO,
25+
NAV_DATA_DIRNAME: process.env.NAV_DATA_DIRNAME || '',
26+
CONTENT_DIRNAME: process.env.CONTENT_DIRNAME || '',
27+
CURRENT_GIT_BRANCH: process.env.CURRENT_GIT_BRANCH || 'main',
2628
},
2729
images: {
2830
domains: ['www.datocms-assets.com'],

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"generate:component": "next-hashicorp generate component",
8181
"generate:readme": "next-hashicorp markdown-blocks README.md",
8282
"lint": "next-hashicorp lint",
83-
"start": "next-remote-watch './content/**/*.mdx'",
83+
"start": "next dev",
8484
"static": "npm run build && npm run export",
8585
"linkcheck": "linkcheck <website url>"
8686
}

pages/cdktf/[[...page]].jsx

+21-28
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import DocsPage from '@hashicorp/react-docs-page'
33
import otherDocsData from 'data/other-docs-nav-data.json'
44
// Imports below are only used server-side
55
import { getStaticGenerationFunctions } from '@hashicorp/react-docs-page/server'
6-
import visit from 'unist-util-visit'
76
import path from 'path'
87

8+
import { remarkRewriteAssets } from 'lib/remark-rewrite-assets'
9+
910
// Configure the docs path
1011
const BASE_ROUTE = 'cdktf'
11-
const NAV_DATA = process.env.NAV_DATA_PATH || '../data/cdktf-nav-data.json'
12-
const CONTENT_DIR = process.env.CONTENT_DIR || '../docs/cdktf'
12+
const NAV_DATA = path.join(
13+
process.env.NAV_DATA_DIRNAME,
14+
BASE_ROUTE + '-nav-data.json'
15+
)
16+
const CONTENT_DIR = path.join(process.env.CONTENT_DIRNAME, BASE_ROUTE)
1317
const PRODUCT = { name: productName, slug: 'terraform-cdk' }
1418

1519
export default function CDKLayout(props) {
@@ -27,7 +31,8 @@ export default function CDKLayout(props) {
2731
}
2832

2933
const { getStaticPaths, getStaticProps } = getStaticGenerationFunctions(
30-
process.env.IS_CONTENT_PREVIEW
34+
process.env.IS_CONTENT_PREVIEW &&
35+
process.env.PREVIEW_FROM_REPO === 'terraform-cdk'
3136
? {
3237
strategy: 'fs',
3338
basePath: BASE_ROUTE,
@@ -40,30 +45,18 @@ const { getStaticPaths, getStaticProps } = getStaticGenerationFunctions(
4045
return `https://github.com/hashicorp/${PRODUCT.slug}/blob/main/website/${filepath}`
4146
},
4247
remarkPlugins: (params) => [
43-
() => {
44-
const product = PRODUCT.slug
45-
const version = 'main'
46-
return function transform(tree) {
47-
visit(tree, 'image', (node) => {
48-
const originalUrl = node.url
49-
const assetPath = params.page
50-
? path.posix.join(
51-
...params.page,
52-
node.url.startsWith('.')
53-
? `.${node.url}`
54-
: `../${node.url}`
55-
)
56-
: node.url
57-
const asset = path.posix.join('website/docs/cdktf', assetPath)
58-
node.url = `https://mktg-content-api.vercel.app/api/assets?product=${product}&version=${version}&asset=${asset}`
59-
console.log(`Rewriting asset url for local preview:
60-
- Found: ${originalUrl}
61-
- Replaced with: ${node.url}
62-
63-
If this is a net-new asset, it may not be available in the preview yet.`)
64-
})
65-
}
66-
},
48+
remarkRewriteAssets({
49+
product: PRODUCT.slug,
50+
version: process.env.CURRENT_GIT_BRANCH,
51+
getAssetPathParts: (nodeUrl) =>
52+
params.page
53+
? [
54+
'website/docs/cdktf',
55+
...params.page,
56+
nodeUrl.startsWith('.') ? `.${nodeUrl}` : `../${nodeUrl}`,
57+
]
58+
: ['website/docs/cdktf', nodeUrl],
59+
}),
6760
],
6861
}
6962
: {

pages/cli/[[...page]].jsx

+38-27
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import { productName, productSlug } from 'data/metadata'
22
import DocsPage from '@hashicorp/react-docs-page'
33
import otherDocsData from 'data/other-docs-nav-data.json'
44
// Imports below are only used server-side
5-
import {
6-
generateStaticPaths,
7-
generateStaticProps,
8-
} from '@hashicorp/react-docs-page/server'
5+
import { getStaticGenerationFunctions } from '@hashicorp/react-docs-page/server'
6+
import path from 'path'
7+
8+
import { remarkRewriteAssets } from 'lib/remark-rewrite-assets'
99

1010
// Configure the docs path
1111
const BASE_ROUTE = 'cli'
12-
const NAV_DATA = 'data/cli-nav-data.json'
13-
const CONTENT_DIR = 'content/cli'
12+
const NAV_DATA = path.join(
13+
process.env.NAV_DATA_DIRNAME,
14+
BASE_ROUTE + '-nav-data.json'
15+
)
16+
const CONTENT_DIR = path.join(process.env.CONTENT_DIRNAME, BASE_ROUTE)
1417
const PRODUCT = { name: productName, slug: productSlug }
1518

1619
export default function CLILayout(props) {
@@ -27,25 +30,33 @@ export default function CLILayout(props) {
2730
)
2831
}
2932

30-
export async function getStaticPaths() {
31-
const paths = await generateStaticPaths({
32-
navDataFile: NAV_DATA,
33-
localContentDir: CONTENT_DIR,
34-
product: PRODUCT,
35-
})
36-
return { paths, fallback: false }
37-
}
33+
const { getStaticPaths, getStaticProps } = getStaticGenerationFunctions(
34+
process.env.IS_CONTENT_PREVIEW &&
35+
process.env.PREVIEW_FROM_REPO === 'terraform'
36+
? {
37+
strategy: 'fs',
38+
basePath: BASE_ROUTE,
39+
localContentDir: CONTENT_DIR,
40+
navDataFile: NAV_DATA,
41+
product: PRODUCT.slug,
42+
githubFileUrl(filepath) {
43+
// This path rewriting is meant for local preview from `terraform`.
44+
filepath = filepath.replace('preview/', '')
45+
return `https://github.com/hashicorp/${PRODUCT.slug}/blob/main/website/${filepath}`
46+
},
47+
remarkPlugins: (params) => [
48+
remarkRewriteAssets({
49+
product: PRODUCT.slug,
50+
version: process.env.CURRENT_GIT_BRANCH,
51+
getAssetPathParts: (nodeUrl) => ['website', nodeUrl],
52+
}),
53+
],
54+
}
55+
: {
56+
strategy: 'remote',
57+
basePath: BASE_ROUTE,
58+
product: PRODUCT.slug,
59+
}
60+
)
3861

39-
export async function getStaticProps({ params }) {
40-
const props = await generateStaticProps({
41-
navDataFile: NAV_DATA,
42-
localContentDir: CONTENT_DIR,
43-
params,
44-
product: PRODUCT,
45-
githubFileUrl(path) {
46-
const filepath = path.replace('content/', '')
47-
return `https://github.com/hashicorp/${PRODUCT.slug}/blob/main/website/docs/${filepath}`
48-
},
49-
})
50-
return { props }
51-
}
62+
export { getStaticPaths, getStaticProps }

pages/configuration/[[...page]].jsx

+38-27
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { productName, productSlug } from 'data/metadata'
22
import DocsPage from '@hashicorp/react-docs-page'
3+
import otherDocsData from 'data/other-docs-nav-data.json'
34
// Imports below are only used server-side
4-
import {
5-
generateStaticPaths,
6-
generateStaticProps,
7-
} from '@hashicorp/react-docs-page/server'
5+
import { getStaticGenerationFunctions } from '@hashicorp/react-docs-page/server'
6+
import path from 'path'
7+
import { remarkRewriteAssets } from 'lib/remark-rewrite-assets'
88

99
// Configure the docs path
1010
const BASE_ROUTE = 'configuration'
11-
const NAV_DATA = 'data/configuration-nav-data.json'
12-
const CONTENT_DIR = 'content/configuration'
11+
const NAV_DATA = path.join(
12+
process.env.NAV_DATA_DIRNAME,
13+
BASE_ROUTE + '-nav-data.json'
14+
)
15+
const CONTENT_DIR = path.join(process.env.CONTENT_DIRNAME, BASE_ROUTE)
1316
const PRODUCT = { name: productName, slug: productSlug }
1417

1518
export default function ConfigurationLayout(props) {
@@ -18,25 +21,33 @@ export default function ConfigurationLayout(props) {
1821
)
1922
}
2023

21-
export async function getStaticPaths() {
22-
const paths = await generateStaticPaths({
23-
navDataFile: NAV_DATA,
24-
localContentDir: CONTENT_DIR,
25-
product: PRODUCT,
26-
})
27-
return { paths, fallback: false }
28-
}
24+
const { getStaticPaths, getStaticProps } = getStaticGenerationFunctions(
25+
process.env.IS_CONTENT_PREVIEW &&
26+
process.env.PREVIEW_FROM_REPO === 'terraform'
27+
? {
28+
strategy: 'fs',
29+
basePath: BASE_ROUTE,
30+
localContentDir: CONTENT_DIR,
31+
navDataFile: NAV_DATA,
32+
product: PRODUCT.slug,
33+
githubFileUrl(filepath) {
34+
// This path rewriting is meant for local preview from `terraform`.
35+
filepath = filepath.replace('preview/', '')
36+
return `https://github.com/hashicorp/${PRODUCT.slug}/blob/main/website/${filepath}`
37+
},
38+
remarkPlugins: (params) => [
39+
remarkRewriteAssets({
40+
product: PRODUCT.slug,
41+
version: process.env.CURRENT_GIT_BRANCH,
42+
getAssetPathParts: (nodeUrl) => ['website', nodeUrl],
43+
}),
44+
],
45+
}
46+
: {
47+
strategy: 'remote',
48+
basePath: BASE_ROUTE,
49+
product: PRODUCT.slug,
50+
}
51+
)
2952

30-
export async function getStaticProps({ params }) {
31-
const props = await generateStaticProps({
32-
navDataFile: NAV_DATA,
33-
localContentDir: CONTENT_DIR,
34-
params,
35-
product: PRODUCT,
36-
githubFileUrl(path) {
37-
const filepath = path.replace('content/', '')
38-
return `https://github.com/hashicorp/${PRODUCT.slug}/blob/main/website/docs/${filepath}`
39-
},
40-
})
41-
return { props }
42-
}
53+
export { getStaticPaths, getStaticProps }

0 commit comments

Comments
 (0)