radiant-docs 0.1.24 → 0.1.25
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
|
@@ -9,6 +9,7 @@ import path from "path";
|
|
|
9
9
|
import { getConfig, validateMdxContent } from "./src/lib/validation";
|
|
10
10
|
import remarkCodeBlockComponent from "./src/lib/mdx/remark-code-block-component";
|
|
11
11
|
import remarkDemoteH1 from "./src/lib/mdx/remark-demote-h1";
|
|
12
|
+
import remarkStandaloneImageComponent from "./src/lib/mdx/remark-standalone-image-component";
|
|
12
13
|
import rehypeExternalLinks from "./src/lib/mdx/rehype-external-links";
|
|
13
14
|
import remarkGfm from "remark-gfm";
|
|
14
15
|
import rehypeSlug from "rehype-slug";
|
|
@@ -271,7 +272,12 @@ export default defineConfig({
|
|
|
271
272
|
},
|
|
272
273
|
integrations: [
|
|
273
274
|
mdx({
|
|
274
|
-
remarkPlugins: [
|
|
275
|
+
remarkPlugins: [
|
|
276
|
+
remarkGfm,
|
|
277
|
+
remarkDemoteH1,
|
|
278
|
+
remarkStandaloneImageComponent,
|
|
279
|
+
remarkCodeBlockComponent,
|
|
280
|
+
],
|
|
275
281
|
rehypePlugins: [
|
|
276
282
|
rehypeSlug,
|
|
277
283
|
rehypeExternalLinks,
|
|
@@ -27,7 +27,7 @@ validateProps(
|
|
|
27
27
|
---
|
|
28
28
|
|
|
29
29
|
<figure
|
|
30
|
-
class="p-1.5 pb-2
|
|
30
|
+
class="p-1.5 pb-2 group border border-neutral-200/80 dark:border-neutral-800 shadow-xs bg-neutral-50 dark:bg-neutral-900 rounded-2xl"
|
|
31
31
|
x-data="{
|
|
32
32
|
open: false,
|
|
33
33
|
showZoomed: false,
|
|
@@ -6,6 +6,7 @@ import { gfm } from "micromark-extension-gfm";
|
|
|
6
6
|
import { mdxjs } from "micromark-extension-mdxjs";
|
|
7
7
|
import type { Plugin } from "unified";
|
|
8
8
|
import { visitParents } from "unist-util-visit-parents";
|
|
9
|
+
import { transformStandaloneImageParagraphs } from "./remark-standalone-image-component";
|
|
9
10
|
|
|
10
11
|
type CodeNode = {
|
|
11
12
|
type: "code";
|
|
@@ -275,6 +276,7 @@ function parseComponentPreviewChildren(rawCode: string): unknown[] {
|
|
|
275
276
|
mdastExtensions: [gfmFromMarkdown(), mdxFromMarkdown()],
|
|
276
277
|
}) as Root;
|
|
277
278
|
|
|
279
|
+
transformStandaloneImageParagraphs(parsedTree);
|
|
278
280
|
transformCodeBlockNodes(parsedTree);
|
|
279
281
|
|
|
280
282
|
return transformPreviewChildren(
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { Root } from "mdast";
|
|
2
|
+
import type { Plugin } from "unified";
|
|
3
|
+
import { visitParents } from "unist-util-visit-parents";
|
|
4
|
+
|
|
5
|
+
type ParagraphNode = {
|
|
6
|
+
type: "paragraph";
|
|
7
|
+
children?: unknown[];
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type ImageNode = {
|
|
11
|
+
type: "image";
|
|
12
|
+
url?: string | null;
|
|
13
|
+
alt?: string | null;
|
|
14
|
+
title?: string | null;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type ParentNode = {
|
|
18
|
+
children?: unknown[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type MdxJsxAttributeNode = {
|
|
22
|
+
type: "mdxJsxAttribute";
|
|
23
|
+
name: string;
|
|
24
|
+
value: string | null;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type MdxJsxFlowElementNode = {
|
|
28
|
+
type: "mdxJsxFlowElement";
|
|
29
|
+
name: string;
|
|
30
|
+
attributes: MdxJsxAttributeNode[];
|
|
31
|
+
children: unknown[];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
function createAttribute(name: string, value: string): MdxJsxAttributeNode {
|
|
35
|
+
return {
|
|
36
|
+
type: "mdxJsxAttribute",
|
|
37
|
+
name,
|
|
38
|
+
value,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function transformStandaloneImageParagraphs(tree: Root): void {
|
|
43
|
+
visitParents(tree, "paragraph", (node, ancestors) => {
|
|
44
|
+
const paragraph = node as ParagraphNode;
|
|
45
|
+
if (!Array.isArray(paragraph.children) || paragraph.children.length !== 1) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const onlyChild = paragraph.children[0] as ImageNode;
|
|
50
|
+
if (onlyChild.type !== "image") return;
|
|
51
|
+
if (typeof onlyChild.url !== "string" || onlyChild.url.trim().length === 0) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const parent = ancestors[ancestors.length - 1] as ParentNode | undefined;
|
|
56
|
+
if (!Array.isArray(parent?.children)) return;
|
|
57
|
+
|
|
58
|
+
const currentIndex = parent.children.indexOf(node);
|
|
59
|
+
if (currentIndex < 0) return;
|
|
60
|
+
|
|
61
|
+
const attributes: MdxJsxAttributeNode[] = [
|
|
62
|
+
createAttribute("src", onlyChild.url),
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
if (typeof onlyChild.alt === "string") {
|
|
66
|
+
attributes.push(createAttribute("alt", onlyChild.alt));
|
|
67
|
+
}
|
|
68
|
+
if (typeof onlyChild.title === "string") {
|
|
69
|
+
attributes.push(createAttribute("title", onlyChild.title));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const replacementNode: MdxJsxFlowElementNode = {
|
|
73
|
+
type: "mdxJsxFlowElement",
|
|
74
|
+
name: "Image",
|
|
75
|
+
attributes,
|
|
76
|
+
children: [],
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
parent.children[currentIndex] = replacementNode;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export const remarkStandaloneImageComponent: Plugin<[], Root> = () => {
|
|
84
|
+
return (tree) => {
|
|
85
|
+
transformStandaloneImageParagraphs(tree);
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export default remarkStandaloneImageComponent;
|