Skip to content

Commit bb712ed

Browse files
author
iexitdev
committed
Add editable docs playground
1 parent e717986 commit bb712ed

6 files changed

Lines changed: 1030 additions & 15 deletions

File tree

apps/site/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dependencies": {
2323
"@tailwindcss/vite": "^4.3.0",
2424
"lucide-astro": "^0.556.0",
25+
"react-live": "^4.1.8",
2526
"tailwindcss": "^4.3.0"
2627
}
2728
}

apps/site/src/lib/remark-strip-duplicate-title.mjs

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,71 @@ const escapeAttribute = (value) =>
9898
.replace(/"/g, """)
9999
.replace(/</g, "&lt;");
100100

101+
const editablePreviewIds = new Set(["line-basic"]);
102+
103+
const encodeCodeAttribute = (value) => encodeURIComponent(String(value));
104+
105+
const getPreviewHtml = (id, title) => {
106+
const titleAttribute =
107+
typeof title === "string" && title.length > 0
108+
? ` data-preview-title="${escapeAttribute(title)}"`
109+
: "";
110+
111+
return `<chart-kit-preview data-preview-id="${escapeAttribute(
112+
id
113+
)}"${titleAttribute}><div class="chart-kit-preview-fallback">Loading chart preview</div></chart-kit-preview>`;
114+
};
115+
101116
const transformPreviewDirectives = (tree) => {
117+
if (Array.isArray(tree.children)) {
118+
for (let index = 0; index < tree.children.length; index += 1) {
119+
const node = tree.children[index];
120+
121+
if (node.type !== "leafDirective" || node.name !== "chart-preview") {
122+
continue;
123+
}
124+
125+
const id = node.attributes?.id;
126+
127+
if (typeof id !== "string" || id.length === 0) {
128+
node.type = "html";
129+
node.value =
130+
'<div class="chart-kit-preview-fallback">Missing chart preview id</div>';
131+
node.children = [];
132+
continue;
133+
}
134+
135+
const previousNode = tree.children[index - 1];
136+
137+
if (
138+
editablePreviewIds.has(id) &&
139+
previousNode?.type === "code" &&
140+
["jsx", "tsx"].includes(previousNode.lang)
141+
) {
142+
const title = node.attributes?.title;
143+
const titleAttribute =
144+
typeof title === "string" && title.length > 0
145+
? ` data-preview-title="${escapeAttribute(title)}"`
146+
: "";
147+
148+
previousNode.type = "html";
149+
previousNode.value = `<chart-kit-playground data-preview-id="${escapeAttribute(
150+
id
151+
)}" data-code="${escapeAttribute(
152+
encodeCodeAttribute(previousNode.value)
153+
)}"${titleAttribute}><div class="chart-kit-preview-fallback">Loading chart playground</div></chart-kit-playground>`;
154+
previousNode.children = [];
155+
tree.children.splice(index, 1);
156+
index -= 1;
157+
continue;
158+
}
159+
160+
node.type = "html";
161+
node.value = getPreviewHtml(id, node.attributes?.title);
162+
node.children = [];
163+
}
164+
}
165+
102166
visit(tree, (node) => {
103167
if (node.type !== "leafDirective" || node.name !== "chart-preview") {
104168
return;
@@ -114,16 +178,8 @@ const transformPreviewDirectives = (tree) => {
114178
return;
115179
}
116180

117-
const title = node.attributes?.title;
118-
const titleAttribute =
119-
typeof title === "string" && title.length > 0
120-
? ` data-preview-title="${escapeAttribute(title)}"`
121-
: "";
122-
123181
node.type = "html";
124-
node.value = `<chart-kit-preview data-preview-id="${escapeAttribute(
125-
id
126-
)}"${titleAttribute}><div class="chart-kit-preview-fallback">Loading chart preview</div></chart-kit-preview>`;
182+
node.value = getPreviewHtml(id, node.attributes?.title);
127183
node.children = [];
128184
});
129185
};

0 commit comments

Comments
 (0)