@entro314labs/starlight-document-converter
Version:
A comprehensive document converter for Astro Starlight that transforms various document formats into Starlight-compatible Markdown with proper frontmatter
224 lines (204 loc) • 6.14 kB
JavaScript
// src/plugins/built-in/json-processor.ts
var jsonProcessor = {
extensions: [".json"],
metadata: {
name: "json-processor",
version: "1.0.0",
description: "Converts JSON files to formatted markdown documentation",
author: "Starlight Document Converter"
},
validate: (content) => {
try {
JSON.parse(content);
return true;
} catch {
return false;
}
},
process: (content, context) => {
try {
const jsonData = JSON.parse(content);
let markdown = "";
if (isAPISpec(jsonData)) {
markdown = formatAsAPISpec(jsonData, context);
} else if (isConfigFile(jsonData)) {
markdown = formatAsConfig(jsonData, context);
} else if (isDataSchema(jsonData)) {
markdown = formatAsSchema(jsonData, context);
} else {
markdown = formatAsGenericJSON(jsonData, context);
}
return markdown;
} catch (error) {
throw new Error(`Failed to process JSON: ${error}`);
}
}
};
function isAPISpec(data) {
if (!data || typeof data !== "object" || Array.isArray(data)) {
return false;
}
const obj = data;
return !!(obj.openapi || obj.swagger || obj.paths && typeof obj.paths === "object");
}
function isConfigFile(data) {
if (!data || typeof data !== "object" || Array.isArray(data)) {
return false;
}
const obj = data;
return !!(obj.name || obj.version || obj.scripts || obj.dependencies || obj.config);
}
function isDataSchema(data) {
if (!data || typeof data !== "object" || Array.isArray(data)) {
return false;
}
const obj = data;
return !!(obj.$schema || obj.type === "object" || obj.properties);
}
function formatAsAPISpec(data, _context) {
const title = data.info?.title || "API Specification";
const description = data.info?.description || "API documentation generated from OpenAPI specification.";
const version = data.info?.version || "1.0.0";
let markdown = `# ${title}
`;
markdown += `${description}
`;
markdown += `**Version:** ${version}
`;
if (data.servers && Array.isArray(data.servers) && data.servers.length > 0) {
markdown += "## Servers\n\n";
data.servers.forEach((server) => {
const serverObj = server;
markdown += `- ${serverObj.url}`;
if (serverObj.description) {
markdown += ` - ${serverObj.description}`;
}
markdown += "\n";
});
markdown += "\n";
}
if (data.paths) {
markdown += "## API Endpoints\n\n";
Object.entries(data.paths).forEach(([path, methods]) => {
markdown += `### \`${path}\`
`;
if (typeof methods === "object" && methods !== null && !Array.isArray(methods)) {
Object.entries(methods).forEach(([method, spec]) => {
const specObj = spec;
markdown += `#### ${method.toUpperCase()}
`;
if (specObj.summary) {
markdown += `${specObj.summary}
`;
}
if (specObj.description) {
markdown += `${specObj.description}
`;
}
});
}
});
}
return markdown;
}
function formatAsConfig(data, context) {
const name = data.name || context.filename.replace(".json", "");
const description = data.description || "Configuration file documentation.";
let markdown = `# ${name}
`;
markdown += `${description}
`;
if (data.version) {
markdown += `**Version:** ${data.version}
`;
}
const ignoredKeys = ["name", "version", "description"];
const sections = Object.entries(data).filter(([key]) => !ignoredKeys.includes(key));
sections.forEach(([key, value]) => {
markdown += `## ${formatSectionTitle(key)}
`;
markdown += formatConfigValue(value);
markdown += "\n\n";
});
return markdown;
}
function formatAsSchema(data, _context) {
const title = data.title || "Data Schema";
const description = data.description || "Data schema documentation.";
let markdown = `# ${title}
`;
markdown += `${description}
`;
if (data.type) {
markdown += `**Type:** ${data.type}
`;
}
if (data.properties) {
markdown += "## Properties\n\n";
Object.entries(data.properties).forEach(([prop, spec]) => {
const specObj = spec;
markdown += `### \`${prop}\`
`;
if (specObj.type) {
markdown += `**Type:** ${specObj.type}
`;
}
if (specObj.description) {
markdown += `${specObj.description}
`;
}
if (specObj.example !== void 0) {
markdown += `**Example:** \`${specObj.example}\`
`;
}
});
}
return markdown;
}
function formatAsGenericJSON(data, context) {
const filename = context.filename.replace(".json", "");
const title = formatSectionTitle(filename);
let markdown = `# ${title}
`;
markdown += "JSON data documentation.\n\n";
markdown += "## Data Structure\n\n";
markdown += "```json\n";
markdown += JSON.stringify(data, null, 2);
markdown += "\n```\n\n";
if (typeof data === "object" && data !== null && !Array.isArray(data) && isFlattish(data)) {
markdown += "## Properties\n\n";
markdown += "| Property | Type | Value |\n";
markdown += "|----------|------|-------|\n";
Object.entries(data).forEach(([key, value]) => {
const type = Array.isArray(value) ? "array" : typeof value;
const displayValue = typeof value === "string" ? value : JSON.stringify(value);
markdown += `| ${key} | ${type} | ${displayValue} |
`;
});
}
return markdown;
}
function formatSectionTitle(key) {
return key.replace(/[_-]/g, " ").replace(/\b\w/g, (l) => l.toUpperCase());
}
function formatConfigValue(value) {
if (typeof value === "object" && value !== null) {
if (Array.isArray(value)) {
return value.map((item) => `- ${item}`).join("\n");
}
return Object.entries(value).map(([k, v]) => `- **${k}**: ${JSON.stringify(v)}`).join("\n");
}
return String(value);
}
function isFlattish(obj) {
if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
return false;
}
return Object.values(obj).every(
(value) => typeof value !== "object" || value === null || Array.isArray(value)
);
}
export {
jsonProcessor
};
//# sourceMappingURL=chunk-HRDYKXAM.js.map