Skip to content

Commit 8594930

Browse files
committed
chore(website): Split form demos into separate pages
1 parent 367cc0d commit 8594930

File tree

94 files changed

+442
-270
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+442
-270
lines changed

packages/dev-utils/src/indexer/generate.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { join, sep } from "path";
44

55
import { documentationRoot, src } from "../constants";
66
import { toId, toTitle } from "../utils";
7-
import { parseDemoIndex } from "./parseDemoIndex";
7+
import { parseDemoRenderer } from "./parseDemoIndex";
88
import { parseMarkdown } from "./parseMarkdown";
99
import { parseSassDocAnchors } from "./parseSassDocAnchors";
1010
import {
@@ -62,6 +62,10 @@ function getTitleForRoute(route: string): string {
6262
.replace("Api", "API")
6363
.replace("Sassdoc", "SassDoc");
6464

65+
if (/Demos$/.test(title) && route.includes("form")) {
66+
return title;
67+
}
68+
6569
if (
6670
/SassDoc|API|Demos|Changelog/.test(title) ||
6771
(title === "Installation" && route.startsWith("/packages"))
@@ -104,9 +108,9 @@ export async function generate(
104108
type = "theme";
105109
summary =
106110
"Create a custom theme for your app and this documentation site using the live preview. The theme builder supports changing the theme colors using the material design color palette as well as warning when there are contrast ratio problems.";
107-
} else if (route.endsWith("/demos")) {
111+
} else if (/(\/|-)demos$/.test(route)) {
108112
type = "demos";
109-
({ anchors, summary, demos } = parseDemoIndex(route));
113+
({ anchors, summary, demos } = parseDemoRenderer(route));
110114
} else if (/\/(api|sassdoc)$/.test(route)) {
111115
const [routeType, pkgName] = route.split("/").reverse();
112116
switch (routeType) {

packages/dev-utils/src/indexer/getRoutes.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ export async function getRoutes({
2121
const pagesFolder = join(documentationRoot, src, "pages");
2222
const paths = await glob("**/*.+(ts|tsx)", {
2323
cwd: pagesFolder,
24-
ignore: ["api/**/*", "index.ts", "_*"],
24+
ignore: ["api/**/*", "index.ts", "**/form/demos.tsx", "_*"],
2525
});
2626

2727
const apiablePackages = getPackages("typescript");
28-
const demoablePackages = apiablePackages.filter((name) => name !== "layout");
28+
const demoablePackages = apiablePackages.filter(
29+
(name) => name !== "layout" && name !== "form"
30+
);
2931
const sassdocablePackages = getPackages("scss");
3032
const packages = getPackages();
3133

packages/dev-utils/src/indexer/parseDemoIndex.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,28 @@ interface ReduceResult {
1919
demos: DemoMetadata[];
2020
}
2121

22-
export function parseDemoIndex(demoRoute: string): MarkdownResult {
23-
const [, pkgName] = demoRoute.split("/").reverse();
24-
const demoFolder = join(
22+
export function parseDemoRenderer(demoRoute: string): MarkdownResult {
23+
const [fileName, pkgName] = demoRoute.split("/").reverse();
24+
let demoFolder = join(
2525
documentationRoot,
2626
src,
2727
"components",
2828
"Demos",
2929
toTitle(pkgName, "")
3030
);
31-
const demoIndexPath = join(demoFolder, "index.tsx");
3231

33-
const contents = readFileSync(demoIndexPath, "utf8");
32+
let demoRendererPath = join(demoFolder, "index.tsx");
33+
if (fileName.endsWith("-demos")) {
34+
// I really need to redo this...
35+
const partName = toTitle(
36+
fileName.replace("-demos", fileName.startsWith("validation") ? "" : "s"),
37+
""
38+
);
39+
demoFolder = join(demoFolder, partName);
40+
demoRendererPath = join(demoFolder, `${partName}.tsx`);
41+
}
42+
43+
const contents = readFileSync(demoRendererPath, "utf8");
3444
const lines = contents.split(/\r?\n/);
3545

3646
const { anchors, demos } = lines.reduce<ReduceResult>(

packages/dev-utils/src/sandbox/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ export const SANDBOXES_PATH = join(
1010
"sandboxes"
1111
);
1212

13+
export const FORM_PARTS = [
14+
"FileInputs",
15+
"SelectFields",
16+
"SelectionControls",
17+
"Sliders",
18+
"TextFields",
19+
"Validation",
20+
];
21+
1322
export const DEPENDENCIES = [
1423
"@react-md/layout",
1524
"@react-md/states",

packages/dev-utils/src/sandbox/createPackageJson.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { JSONObject } from "../constants";
22
import { toId } from "../utils";
3-
import { DEV_DEPENDENCIES } from "./constants";
3+
import { DEV_DEPENDENCIES, FORM_PARTS } from "./constants";
44
import { ReferencedDependencies } from "./getAllDependencies";
55

66
function toDependencyJson(
@@ -48,19 +48,33 @@ function toDevDependencyJson(
4848
return toDependencyJson(deps);
4949
}
5050

51-
export function createPackageJson(
52-
demoTitle: string,
53-
fullDemoTitle: string,
54-
packageName: string,
55-
dependencies: ReferencedDependencies
56-
): JSONObject {
51+
interface CreatePackageJsonOptions {
52+
demoTitle: string;
53+
formPart: typeof FORM_PARTS[number] | undefined;
54+
packageName: string;
55+
dependencies: ReferencedDependencies;
56+
fullDemoTitle: string;
57+
}
58+
59+
export function createPackageJson({
60+
demoTitle,
61+
formPart,
62+
packageName,
63+
dependencies,
64+
fullDemoTitle,
65+
}: CreatePackageJsonOptions): JSONObject {
5766
const allDeps = Array.from(dependencies);
67+
let demoPrefix = `${toId(packageName)}/demos`;
68+
if (formPart) {
69+
demoPrefix = demoPrefix.replace("/demos", `/${toId(formPart)}-demos`);
70+
}
71+
72+
const demoId = toId(demoTitle);
73+
const packageDemoUrl = `https://react-md.dev/packages/${demoPrefix}#${demoId}`;
5874

5975
const packageJson: JSONObject = {
6076
title: fullDemoTitle,
61-
description: `Example from https://react-md.dev/packages/${toId(
62-
packageName
63-
)}/demos#${toId(demoTitle)}`,
77+
description: `Example from ${packageDemoUrl}`,
6478
main: "src/index.tsx",
6579
dependencies: toDependencyJson(allDeps),
6680
devDependencies: toDevDependencyJson(allDeps, DEV_DEPENDENCIES),

packages/dev-utils/src/sandbox/createSandbox.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { getAliases } from "./aliases";
1010
import {
1111
DEMO_INDEX,
1212
DEMO_INDEX_HTML,
13+
FORM_PARTS,
1314
SANDBOXES_PATH,
1415
VARIABLES_SCSS_FILE,
1516
} from "./constants";
@@ -53,6 +54,7 @@ function transformFileContents(
5354
.replace(/<CodeBlock[^>]*>/g, "<pre><code>")
5455
.replace(/<\/CodeBlock>/g, "</code></pre>")
5556
.replace(/<(\/)?Code/g, "<$1code")
57+
.replace(/\.(\.\/TextFieldThemeConfig)/, "$1")
5658
.replace(aliasRegExp, `"${aliasReplacement}`);
5759

5860
if (demoName) {
@@ -119,11 +121,23 @@ function createJsxSandbox(
119121
writeJsonSync(sandboxPath.replace(".json", "-js.json"), files, { spaces: 2 });
120122
}
121123

124+
const FORM_PARTS_REGEXP = new RegExp(`\\/(${FORM_PARTS.join("|")})`);
125+
const FORM_PART_REGEXP = new RegExp(
126+
`(${FORM_PARTS.reduce<string>(
127+
(s, part) => `${s ? `${s}|` : ""}${part.replace(/s$/, "")}`,
128+
""
129+
)})`
130+
);
131+
122132
export function createSandbox(
123133
demo: ImportDeclaration,
124134
project: Project,
125135
parentFolder: string
126136
): void {
137+
const formPart = demo
138+
.getSourceFile()
139+
.getFilePath()
140+
.match(FORM_PART_REGEXP)?.[0];
127141
const importName = demo.getModuleSpecifierValue();
128142
const sourceFile = getDemoSourceFile(importName, parentFolder, project);
129143
const [dependencies, resolvedFiles] = getAllDependencies(sourceFile, project);
@@ -148,12 +162,13 @@ export function createSandbox(
148162
"src/_variables.scss": {
149163
content: VARIABLES_SCSS_FILE,
150164
},
151-
"package.json": createPackageJson(
165+
"package.json": createPackageJson({
152166
demoTitle,
153-
fullDemoTitle,
167+
formPart,
154168
packageName,
155-
dependencies
156-
),
169+
dependencies,
170+
fullDemoTitle,
171+
}),
157172
"src/Demo.tsx": {
158173
content: transformFileContents(
159174
sourceFile.getFullText(),
@@ -168,6 +183,7 @@ export function createSandbox(
168183
const fileName = aliasedFileName
169184
.replace(aliasRegExp, "")
170185
.replace(`${demoName}${sep}`, "")
186+
.replace(FORM_PARTS_REGEXP, "")
171187
.replace(/\/?Demos\/[A-z]+\/?/, "");
172188

173189
const key = join("src", `${fileName}${getExtension(fileName)}`);

packages/dev-utils/src/sandbox/createSandboxes.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export function createSandboxes(
2525

2626
const sourceFile = project.getSourceFileOrThrow(demoFilePath);
2727
const imports = sourceFile.getImportDeclarations();
28-
const i =
29-
imports.findIndex((imp) => imp.getText().endsWith('../DemoPage";')) + 1;
28+
let i =
29+
imports.findIndex((imp) => imp.getText().endsWith('/DemoPage";')) + 1;
3030
if (i === 0) {
3131
log.error(
3232
"Unable to find a `DemoPage` token for the current source file:"
@@ -35,6 +35,10 @@ export function createSandboxes(
3535
log.error(new Error().stack);
3636
process.exit(1);
3737
}
38+
if (imports[i].getText().endsWith('Demos/types";')) {
39+
i += 1;
40+
}
41+
3842
const demos = imports
3943
.slice(i)
4044
.filter(

packages/dev-utils/src/sandbox/index.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import log from "loglevel";
33
import { join } from "path";
44

55
import { clean, format, glob } from "../utils";
6-
import { DEMOS_FOLDER, SANDBOXES_FILE, SANDBOXES_PATH } from "./constants";
6+
import {
7+
FORM_PARTS,
8+
DEMOS_FOLDER,
9+
SANDBOXES_FILE,
10+
SANDBOXES_PATH,
11+
} from "./constants";
712
import { createSandboxes } from "./createSandboxes";
813

914
type Lookups = Record<string, Record<string, string>>;
@@ -43,7 +48,17 @@ export async function sandbox({
4348

4449
const fullPattern = `${DEMOS_FOLDER}/${pattern}/index.tsx`;
4550
const demoFiles = await glob(fullPattern);
46-
if (!demoFiles.length) {
51+
if (
52+
pattern === "*" ||
53+
/form/.test(pattern) ||
54+
new RegExp(FORM_PARTS.join("|"), "i").test(pattern)
55+
) {
56+
const FORM_FILES = FORM_PARTS.map(
57+
(part) => `${DEMOS_FOLDER}/Form/${part}/${part}.tsx`
58+
);
59+
demoFiles.push(...FORM_FILES);
60+
}
61+
if (!demoFiles.length && pattern !== "*") {
4762
log.error("No demos found with the following pattern:");
4863
log.error(pattern);
4964
log.error(new Error().stack);
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)