1 import path from "path"
2 import { Transformer } from "unified"
3 import {
4 UnistNodeWithData,
5 UnistTree,
6 ComponentLinkFixerOptions,
7 ExpressionJsVarLiteral,
8 } from "types"
9 import { FixLinkOptions, fixLinkUtil } from "../index.js"
10 import getAttribute from "../utils/get-attribute.js"
11 import { estreeToJs } from "docs-utils"
12 import { performActionOnLiteral } from "./perform-action-on-literal.js"
13 import { MD_LINK_REGEX } from "../constants.js"
14
15 const VALUE_LINK_REGEX = /^(![a-z]+!|\.)/gm
16
17 async function matchMdLinks(
18 str: string,
19 linkOptions: Omit<FixLinkOptions, "linkedPath">
20 ) {
21 let linkMatches
22 // reset regex
23 MD_LINK_REGEX.lastIndex = 0
24 while ((linkMatches = MD_LINK_REGEX.exec(str)) !== null) {
25 if (!linkMatches.groups?.link) {
26 return
27 }
28
29 const newUrl = await fixLinkUtil({
30 ...linkOptions,
31 linkedPath: linkMatches.groups.link,
32 })
33
34 str = str.replace(linkMatches.groups.link, newUrl)
35 // reset regex
36 MD_LINK_REGEX.lastIndex = 0
37 }
38
39 return str
40 }
41
42 async function matchValueLink(
43 str: string,
44 linkOptions: Omit<FixLinkOptions, "linkedPath">
45 ) {
46 // reset index
47 VALUE_LINK_REGEX.lastIndex = 0
48 if (!VALUE_LINK_REGEX.exec(str)) {
49 return str
50 }
51
52 return fixLinkUtil({
53 ...linkOptions,
54 linkedPath: str,
55 })
56 }
57
58 export function componentLinkFixer(
59 componentName: string,
60 attributeName: string,
61 options?: ComponentLinkFixerOptions
62 ): Transformer {
63 const { filePath, basePath, checkLinksType = "md", r2BaseUrl } = options || {}
64 return async (tree, file) => {
65 if (!file.cwd) {
66 return
67 }
68
69 if (!file.history.length) {
70 if (!filePath) {
71 return
72 }
73
74 file.history.push(filePath)
75 }
76
77 const { visit } = await import("unist-util-visit")
78
79 const currentPageFilePath = file.history[0].replace(
80 `/${path.basename(file.history[0])}`,
81 ""
82 )
83 const appsPath = basePath || path.join(file.cwd, "app")
84 const linkFn = checkLinksType === "md" ? matchMdLinks : matchValueLink
85
86 const nodesToProcess: UnistNodeWithData[] = []
87 visit(tree as UnistTree, "mdxJsxFlowElement", (node: UnistNodeWithData) => {
88 if (node.name === componentName) {
89 nodesToProcess.push(node)
90 }
91 })
92
93 const linkOptions = {
94 currentPageFilePath,
95 appsPath,
96 r2BaseUrl,
97 }
98
99 for (const node of nodesToProcess) {
100 const attribute = getAttribute(node, attributeName)
101
102 if (!attribute) {
103 continue
104 }
105
106 if (typeof attribute.value === "string") {
107 attribute.value =
108 (await linkFn(attribute.value, linkOptions)) || attribute.value
109 continue
110 }
111
112 if (!attribute.value.data?.estree) {
113 continue
114 }
115
116 const itemJsVar = estreeToJs(attribute.value.data.estree)
117
118 if (
119 !itemJsVar ||
120 ("name" in itemJsVar &&
121 typeof (itemJsVar as Record<string, unknown>).name === "string")
122 ) {
123 continue
124 }
125
126 const literals: ExpressionJsVarLiteral[] = []
127 performActionOnLiteral(itemJsVar, (item) => {
128 literals.push(item)
129 })
130
131 for (const item of literals) {
132 const newValue = await linkFn(
133 item.original.value as string,
134 linkOptions
135 )
136 if (newValue !== undefined) {
137 item.original.value = newValue
138 item.original.raw = JSON.stringify(newValue)
139 }
140 }
141 }
142 }
143 }