Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions apps/www/lib/rehype-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ export function rehypeComponent() {
src = path.join(process.cwd(), srcPath)
} else {
const component = Index[name]
src = fileName
? (component.files.find((file: unknown) => {
if (typeof file === "string") {
return (
file.endsWith(`${fileName}.tsx`) ||
file.endsWith(`${fileName}.ts`)
)
}
return false
}) ?? component.files[0]?.path)
: component.files[0]?.path
if (fileName) {
const matchedFile = component.files.find((file: unknown) => {
const filePath = getFilePath(file)
return (
filePath?.endsWith(`${fileName}.tsx`) ||
filePath?.endsWith(`${fileName}.ts`)
)
})
src = getFilePath(matchedFile) ?? component.files[0]?.path
} else {
src = component.files[0]?.path
}
}

// Read the source file.
Expand Down Expand Up @@ -147,3 +148,13 @@ export function rehypeComponent() {
function getNodeAttributeByName(node: UnistNode, name: string) {
return node.attributes?.find((attribute) => attribute.name === name)
}

// Registry file entries can be either a plain path string or an object
// like { path, type, target }; pull the path out of either shape.
function getFilePath(file: unknown): string | undefined {
if (typeof file === "string") {
return file
}

return (file as { path?: string } | undefined)?.path
}
Loading