radiant-docs 0.1.9 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "radiant-docs",
3
- "version": "0.1.9",
3
+ "version": "0.1.12",
4
4
  "description": "CLI tool for previewing Radiant documentation locally",
5
5
  "type": "module",
6
6
  "bin": {
@@ -11,6 +11,7 @@ interface Props {
11
11
  highlightedLines?: string;
12
12
  collapsedLines?: string;
13
13
  previewVisibleLines?: number | string;
14
+ showAllCode?: boolean | string;
14
15
  }
15
16
 
16
17
  const {
@@ -23,8 +24,19 @@ const {
23
24
  highlightedLines = "",
24
25
  collapsedLines = "",
25
26
  previewVisibleLines = 5,
27
+ showAllCode = false,
26
28
  } = Astro.props as Props;
27
29
 
30
+ function toBoolean(value: boolean | string | undefined, defaultValue = false) {
31
+ if (typeof value === "boolean") return value;
32
+ if (typeof value === "string") {
33
+ const normalized = value.trim().toLowerCase();
34
+ if (normalized === "true") return true;
35
+ if (normalized === "false") return false;
36
+ }
37
+ return defaultValue;
38
+ }
39
+
28
40
  function parsePositiveInteger(
29
41
  value: number | string | undefined,
30
42
  fallback: number,
@@ -43,6 +55,8 @@ function parsePositiveInteger(
43
55
 
44
56
  const visibleLines = parsePositiveInteger(previewVisibleLines, 5);
45
57
  const totalLineCount = Math.max(1, raw.split("\n").length);
58
+ const shouldShowAllCode = toBoolean(showAllCode, false);
59
+ const isInitiallyExpanded = shouldShowAllCode || totalLineCount <= visibleLines;
46
60
  ---
47
61
 
48
62
  <div
@@ -56,7 +70,7 @@ const totalLineCount = Math.max(1, raw.split("\n").length);
56
70
  </div>
57
71
  <div
58
72
  class="rd-component-preview__code not-prose relative"
59
- data-rd-preview-expanded="false"
73
+ data-rd-preview-expanded={isInitiallyExpanded ? "true" : "false"}
60
74
  style={{ "--rd-preview-visible-lines": String(visibleLines) }}
61
75
  >
62
76
  <CodeBlock
@@ -123,7 +137,14 @@ const totalLineCount = Math.max(1, raw.split("\n").length);
123
137
  }
124
138
  </style>
125
139
 
126
- <script is:inline define:vars={{ visibleLines, totalLineCount }}>
140
+ <script
141
+ is:inline
142
+ define:vars={{
143
+ visibleLines,
144
+ totalLineCount,
145
+ shouldShowAllCode,
146
+ }}
147
+ >
127
148
  (() => {
128
149
  const script = document.currentScript;
129
150
  if (!(script instanceof HTMLScriptElement)) return;
@@ -161,7 +182,7 @@ const totalLineCount = Math.max(1, raw.split("\n").length);
161
182
 
162
183
  syncExpandedHeight();
163
184
 
164
- if (totalLineCount <= visibleLines) {
185
+ if (shouldShowAllCode || totalLineCount <= visibleLines) {
165
186
  codeWrapper.dataset.rdPreviewExpanded = "true";
166
187
  window.addEventListener("resize", syncExpandedHeight, { passive: true });
167
188
  return;
@@ -24,10 +24,15 @@ type ParsedCodeMeta = {
24
24
  collapsedLines: string;
25
25
  };
26
26
 
27
+ type MdxJsxAttributeValueExpressionNode = {
28
+ type: "mdxJsxAttributeValueExpression";
29
+ value?: string | null;
30
+ };
31
+
27
32
  type MdxJsxAttributeNode = {
28
33
  type: "mdxJsxAttribute";
29
34
  name: string;
30
- value: string;
35
+ value: string | null | MdxJsxAttributeValueExpressionNode;
31
36
  };
32
37
 
33
38
  type MdxJsxFlowElementNode = {
@@ -146,6 +151,45 @@ function createAttribute(name: string, value: string): MdxJsxAttributeNode {
146
151
  };
147
152
  }
148
153
 
154
+ function parseBooleanAttributeValue(
155
+ value: MdxJsxAttributeNode["value"],
156
+ ): boolean {
157
+ if (value === null) return true;
158
+
159
+ if (typeof value === "string") {
160
+ const normalized = value.trim().toLowerCase();
161
+ if (normalized === "" || normalized === "true") return true;
162
+ if (normalized === "false") return false;
163
+ return false;
164
+ }
165
+
166
+ if (
167
+ value?.type === "mdxJsxAttributeValueExpression" &&
168
+ typeof value.value === "string"
169
+ ) {
170
+ const normalized = value.value.trim().toLowerCase();
171
+ if (normalized === "true") return true;
172
+ if (normalized === "false") return false;
173
+ }
174
+
175
+ return false;
176
+ }
177
+
178
+ function readBooleanAttribute(
179
+ attributes: MdxJsxAttributeNode[] | undefined,
180
+ attributeName: string,
181
+ ): boolean {
182
+ if (!Array.isArray(attributes)) return false;
183
+
184
+ const attribute = attributes.find(
185
+ (candidate) =>
186
+ candidate.type === "mdxJsxAttribute" && candidate.name === attributeName,
187
+ );
188
+ if (!attribute) return false;
189
+
190
+ return parseBooleanAttributeValue(attribute.value);
191
+ }
192
+
149
193
  function parseComponentPreviewChildren(rawCode: string): unknown[] {
150
194
  const parsedTree = fromMarkdown(rawCode, {
151
195
  extensions: [mdxjs()],
@@ -190,10 +234,7 @@ function getNearestMdxJsxFlowElementName(
190
234
  ancestors: unknown[],
191
235
  ): string | null {
192
236
  for (let index = ancestors.length - 1; index >= 0; index -= 1) {
193
- const ancestor = ancestors[index] as {
194
- type?: string;
195
- name?: string | null;
196
- };
237
+ const ancestor = ancestors[index] as MdxJsxFlowElementNode;
197
238
  if (ancestor?.type !== "mdxJsxFlowElement") continue;
198
239
  return typeof ancestor.name === "string" ? ancestor.name : null;
199
240
  }
@@ -201,6 +242,20 @@ function getNearestMdxJsxFlowElementName(
201
242
  return null;
202
243
  }
203
244
 
245
+ function getNearestMdxJsxFlowElement(
246
+ ancestors: unknown[],
247
+ elementName: string,
248
+ ): MdxJsxFlowElementNode | null {
249
+ for (let index = ancestors.length - 1; index >= 0; index -= 1) {
250
+ const ancestor = ancestors[index] as MdxJsxFlowElementNode;
251
+ if (ancestor?.type !== "mdxJsxFlowElement") continue;
252
+ if (ancestor.name !== elementName) continue;
253
+ return ancestor;
254
+ }
255
+
256
+ return null;
257
+ }
258
+
204
259
  function isInsideMdxJsxTextElement(ancestors: unknown[]): boolean {
205
260
  return ancestors.some((ancestor) => {
206
261
  const node = ancestor as { type?: string };
@@ -234,6 +289,9 @@ export const remarkCodeBlockComponent: Plugin<[], Root> = () => {
234
289
  const isInsideCodeGroup = nearestMdxFlowElementName === "CodeGroup";
235
290
  const isInsideComponentPreview =
236
291
  nearestMdxFlowElementName === COMPONENT_PREVIEW_NAME;
292
+ const componentPreviewNode = isInsideComponentPreview
293
+ ? getNearestMdxJsxFlowElement(ancestors, COMPONENT_PREVIEW_NAME)
294
+ : null;
237
295
  const isInsideInlineMdx = isInsideMdxJsxTextElement(ancestors);
238
296
  if (isInsideInlineMdx) return;
239
297
 
@@ -317,6 +375,13 @@ export const remarkCodeBlockComponent: Plugin<[], Root> = () => {
317
375
  const previewAttributes = attributes.filter(
318
376
  (attribute) => attribute.name !== "inCodeGroup",
319
377
  );
378
+ const showAllCode = readBooleanAttribute(
379
+ componentPreviewNode?.attributes,
380
+ "showAllCode",
381
+ );
382
+ if (showAllCode) {
383
+ previewAttributes.push(createAttribute("showAllCode", "true"));
384
+ }
320
385
  const previewNode: MdxJsxFlowElementNode = {
321
386
  type: "mdxJsxFlowElement",
322
387
  name: COMPONENT_PREVIEW_BLOCK_NAME,