A comprehensive read/write system for Logseq content that uses the same builder constructs for both content generation and parsing, creating a seamless unified workflow.
The Unified Builder System extends the existing DSL builder architecture with parsing capabilities, allowing you to:
- Read existing Logseq content as builder objects
- Modify content using the same fluent builder interface
- Write content back seamlessly
- Round-trip process content (read → modify → write)
This creates a unified API where the same builder classes are used for both content generation and content manipulation.
📊 PARSING LAYER
┌─────────────────────────────────────────────┐
│ BuilderParser │
│ • Analyzes content structure │
│ • Detects content types │
│ • Creates appropriate builders │
└─────────────────────────────────────────────┘
⬇️
🔧 BUILDER LAYER
┌─────────────────────────────────────────────┐
│ ContentBuilders (TaskBuilder, CodeBuilder) │
│ • Fluent interface for modification │
│ • Type-safe content construction │
│ • Chainable operations │
└─────────────────────────────────────────────┘
⬇️
📝 GENERATION LAYER
┌─────────────────────────────────────────────┐
│ ContentReconstructor │
│ • Converts builders back to content │
│ • Maintains Logseq format compatibility │
│ • Handles complex structures │
└─────────────────────────────────────────────┘
Purpose: Converts parsed Logseq content back into builder objects
Key Methods:
parse_page_to_builder(page: Page) -> PageBuilderparse_block_to_builder(block: Block) -> ContentBuilder
Content Type Detection:
- Automatically detects tasks, code blocks, math, headings, quotes, tables, queries
- Creates appropriate builder types (TaskBuilder, CodeBlockBuilder, etc.)
- Preserves content structure and metadata
Purpose: Reconstructs content using builders from parsed data
Key Methods:
reconstruct_page(page: Page) -> strreconstruct_block(block: Block) -> strmodify_and_reconstruct(page: Page, modifications: dict) -> str
Purpose: High-level loader that returns builder objects instead of model objects
Key Methods:
load_page_as_builder(page_name: str) -> PageBuilderload_all_pages_as_builders() -> Dict[str, PageBuilder]modify_page_content(page_name: str, modifier_func) -> str
The LogseqClient is extended with builder-based methods:
# Get pages and blocks as builders
page_builder = client.get_page_as_builder("My Page")
block_builder = client.get_block_as_builder("block-id")
# Modify pages using builders
def enhance_page(builder):
builder.heading(2, "Enhancement")
builder.add(TaskBuilder().todo().text("New task"))
success = client.modify_page_with_builder("My Page", enhance_page)All builder classes are enhanced with parsing methods:
# Class methods for creating builders from existing content
page_builder = PageBuilder.from_page(page_object)
task_builder = TaskBuilder.from_block(block_object)
code_builder = CodeBlockBuilder.from_content(code_string)from logseq_py.builders import BuilderBasedLoader
# Initialize loader
loader = BuilderBasedLoader("/path/to/logseq")
# Load page as builder
page_builder = loader.load_page_as_builder("My Project")
# Modify using builder methods
page_builder.add(
TaskBuilder()
.todo()
.priority("A")
.text("New high-priority task")
)
# Generate modified content
modified_content = page_builder.build()from logseq_py import LogseqClient
client = LogseqClient("/path/to/logseq")
def add_status_update(page_builder):
page_builder.heading(2, "Status Update")
page_builder.text(f"Updated on {datetime.now()}")
page_builder.add(
TaskBuilder().doing().text("Integration testing")
)
# Modify page using builder workflow
success = client.modify_page_with_builder(
"Project Status",
add_status_update
)from logseq_py.builders import ContentReconstructor
# Parse existing page
page = client.get_page("Documentation")
# Reconstruct using builders (for analysis/modification)
reconstructed = ContentReconstructor.reconstruct_page(page)
# Or modify and reconstruct in one step
enhanced = ContentReconstructor.modify_and_reconstruct(page, {
'add_tasks': [
{'content': 'Update API docs', 'status': 'TODO', 'priority': 'A'},
{'content': 'Review examples', 'status': 'LATER', 'priority': 'B'}
]
})from logseq_py.builders import BuilderBasedLoader, BuilderParser
loader = BuilderBasedLoader("/path/to/logseq")
# Load multiple pages as builders
all_builders = loader.load_all_pages_as_builders()
# Analyze and enhance content
for page_name, builder in all_builders.items():
# Add standard footer to all pages
builder.empty_line()
builder.separator()
builder.text(f"Last updated: {datetime.now()}")
# Save enhanced content
enhanced_content = builder.build()
# ... save logic- Same interface for reading and writing content
- Consistent builder patterns across all operations
- Type-safe content manipulation
- Load existing content as builders
- Modify using fluent interface
- Write back without format loss
- Automatic content type detection
- Intelligent builder selection
- Preserved structure and metadata
- Batch processing of multiple pages
- Dynamic content generation from existing data
- Automated content enhancement and maintenance
| Logseq Content | Builder Type | Detection Logic |
|---|---|---|
- TODO Task [#A] |
TaskBuilder |
Task state markers |
# Heading |
HeadingBuilder |
# prefix patterns |
code |
CodeBlockBuilder |
Code fence detection |
$$math$$ |
MathBuilder |
Math delimiters |
> Quote |
QuoteBuilder |
Quote prefix |
| Table | |
TableBuilder |
Table format detection |
query |
QueryBuilder |
Query block detection |
| Regular text | BlockBuilder |
Default fallback |
Existing Content → Parse → Analyze → Transform → Rebuild → Export
Load as Builders → Batch Modify → Generate → Save → Verify
Parse Content → Extract Patterns → Generate Enhancements → Merge → Output
- Transform content between different formats
- Preserve structure during migration
- Enhance content during the process
- Standardize formatting across pages
- Update templates and structures
- Add metadata and cross-references
- Generate summaries from existing content
- Create dashboards and analytics pages
- Build cross-reference indexes
- Apply changes across multiple pages
- Format content consistently
- Validate and fix content issues
- Extract patterns and insights
- Identify content gaps and opportunities
- Generate recommendations
- Live content modification
- Interactive content enhancement
- Dynamic content adaptation
- Graceful fallbacks for unknown content types
- Validation of builder operations
- Recovery from parsing errors
- Maintains original structure when possible
- Preserves metadata and properties
- Handles complex nested content
- Lazy parsing for large content
- Efficient builder creation
- Optimized content reconstruction
- Machine learning content classification
- Context-aware builder selection
- Semantic content understanding
- Template-based builder creation
- Rule-based content transformation
- Plugin system for custom builders
- API endpoints for remote processing
- Streaming content processing
- Real-time collaboration features
The Unified Builder System creates a complete content lifecycle management solution that:
✅ Unifies content generation and manipulation
✅ Preserves content structure and metadata
✅ Enables sophisticated content workflows
✅ Provides type-safe programmatic access
✅ Supports advanced analysis and enhancement
This system transforms Logseq content management from a simple read/write operation into a powerful, flexible, and intelligent content processing platform.
Ready for: migration, automation, analysis, enhancement, and any advanced content workflow you can imagine!