6.3 KiB
NKBIP-01 Hierarchical Parsing Technical Plan
Overview
This document outlines the complete restart plan for implementing NKBIP-01 compliant hierarchical AsciiDoc parsing using proper Asciidoctor tree processor extensions.
Current State Analysis
Problems Identified
-
Dual Architecture Conflict: Two competing parsing implementations exist:
publication_tree_factory.ts- AST-first approach (currently used)publication_tree_extension.ts- Extension approach (incomplete)
-
Missing Proper Extension Registration: Current code doesn't follow the official Asciidoctor extension pattern you provided
-
Incomplete NKBIP-01 Compliance: Testing with
deep_hierarchy_test.adocmay not produce the exact structures shown indocreference.md
NKBIP-01 Specification Summary
From test_data/AsciidocFiles/docreference.md:
Event Types
- 30040: Index events (collections/hierarchical containers)
- 30041: Content events (actual article sections)
Parse Level Behaviors
- Level 2: Only
==sections → 30041 events (subsections included in content) - Level 3:
==→ 30040 indices,===→ 30041 content events - Level 4+: Full hierarchy with each level becoming separate events
Key Rules
- If a section has subsections at target level → becomes 30040 index
- If no subsections at target level → becomes 30041 content event
- Content inclusion: 30041 events include all content below parse level
- Hierarchical references: Parent indices use
atags to reference children
Proposed Architecture
Core Pattern: Asciidoctor Tree Processor Extension
Following the pattern you provided:
// Extension registration pattern
module.exports = function (registry) {
registry.treeProcessor(function () {
var self = this
self.process(function (doc) {
// Process document and build PublicationTree
return doc
})
})
}
Implementation Components
-
PublicationTreeProcessor (
src/lib/utils/publication_tree_processor.ts)- Implements the tree processor extension
- Registers with Asciidoctor during document processing
- Builds PublicationTree with NDK events during AST traversal
- Returns result via closure to avoid Ruby compatibility issues
-
Unified Parser Interface (
src/lib/utils/asciidoc_publication_parser.ts)- Single entry point for all parsing operations
- Manages extension registration and cleanup
- Provides clean API for ZettelEditor integration
-
Enhanced ZettelEditor Integration
- Replace
publication_tree_factory.tsusage - Use proper extension-based parsing
- Maintain current preview and publishing workflow
- Replace
Technical Implementation Plan
Phase 1: Core Tree Processor (publication_tree_processor.ts)
export function registerPublicationTreeProcessor(
registry: Registry,
ndk: NDK,
parseLevel: number,
options?: ProcessorOptions
): { getResult: () => ProcessorResult | null }
Key Features:
- Follows Asciidoctor extension pattern exactly
- Builds events during AST traversal (not after)
- Preserves original AsciiDoc content in events
- Handles all parse levels (2-7) with proper NKBIP-01 compliance
- Uses closure pattern to return results safely
Phase 2: Unified Parser Interface (asciidoc_publication_parser.ts)
export async function parseAsciiDocWithTree(
content: string,
ndk: NDK,
parseLevel: number = 2
): Promise<PublicationTreeResult>
Responsibilities:
- Create Asciidoctor instance
- Register tree processor extension
- Execute parsing with extension
- Return PublicationTree and events
- Clean up resources
Phase 3: ZettelEditor Integration
Changes to ZettelEditor.svelte:
- Replace
createPublicationTreeFromContent()calls - Use new
parseAsciiDocWithTree()function - Maintain existing preview/publishing interface
- No changes to component props or UI
Phase 4: Validation Testing
Test Suite:
- Parse
deep_hierarchy_test.adocat levels 2-7 - Verify event structures match
docreference.mdexamples - Validate content preservation and tag inheritance
- Test publish workflow end-to-end
File Organization
Files to Create
src/lib/utils/publication_tree_processor.ts- Core tree processor extensionsrc/lib/utils/asciidoc_publication_parser.ts- Unified parser interfacetests/unit/publication_tree_processor.test.ts- Comprehensive test suite
Files to Modify
src/lib/components/ZettelEditor.svelte- Update parsing callssrc/routes/new/compose/+page.svelte- Verify integration works
Files to Remove (After Validation)
src/lib/utils/publication_tree_factory.ts- Replace with processorsrc/lib/utils/publication_tree_extension.ts- Merge concepts into processor
Success Criteria
- NKBIP-01 Compliance: All parse levels produce structures exactly matching
docreference.md - Content Preservation: Original AsciiDoc content preserved in events (not converted to HTML)
- Proper Extension Pattern: Uses official Asciidoctor tree processor registration
- Zero Regression: Current ZettelEditor functionality unchanged
- Performance: No degradation in parsing or preview speed
- Test Coverage: Comprehensive validation with
deep_hierarchy_test.adoc
Development Sequence
- Study & Plan ✓ (Current phase)
- Implement Core Processor - Create
publication_tree_processor.ts - Build Unified Interface - Create
asciidoc_publication_parser.ts - Integrate with ZettelEditor - Update parsing calls
- Validate with Test Documents - Verify NKBIP-01 compliance
- Clean Up Legacy Code - Remove old implementations
- Documentation & Testing - Comprehensive test suite
Risk Mitigation
- Incremental Integration: Keep old code until new implementation validated
- Extensive Testing: Use both test documents for validation
- Performance Monitoring: Ensure no degradation in user experience
- Rollback Plan: Can revert to
publication_tree_factory.tsif needed
References
- NKBIP-01 Specification:
test_data/AsciidocFiles/docreference.md - Test Document:
test_data/AsciidocFiles/deep_hierarchy_test.adoc - Asciidoctor Extensions: Official Documentation
- Current Implementation:
src/lib/components/ZettelEditor.svelte:64