polen
Version:
A framework for delightful GraphQL developer portals
71 lines • 2.94 kB
TypeScript
/**
* Tree-sitter GraphQL parsing with semantic analysis
*
* This module combines tree-sitter syntax parsing with GraphQL semantic
* analysis to create unified tokens for interactive code blocks.
*/
import type { CodeAnnotation } from 'codehike/code';
import { type GraphQLSchema } from 'graphql';
import * as WebTreeSitter from 'web-tree-sitter';
import type { SemanticNode } from './semantic-nodes.js';
/**
* Unified token structure that combines tree-sitter and GraphQL semantics
*
* This interface represents a single parsed token from a GraphQL document,
* enriched with semantic information from the GraphQL schema when available.
* It provides a consistent API for accessing syntax highlighting, interactivity,
* and CodeHike annotation data.
*
* @example
* ```typescript
* const tokens = await parseGraphQLWithTreeSitter(code, [], schema)
* const fieldToken = tokens.find(t => t.text === 'name')
*
* if (fieldToken?.polen.isInteractive()) {
* const url = fieldToken.polen.getReferenceUrl()
* console.log(`Navigate to: ${url}`)
* }
* ```
*/
export interface GraphQLToken {
/** Reference to the tree-sitter node that this token represents */
treeSitterNode: WebTreeSitter.Node;
/**
* Optional semantic information from GraphQL schema analysis
* This includes type information, field definitions, and validation results
*/
semantic?: SemanticNode;
/** Text content of the token (computed from tree-sitter node) */
get text(): string;
/** Start character position in the source code (computed from tree-sitter node) */
get start(): number;
/** End character position in the source code (computed from tree-sitter node) */
get end(): number;
/** Polen specific functionality for interactive GraphQL documentation */
polen: {
/** Check if this token should be interactive (clickable/hoverable) */
isInteractive: () => boolean;
/** Get the reference URL for navigation, or null if not applicable */
getReferenceUrl: () => string | null;
};
/** Syntax highlighting functionality */
highlighter: {
/** Get the CSS class name for styling this token */
getCssClass: () => string;
};
/** CodeHike integration for enhanced code block features */
codeHike: {
/** Array of CodeHike annotations that apply to this token */
annotations: CodeAnnotation[];
};
}
/**
* Parse GraphQL code into interactive tokens with semantic information
*
* @param code - The raw GraphQL code to parse
* @param annotations - CodeHike annotations that might affect rendering
* @param schema - Optional GraphQL schema for semantic analysis
* @returns Array of tokens representing the parsed code
*/
export declare function parseGraphQLWithTreeSitter(code: string, annotations?: CodeAnnotation[], schema?: GraphQLSchema): Promise<GraphQLToken[]>;
//# sourceMappingURL=parser.d.ts.map