Repository navigation
Expand file tree
/
Copy pathdocusaurus-plugin-widget-api.js
More file actions
93 lines (81 loc) · 2.35 KB
/
Copy pathdocusaurus-plugin-widget-api.js
File metadata and controls
93 lines (81 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Generates widget API doc pages from static/widget-schema/*.json at build time.
* Runs before the docs plugin loads content (register in plugins[] before presets).
*/
const fs = require("fs");
const path = require("path");
const SCHEMA_DIR = path.join(__dirname, "static/widget-schema");
const DOCS_DIR = path.join(__dirname, "docs/widget-api");
const LABEL_OVERRIDES = {
"ai-chat": "AI Chat",
"bar-chart": "Bar Chart",
"code-editor": "Code Editor",
"color-input": "Color Input",
"datetime-input": "Datetime Input",
"donut-chart": "Donut Chart",
"fused-map": "Fused Map",
"gallery-input": "Gallery Input",
"heatmap-chart": "Heatmap Chart",
"line-chart": "Line Chart",
"map-bounds": "Map Bounds",
"map-h3": "Map H3",
"number-input": "Number Input",
"pdf-gallery-viewer": "PDF Gallery Viewer",
"scatter-chart": "Scatter Chart",
"sql-runner": "SQL Runner",
"sql-table": "SQL Table",
"stacked-area-chart": "Stacked Area Chart",
"stacked-bar-chart": "Stacked Bar Chart",
"text-area": "Text Area",
"text-input": "Text Input",
"widget-builder": "Widget Builder",
};
function widgetLabel(slug) {
if (LABEL_OVERRIDES[slug]) return LABEL_OVERRIDES[slug];
return slug
.split("-")
.map((p) => p.charAt(0).toUpperCase() + p.slice(1))
.join(" ");
}
function generateWidgetMdx(slug) {
const label = widgetLabel(slug);
return `---
id: ${slug}
title: ${label}
sidebar_label: ${label}
---
import WidgetSchemaPage from '@site/src/components/WidgetSchemaPage';
<WidgetSchemaPage slug="${slug}" />
`;
}
function syncWidgetApiDocs() {
if (!fs.existsSync(DOCS_DIR)) {
fs.mkdirSync(DOCS_DIR, { recursive: true });
}
const schemas = fs
.readdirSync(SCHEMA_DIR)
.filter((f) => f.endsWith(".json"))
.map((f) => f.replace(/\.json$/, ""));
const expected = new Set(schemas.map((s) => `${s}.mdx`));
for (const file of fs.readdirSync(DOCS_DIR)) {
if (file === "overview.mdx") continue;
if (file.endsWith(".mdx") && !expected.has(file)) {
fs.unlinkSync(path.join(DOCS_DIR, file));
}
}
for (const slug of schemas) {
fs.writeFileSync(
path.join(DOCS_DIR, `${slug}.mdx`),
generateWidgetMdx(slug),
);
}
}
module.exports = function widgetApiPlugin() {
return {
name: "docusaurus-plugin-widget-api",
loadContent() {
syncWidgetApiDocs();
return null;
},
};
};