@patternfly/documentation-framework 1.2.14 → 1.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## 1.2.15 (2022-09-07)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **docs:** codesandbox relative imports point to package import ([#3122](https://github.com/patternfly/patternfly-org/issues/3122)) ([fbecd9d](https://github.com/patternfly/patternfly-org/commit/fbecd9d1b120d657910f3bb00486a49bf90360b6))
12
+
13
+
14
+
15
+
16
+
6
17
  ## 1.2.14 (2022-09-06)
7
18
 
8
19
  **Note:** Version bump only for package @patternfly/documentation-framework
@@ -79,7 +79,9 @@ export const Example = ({
79
79
  // Content that appears between h3 and code block to explain example
80
80
  children,
81
81
  // Show dark theme switcher on full page examples
82
- hasDarkThemeSwitcher = process.env.hasDarkThemeSwitcher
82
+ hasDarkThemeSwitcher = process.env.hasDarkThemeSwitcher,
83
+ // Map of relative imports matched to their npm package import path (passed to Codesandbox)
84
+ relativeImports
83
85
  }) => {
84
86
  if (isFullscreenPreview) {
85
87
  isFullscreen = false;
@@ -98,9 +100,9 @@ export const Example = ({
98
100
 
99
101
  const [editorCode, setEditorCode] = React.useState(code);
100
102
  const loc = useLocation();
101
-
102
103
  const scope = {
103
104
  ...liveContext,
105
+ // These 2 are in the bundle anyways for the site since we dogfood
104
106
  ...reactCoreModule,
105
107
  ...reactTableModule,
106
108
  ...(source === 'react-next' ? reactCoreNextModule : {})
@@ -158,7 +160,7 @@ export const Example = ({
158
160
  const codeBoxParams = getParameters(
159
161
  lang === 'html'
160
162
  ? getStaticParams(title, editorCode)
161
- : getReactParams(title, editorCode, scope, lang)
163
+ : getReactParams(title, editorCode, scope, lang, relativeImports)
162
164
  );
163
165
  const fullscreenLink = loc.pathname.replace(/\/$/, '')
164
166
  + (loc.pathname.endsWith(source) ? '' : `/${source}`)
@@ -2,6 +2,7 @@ const { parse } = require('@patternfly/ast-helpers');
2
2
  const versions = require('../versions.json');
3
3
  const overpass = require('./fonts');
4
4
  const { capitalize } = require('./capitalize');
5
+ const pathPrefix = process.env.pathPrefix;
5
6
 
6
7
  const getStaticParams = (title, html) => {
7
8
  const imgAssetRegex = /['"](\/assets\/images\/.*)['"]/g;
@@ -106,7 +107,7 @@ function prettyExampleCode(title, code, declaration, identifier) {
106
107
  }
107
108
 
108
109
  // TODO: Make React examples work and use a template that has our assets.
109
- function getReactParams(title, code, scope, lang) {
110
+ function getReactParams(title, code, scope, lang, relativeImports) {
110
111
  let toRender = null;
111
112
  try {
112
113
  let declaration = getExampleDeclaration(code);
@@ -129,7 +130,7 @@ function getReactParams(title, code, scope, lang) {
129
130
  catch (err) {
130
131
  // Ignore
131
132
  }
132
-
133
+ // Update image imports to point to pf.org
133
134
  const imgImportRegex = /import\s*(\w*).*['"](.*)(\.(png|jpe?g|webp|gif|svg))['"]/g;
134
135
  let imgImportMatch;
135
136
  while ((imgImportMatch = imgImportRegex.exec(code))) {
@@ -137,6 +138,16 @@ function getReactParams(title, code, scope, lang) {
137
138
  code = code.replace(imgImportMatch[0], `const ${imgName} = "https://www.patternfly.org/v4${scope[imgName]}"`);
138
139
  }
139
140
 
141
+ const relImportRegex = /(?<=import[\s*{])([\w*{}\n\r\t, ]+)(?=[\s*]from\s["']([\.\/]+.*)["'])/gm;
142
+ let relImportMatch;
143
+ while (relImportMatch = relImportRegex.exec(code)) {
144
+ const [ relImportName, _name, relImportPath ] = relImportMatch;
145
+ if (relativeImports[relImportName]) {
146
+ code = code.replace(relImportPath, relativeImports[relImportName]);
147
+ }
148
+ }
149
+
150
+
140
151
  const dependencies = {
141
152
  '@patternfly/react-core': versions.Releases[0].versions['@patternfly/react-core']
142
153
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@patternfly/documentation-framework",
3
3
  "description": "A framework to build documentation for PatternFly.",
4
- "version": "1.2.14",
4
+ "version": "1.2.15",
5
5
  "author": "Red Hat",
6
6
  "license": "MIT",
7
7
  "private": false,
@@ -84,5 +84,5 @@
84
84
  "react": "^16.8.0 || ^17.0.0",
85
85
  "react-dom": "^16.8.0 || ^17.0.0"
86
86
  },
87
- "gitHead": "9c5b4bf2295a80829fcc1c706263b4ee2ea7d443"
87
+ "gitHead": "ca1b4cfd13c0d6cae2f9d7638188a75f323cd5be"
88
88
  }
@@ -69,12 +69,36 @@ function serializeRoot(node, options) {
69
69
 
70
70
  const importStatements = groups.import
71
71
  .map(node => node.value)
72
- .map(imp => imp.replace(/(['"])\./g, (_, match) => `${match}${getRelPath()}${path.posix.sep}\.`))
72
+ .map(imp => imp.replace(/(['"])\./g, (_, match) => `${match}${getRelPath()}${path.posix.sep}\.`));
73
+
74
+ // Map relative import name to '@package...'
75
+ const relativeImportsRegex = /(?:[\.\/]+.*)(@.*)['"]/gm;
76
+ let relativeImportMatch;
77
+ let relativeImportMatches = {};
78
+ while (relativeImportMatch = relativeImportsRegex.exec(importStatements[0])) {
79
+ const [_match, absoluteImportPath] = relativeImportMatch;
80
+ if (absoluteImportPath && !absoluteImportPath.includes('srcImport')) {
81
+ // `@patternfly/react-core/src/demos/./examples/DashboardWrapper` to `DashboardWrapper`
82
+ let relativeFileImport = /(?<=\/)(\.+\/.*)/gm.exec(absoluteImportPath);
83
+ if (relativeFileImport) {
84
+ // Build map of relative imports (from example.js code) to npm package import path (used in codesandbox.js)
85
+ const relativeFilePath = relativeFileImport[0];
86
+ const relativeImportName = relativeFilePath
87
+ .split('/')
88
+ .pop()
89
+ .split('.')
90
+ .shift();
91
+ relativeImportMatches[relativeImportName] = absoluteImportPath;
92
+ }
93
+ }
94
+ }
95
+
96
+ const importStatementsWithThumbnails = importStatements
73
97
  .concat(thumbnailImports)
74
- .join('\n')
98
+ .join('\n');
75
99
 
76
100
  // https://astexplorer.net/#/gist/9c531dd372dfc57e194c13c2889d31c3/03f2d6e889db1a733c6a079554e8af7784863739
77
- options.importSpecifiers = parse(importStatements).body
101
+ options.importSpecifiers = parse(importStatementsWithThumbnails).body
78
102
  .map(node => node.specifiers)
79
103
  .flat(1)
80
104
  .map(spec => spec.local ? spec.local.name : null)
@@ -82,14 +106,13 @@ function serializeRoot(node, options) {
82
106
  const liveContext = options.importSpecifiers
83
107
  .filter(localName => !/srcImport.*/.test(localName)) // Images in MD like [!img](./src)
84
108
  .join(',\n ');
85
-
86
109
  const childNodes = groups.rest
87
110
  .map(childNode => toJSX(childNode, node, options))
88
111
  .join('');
89
112
 
90
113
  let res = `import React from 'react';
91
114
  import { AutoLinkHeader, Example, Link as PatternflyThemeLink } from '@patternfly/documentation-framework/components';
92
- ${importStatements}
115
+ ${importStatementsWithThumbnails}
93
116
  const pageData = ${JSON.stringify(pageData, null, 2)};
94
117
  `;
95
118
  if (liveContext) {
@@ -97,6 +120,13 @@ const pageData = ${JSON.stringify(pageData, null, 2)};
97
120
  ' ' + liveContext
98
121
  }\n};\n`
99
122
  }
123
+ if (relativeImportMatches) {
124
+ res += `pageData.relativeImports = {\n${
125
+ ' ' + Object.entries(relativeImportMatches)
126
+ .map(([key, val]) => `'${key}': '${val}'`)
127
+ .join(',\n ')
128
+ }\n};\n`;
129
+ }
100
130
  if (examples) {
101
131
  res += `pageData.examples = {\n${
102
132
  ' ' + Object.entries(examples)