Archify Diagram Viewer 0.1.0
VS Code extension that previews Archify diagrams from their JSON sources (live, as you type) and opens rendered Archify HTML in a viewer tab. Bundles the Archify 3.0.1 renderer and runs it on VS Code's Node runtime. Adds validation diagnostics, JSON schema help, source-link navigation, export saving, render-to-file and open-in-browser commands. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { parse, ParseError } from 'jsonc-parser';
|
||||
|
||||
export const DIAGRAM_TYPES = ['architecture', 'workflow', 'sequence', 'dataflow', 'lifecycle'] as const;
|
||||
export type DiagramType = (typeof DIAGRAM_TYPES)[number];
|
||||
|
||||
/** Node collection that may carry `sources` evidence, per diagram type. */
|
||||
export const EVIDENCE_COLLECTIONS: Record<DiagramType, string> = {
|
||||
architecture: 'components',
|
||||
workflow: 'nodes',
|
||||
sequence: 'participants',
|
||||
dataflow: 'nodes',
|
||||
lifecycle: 'states',
|
||||
};
|
||||
|
||||
const FILENAME_RE = /\.(architecture|workflow|sequence|dataflow|lifecycle)\.json$/i;
|
||||
const MAX_SNIFF_BYTES = 8 * 1024 * 1024;
|
||||
|
||||
export function isDiagramType(value: unknown): value is DiagramType {
|
||||
return typeof value === 'string' && (DIAGRAM_TYPES as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
export function typeFromFilename(fileName: string): DiagramType | undefined {
|
||||
const match = FILENAME_RE.exec(fileName);
|
||||
return match ? (match[1].toLowerCase() as DiagramType) : undefined;
|
||||
}
|
||||
|
||||
export interface DiagramSource {
|
||||
type: DiagramType;
|
||||
diagram: Record<string, unknown>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses JSON text and returns the diagram when it declares an Archify
|
||||
* `diagram_type`. Falls back to the file name for documents that do not
|
||||
* parse yet, so a half-typed source still counts as a diagram.
|
||||
*/
|
||||
export function readDiagramSource(text: string, fileName = ''): DiagramSource | undefined {
|
||||
if (text.length > MAX_SNIFF_BYTES) return undefined;
|
||||
const errors: ParseError[] = [];
|
||||
const value = parse(text, errors, { allowTrailingComma: true });
|
||||
if (value && typeof value === 'object' && !Array.isArray(value) && isDiagramType(value.diagram_type)) {
|
||||
return { type: value.diagram_type, diagram: value };
|
||||
}
|
||||
const byName = typeFromFilename(fileName);
|
||||
if (byName) {
|
||||
const diagram = value && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
||||
return { type: byName, diagram };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** True when the HTML was produced by the Archify renderer. */
|
||||
export function isArchifyHtml(text: string): boolean {
|
||||
const head = text.slice(0, 4096);
|
||||
return /<meta\s+name=["']generator["']\s+content=["']archify\b/i.test(head);
|
||||
}
|
||||
|
||||
export function hasSourceEvidence(type: DiagramType, diagram: Record<string, unknown>): boolean {
|
||||
const meta = diagram.meta as Record<string, unknown> | undefined;
|
||||
if (meta && meta.repository) return true;
|
||||
const nodes = diagram[EVIDENCE_COLLECTIONS[type]];
|
||||
return Array.isArray(nodes) && nodes.some((node) => Array.isArray(node?.sources) && node.sources.length > 0);
|
||||
}
|
||||
|
||||
/** Returns a copy of the diagram with repository evidence removed. */
|
||||
export function stripSourceEvidence(type: DiagramType, diagram: Record<string, unknown>): Record<string, unknown> {
|
||||
const copy = structuredClone(diagram);
|
||||
const meta = copy.meta as Record<string, unknown> | undefined;
|
||||
if (meta) delete meta.repository;
|
||||
const nodes = copy[EVIDENCE_COLLECTIONS[type]];
|
||||
if (Array.isArray(nodes)) {
|
||||
for (const node of nodes) {
|
||||
if (node && typeof node === 'object') delete node.sources;
|
||||
}
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
Reference in New Issue
Block a user