docs: fix page title in reference pagesnot showing + refactor workflows reference (#10537)

* docs: fix references title + refactor workflow reference generation

* update references sidebar
This commit is contained in:
Shahed Nasser
2024-12-10 18:19:03 +02:00
committed by GitHub
parent 4ad9ac1e5f
commit 482929f74d
11 changed files with 159 additions and 53 deletions
@@ -12,6 +12,8 @@ import MDXComponents from "@/components/MDXComponents"
import mdxOptions from "../../../mdx-options.mjs" import mdxOptions from "../../../mdx-options.mjs"
import { slugChanges } from "../../../generated/slug-changes.mjs" import { slugChanges } from "../../../generated/slug-changes.mjs"
import { filesMap } from "../../../generated/files-map.mjs" import { filesMap } from "../../../generated/files-map.mjs"
import { Metadata } from "next"
import { cache } from "react"
type PageProps = { type PageProps = {
params: Promise<{ params: Promise<{
@@ -23,28 +25,20 @@ export default async function ReferencesPage(props: PageProps) {
const params = await props.params const params = await props.params
const { slug } = params const { slug } = params
// ensure that Vercel loads references files const fileData = await loadFile(slug)
path.join(process.cwd(), "references")
const monoRepoPath = path.resolve("..", "..", "..")
const pathname = `/references/${slug.join("/")}` if (!fileData) {
const fileDetails =
slugChanges.find((f) => f.newSlug === pathname) ||
filesMap.find((f) => f.pathname === pathname)
if (!fileDetails) {
return notFound() return notFound()
} }
const fullPath = path.join(monoRepoPath, fileDetails.filePath)
const fileContent = await fs.readFile(fullPath, "utf-8")
const pluginOptions = { const pluginOptions = {
filePath: fullPath, filePath: fileData.path,
basePath: process.cwd(), basePath: process.cwd(),
} }
return ( return (
<MDXRemote <MDXRemote
source={fileContent} source={fileData.content}
components={MDXComponents} components={MDXComponents}
options={{ options={{
mdxOptions: { mdxOptions: {
@@ -79,3 +73,55 @@ export default async function ReferencesPage(props: PageProps) {
/> />
) )
} }
export async function generateMetadata({
params,
}: PageProps): Promise<Metadata> {
// read route params
const slug = (await params).slug
const metadata: Metadata = {}
const fileData = await loadFile(slug)
if (!fileData) {
return metadata
}
const pageTitleMatch = /#(?<title>[\w -]+)/.exec(fileData.content)
if (!pageTitleMatch?.groups?.title) {
return metadata
}
metadata.title = pageTitleMatch.groups.title
return metadata
}
const loadFile = cache(
async (
slug: string[]
): Promise<
| {
content: string
path: string
}
| undefined
> => {
path.join(process.cwd(), "references")
const monoRepoPath = path.resolve("..", "..", "..")
const pathname = `/references/${slug.join("/")}`
const fileDetails =
slugChanges.find((f) => f.newSlug === pathname) ||
filesMap.find((f) => f.pathname === pathname)
if (!fileDetails) {
return undefined
}
const fullPath = path.join(monoRepoPath, fileDetails.filePath)
return {
content: await fs.readFile(fullPath, "utf-8"),
path: fullPath,
}
}
)
+4 -4
View File
@@ -9802,7 +9802,7 @@ export const generatedSidebar = [
"isPathHref": true, "isPathHref": true,
"type": "link", "type": "link",
"path": "/service-factory-reference", "path": "/service-factory-reference",
"title": "Service Factory Reference", "title": "Service Factory",
"isChildSidebar": true, "isChildSidebar": true,
"children": [ "children": [
{ {
@@ -9902,7 +9902,7 @@ export const generatedSidebar = [
"isPathHref": true, "isPathHref": true,
"type": "link", "type": "link",
"path": "/references/helper-steps", "path": "/references/helper-steps",
"title": "Helper Steps Reference", "title": "Helper Steps",
"isChildSidebar": true, "isChildSidebar": true,
"autogenerate_path": "/references/helper_steps/functions", "autogenerate_path": "/references/helper_steps/functions",
"children": [ "children": [
@@ -9976,7 +9976,7 @@ export const generatedSidebar = [
"loaded": true, "loaded": true,
"isPathHref": true, "isPathHref": true,
"type": "link", "type": "link",
"title": "Core Workflows Reference", "title": "Core Workflows",
"path": "/medusa-workflows-reference", "path": "/medusa-workflows-reference",
"isChildSidebar": true, "isChildSidebar": true,
"custom_autogenerate": "core-flows", "custom_autogenerate": "core-flows",
@@ -15114,7 +15114,7 @@ export const generatedSidebar = [
"loaded": true, "loaded": true,
"isPathHref": true, "isPathHref": true,
"type": "link", "type": "link",
"title": "Testing Framework Reference", "title": "Testing Framework",
"path": "/test-tools-reference", "path": "/test-tools-reference",
"isChildSidebar": true, "isChildSidebar": true,
"children": [ "children": [
+4 -4
View File
@@ -2404,7 +2404,7 @@ export const sidebar = sidebarAttachHrefCommonOptions([
{ {
type: "link", type: "link",
path: "/service-factory-reference", path: "/service-factory-reference",
title: "Service Factory Reference", title: "Service Factory",
isChildSidebar: true, isChildSidebar: true,
children: [ children: [
{ {
@@ -2422,20 +2422,20 @@ export const sidebar = sidebarAttachHrefCommonOptions([
{ {
type: "link", type: "link",
path: "/references/helper-steps", path: "/references/helper-steps",
title: "Helper Steps Reference", title: "Helper Steps",
isChildSidebar: true, isChildSidebar: true,
autogenerate_path: "/references/helper_steps/functions", autogenerate_path: "/references/helper_steps/functions",
}, },
{ {
type: "link", type: "link",
title: "Core Workflows Reference", title: "Core Workflows",
path: "/medusa-workflows-reference", path: "/medusa-workflows-reference",
isChildSidebar: true, isChildSidebar: true,
custom_autogenerate: "core-flows", custom_autogenerate: "core-flows",
}, },
{ {
type: "link", type: "link",
title: "Testing Framework Reference", title: "Testing Framework",
path: "/test-tools-reference", path: "/test-tools-reference",
isChildSidebar: true, isChildSidebar: true,
children: [ children: [
+2 -1
View File
@@ -11,7 +11,8 @@
"../../node_modules/@types", "../../node_modules/@types",
"./node_modules/@types", "./node_modules/@types",
"./types" "./types"
] ],
"target": "es2018"
}, },
"include": [ "include": [
"next-env.d.ts", "next-env.d.ts",
@@ -30,9 +30,6 @@ const allowedProjectDocuments: AllowedProjectDocumentsOption = {
[ReflectionKind.Method]: true, [ReflectionKind.Method]: true,
[ReflectionKind.Property]: true, [ReflectionKind.Property]: true,
}, },
"core-flows": {
[ReflectionKind.Function]: true,
},
} }
modules.forEach((module) => { modules.forEach((module) => {
@@ -47,7 +44,11 @@ dmlModules.forEach((module) => {
} }
}) })
getNamespaceNames(getCoreFlowNamespaces()).forEach((namespace) => { const { mainNamespaces: mainCoreFlowNamespaces } = getNamespaceNames(
getCoreFlowNamespaces()
)
mainCoreFlowNamespaces.forEach((namespace) => {
allowedProjectDocuments[namespace] = { allowedProjectDocuments[namespace] = {
...commonAllowedDocuments, ...commonAllowedDocuments,
} }
@@ -67,8 +67,23 @@ export function getCoreFlowNamespaces(): NamespaceGenerateDetails[] {
return namespaces return namespaces
} }
export function getNamespaceNames( export function getNamespaceNames(namespaces: NamespaceGenerateDetails[]): {
namespaces: NamespaceGenerateDetails[] mainNamespaces: string[]
): string[] { childNamespaces: string[]
return namespaces.map((namespace) => namespace.name) } {
const mainNamespaces: string[] = []
const childNamespaces: string[] = []
namespaces.map((namespace) => {
mainNamespaces.push(namespace.name)
childNamespaces.push(
...(namespace.children?.map((childNamespace) => childNamespace.name) ||
[])
)
})
return {
mainNamespaces,
childNamespaces,
}
} }
@@ -2,10 +2,10 @@ import { minimatch } from "minimatch"
import { import {
Application, Application,
Comment, Comment,
Context,
Converter, Converter,
DeclarationReflection, DeclarationReflection,
ParameterType, ParameterType,
ProjectReflection,
ReflectionKind, ReflectionKind,
} from "typedoc" } from "typedoc"
import { NamespaceGenerateDetails } from "types" import { NamespaceGenerateDetails } from "types"
@@ -35,17 +35,18 @@ export function load(app: Application) {
"generatePathNamespaces" "generatePathNamespaces"
) as unknown as NamespaceGenerateDetails[] ) as unknown as NamespaceGenerateDetails[]
const generatePathNamespaces = (ns: NamespaceGenerateDetails[]) => { const generatePathNamespaces = (
ns: NamespaceGenerateDetails[],
parent: ProjectReflection | DeclarationReflection = context.project
) => {
const createdNamespaces: DeclarationReflection[] = [] const createdNamespaces: DeclarationReflection[] = []
ns.forEach((namespace) => { ns.forEach((namespace) => {
const genNamespace = createNamespace(context, namespace) const genNamespace = createNamespace(parent, namespace)
generatedNamespaces.set(namespace.pathPattern, genNamespace) generatedNamespaces.set(namespace.pathPattern, genNamespace)
if (namespace.children) { if (namespace.children) {
generatePathNamespaces(namespace.children).forEach((child) => generatePathNamespaces(namespace.children, genNamespace)
genNamespace.addChild(child)
)
} }
createdNamespaces.push(genNamespace) createdNamespaces.push(genNamespace)
@@ -102,24 +103,27 @@ export function load(app: Application) {
const namespace = findNamespace(namespaces) const namespace = findNamespace(namespaces)
namespace?.addChild(reflection) if (namespace) {
context.project.removeChild(reflection)
namespace?.addChild(reflection)
}
} }
) )
} }
function createNamespace( function createNamespace(
context: Context, parent: DeclarationReflection | ProjectReflection,
namespace: NamespaceGenerateDetails namespace: NamespaceGenerateDetails
): DeclarationReflection { ): DeclarationReflection {
const genNamespace = context.createDeclarationReflection( const reflection = new DeclarationReflection(
namespace.name,
ReflectionKind.Namespace, ReflectionKind.Namespace,
void 0, parent
void 0,
namespace.name
) )
parent.addChild(reflection)
if (namespace.description) { if (namespace.description) {
genNamespace.comment = new Comment([ reflection.comment = new Comment([
{ {
kind: "text", kind: "text",
text: namespace.description, text: namespace.description,
@@ -127,5 +131,5 @@ function createNamespace(
]) ])
} }
return genNamespace return reflection
} }
@@ -1,9 +1,12 @@
import { MarkdownTheme } from "../../theme" import { MarkdownTheme } from "../../theme"
import * as Handlebars from "handlebars" import * as Handlebars from "handlebars"
import { DocumentReflection, SignatureReflection } from "typedoc" import {
DocumentReflection,
ReflectionKind,
SignatureReflection,
} from "typedoc"
import { formatWorkflowDiagramComponent } from "../../utils/format-workflow-diagram-component" import { formatWorkflowDiagramComponent } from "../../utils/format-workflow-diagram-component"
import { getProjectChild } from "utils" import { findReflectionInNamespaces, getProjectChild } from "utils"
import { getWorkflowReflectionFromNamespace } from "../../utils/workflow-utils"
export default function (theme: MarkdownTheme) { export default function (theme: MarkdownTheme) {
Handlebars.registerHelper( Handlebars.registerHelper(
@@ -81,7 +84,13 @@ function getStep({
: "step" : "step"
const namespaceRefl = theme.project const namespaceRefl = theme.project
? getWorkflowReflectionFromNamespace(theme.project, document.name) ? findReflectionInNamespaces(
theme.project
.getChildrenByKind(ReflectionKind.Module)
.find((moduleRef) => moduleRef.name === "core-flows") ||
theme.project,
document.name
)
: undefined : undefined
const associatedReflection = const associatedReflection =
@@ -15,7 +15,7 @@ import {
import ts, { SyntaxKind, VariableStatement } from "typescript" import ts, { SyntaxKind, VariableStatement } from "typescript"
import { WorkflowManager, WorkflowDefinition } from "@medusajs/orchestration" import { WorkflowManager, WorkflowDefinition } from "@medusajs/orchestration"
import Helper from "./utils/helper" import Helper from "./utils/helper"
import { isWorkflow, isWorkflowStep } from "utils" import { findReflectionInNamespaces, isWorkflow, isWorkflowStep } from "utils"
import { StepType } from "./types" import { StepType } from "./types"
type ParsedStep = { type ParsedStep = {
@@ -253,8 +253,10 @@ class WorkflowsPlugin {
workflowName: workflowVarName, workflowName: workflowVarName,
}) })
} else { } else {
const initializerReflection = const initializerReflection = findReflectionInNamespaces(
context.project.getChildByName(initializerName) context.project,
initializerName
)
if ( if (
!initializerReflection || !initializerReflection ||
@@ -5,7 +5,7 @@ import {
} from "typedoc" } from "typedoc"
import ts from "typescript" import ts from "typescript"
import { StepModifier, StepType } from "../types" import { StepModifier, StepType } from "../types"
import { capitalize } from "utils" import { capitalize, findReflectionInNamespaces } from "utils"
/** /**
* A class of helper methods. * A class of helper methods.
@@ -173,7 +173,9 @@ export default class Helper {
project: ProjectReflection project: ProjectReflection
): string | undefined { ): string | undefined {
// load it from the project // load it from the project
const idVarReflection = project.getChildByName(refName) const idVarReflection =
project.getChildByName(refName) ||
findReflectionInNamespaces(project, refName)
if ( if (
!idVarReflection || !idVarReflection ||
+27 -1
View File
@@ -1,4 +1,12 @@
import { ReferenceType, SignatureReflection, SomeType } from "typedoc" import {
DeclarationReflection,
ProjectReflection,
ReferenceType,
Reflection,
ReflectionKind,
SignatureReflection,
SomeType,
} from "typedoc"
export function isWorkflow(reflection: SignatureReflection): boolean { export function isWorkflow(reflection: SignatureReflection): boolean {
return ( return (
@@ -60,3 +68,21 @@ function isAllowedType(type: SomeType | undefined): boolean {
!disallowedIntrinsicTypeNames.includes(type.name)) !disallowedIntrinsicTypeNames.includes(type.name))
) )
} }
export function findReflectionInNamespaces(
parent: ProjectReflection | DeclarationReflection,
childName: string
): Reflection | undefined {
let childReflection: Reflection | undefined
parent.getChildrenByKind(ReflectionKind.Namespace).some((namespace) => {
childReflection = namespace.getChildByName(childName)
if (!childReflection) {
childReflection = findReflectionInNamespaces(namespace, childName)
}
return childReflection !== undefined
})
return childReflection
}