1 import type { Transformer } from "unified"
2 import type {
3 CrossProjectLinksOptions,
4 UnistNode,
5 UnistNodeWithData,
6 UnistTree,
7 } from "types"
8 import { estreeToJs } from "docs-utils"
9 import getAttribute from "./utils/get-attribute.js"
10 import { performActionOnLiteral } from "./utils/perform-action-on-literal.js"
11
12 const PROJECT_REGEX = /^!(?<area>[\w-]+)!/
13
14 function matchAndFixLinks(
15 link: string,
16 { baseUrl, projectUrls, useBaseUrl = false }: CrossProjectLinksOptions
17 ): string {
18 const projectArea = PROJECT_REGEX.exec(link)
19
20 if (!projectArea?.groups?.area) {
21 return link
22 }
23
24 const actualUrl = link.replace(PROJECT_REGEX, "")
25
26 const base =
27 !useBaseUrl &&
28 projectUrls &&
29 Object.hasOwn(projectUrls, projectArea.groups.area) &&
30 projectUrls[projectArea.groups.area]?.url
31 ? projectUrls[projectArea.groups.area].url
32 : baseUrl
33 const path =
34 projectUrls &&
35 Object.hasOwn(projectUrls, projectArea.groups.area) &&
36 projectUrls[projectArea.groups.area]?.path !== undefined
37 ? projectUrls[projectArea.groups.area].path
38 : `/${projectArea.groups.area}`
39
40 return `${base}${path}${actualUrl}`
41 }
42
43 function linkElmFixer(node: UnistNode, options: CrossProjectLinksOptions) {
44 if (!node.properties) {
45 return
46 }
47
48 node.properties.href = matchAndFixLinks(node.properties.href, options)
49 }
50
51 function linkNodeFixer(node: UnistNode, options: CrossProjectLinksOptions) {
52 if (!node.url) {
53 return
54 }
55
56 node.url = matchAndFixLinks(node.url, options)
57 }
58
59 function componentFixer(
60 node: UnistNodeWithData,
61 options: CrossProjectLinksOptions
62 ) {
63 if (!node.name) {
64 return
65 }
66
67 let attributeName: string | undefined
68
69 const maybeCheckAttribute = () => {
70 if (!attributeName) {
71 return
72 }
73
74 const attribute = getAttribute(node, attributeName)
75
76 if (!attribute) {
77 return
78 }
79
80 if (typeof attribute.value === "string") {
81 attribute.value = matchAndFixLinks(attribute.value, options)
82 return
83 }
84
85 if (!attribute.value.data?.estree) {
86 return
87 }
88
89 const itemJsVar = estreeToJs(attribute.value.data.estree)
90
91 if (!itemJsVar || "name" in itemJsVar) {
92 return
93 }
94
95 performActionOnLiteral(itemJsVar, (item) => {
96 item.original.value = matchAndFixLinks(
97 item.original.value as string,
98 options
99 )
100 item.original.raw = JSON.stringify(item.original.value)
101 })
102 }
103
104 switch (node.name) {
105 case "CardList":
106 case "Prerequisites":
107 attributeName = "items"
108 break
109 case "Card":
110 attributeName = "href"
111 break
112 case "WorkflowDiagram":
113 attributeName = "workflow"
114 break
115 case "TypeList":
116 attributeName = "types"
117 break
118 }
119
120 maybeCheckAttribute()
121 }
122
123 const allowedComponentNames = [
124 "Card",
125 "CardList",
126 "Prerequisites",
127 "WorkflowDiagram",
128 "TypeList",
129 ]
130
131 export function crossProjectLinksPlugin(
132 options: CrossProjectLinksOptions
133 ): Transformer {
134 return async (tree) => {
135 const { visit } = await import("unist-util-visit")
136
137 visit(
138 tree as UnistTree,
139 ["element", "mdxJsxFlowElement", "link"],
140 (node: UnistNode) => {
141 const isComponent =
142 node.name && allowedComponentNames.includes(node.name)
143 const isLinkElm = node.tagName === "a" && node.properties?.href
144 const isLinkNode = node.type === "link" && node.url
145 if (!isComponent && !isLinkElm && !isLinkNode) {
146 return
147 }
148
149 if (isComponent) {
150 componentFixer(node as UnistNodeWithData, options)
151 } else if (isLinkElm) {
152 linkElmFixer(node, options)
153 } else {
154 linkNodeFixer(node, options)
155 }
156 }
157 )
158 }
159 }