kogral/docs/book/architecture/adrs/004-logseq-blocks-support.html
2026-01-23 16:11:07 +00:00

392 lines
20 KiB
HTML

<!DOCTYPE HTML>
<html lang="en" class="rust sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>ADR-004: Logseq Blocks Support - KOGRAL Documentation</title>
<!-- Custom HTML head -->
<meta name="description" content="Complete documentation for KOGRAL - Git-native knowledge graphs for developer teams">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../../favicon.svg">
<link rel="shortcut icon" href="../../favicon.png">
<link rel="stylesheet" href="../../css/variables.css">
<link rel="stylesheet" href="../../css/general.css">
<link rel="stylesheet" href="../../css/chrome.css">
<link rel="stylesheet" href="../../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" id="highlight-css" href="../../highlight.css">
<link rel="stylesheet" id="tomorrow-night-css" href="../../tomorrow-night.css">
<link rel="stylesheet" id="ayu-highlight-css" href="../../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root and default themes to javascript -->
<script>
const path_to_root = "../../";
const default_light_theme = "rust";
const default_dark_theme = "navy";
</script>
<!-- Start loading toc.js asap -->
<script src="../../toc.js"></script>
</head>
<body>
<div id="mdbook-help-container">
<div id="mdbook-help-popup">
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
<div>
<p>Press <kbd></kbd> or <kbd></kbd> to navigate between chapters</p>
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
<p>Press <kbd>?</kbd> to show this help</p>
<p>Press <kbd>Esc</kbd> to hide this help</p>
</div>
</div>
</div>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
let theme = localStorage.getItem('mdbook-theme');
let sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('rust')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
let sidebar = null;
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">KOGRAL Documentation</h1>
<div class="right-buttons">
<a href="../../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/your-org/knowledge-base" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/your-org/knowledge-base/edit/main/docs/./architecture/adrs/004-logseq-blocks-support.md" title="Suggest an edit" aria-label="Suggest an edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="adr-004-logseq-blocks-support"><a class="header" href="#adr-004-logseq-blocks-support">ADR-004: Logseq Blocks Support</a></h1>
<h2 id="status"><a class="header" href="#status">Status</a></h2>
<p><strong>Proposed</strong> (Design phase)</p>
<h2 id="context"><a class="header" href="#context">Context</a></h2>
<p>Logseq uses <strong>content blocks</strong> as the fundamental unit of information, not full documents. KB currently treats <code>Node.content</code> as flat markdown string, which loses block-level features on import/export:</p>
<p><strong>Lost features</strong>:</p>
<ul>
<li>Block properties (<code>#card</code>, <code>TODO</code>, custom properties)</li>
<li>Block hierarchy (outliner nesting)</li>
<li>Block references (<code>((block-uuid))</code>)</li>
<li>Block-level queries (find all flashcards, TODOs)</li>
</ul>
<p><strong>User requirement</strong>: Round-trip Logseq import/export with full fidelity:</p>
<pre><code>Logseq → KOGRAL Import → KOGRAL Storage → KOGRAL Export → Logseq
(blocks preserved at every step)
</code></pre>
<h2 id="decision"><a class="header" href="#decision">Decision</a></h2>
<p>Implement <strong>Hybrid Block Support</strong> (structured + markdown):</p>
<h3 id="1-add-block-data-structure"><a class="header" href="#1-add-block-data-structure">1. Add Block Data Structure</a></h3>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub struct Block {
pub id: String, // UUID
pub content: String, // Block text
pub properties: BlockProperties, // Tags, status, custom
pub children: Vec&lt;Block&gt;, // Nested blocks
// ... timestamps ...
}
pub struct BlockProperties {
pub tags: Vec&lt;String&gt;, // #card, #important
pub status: Option&lt;TaskStatus&gt;, // TODO, DONE, etc.
pub custom: HashMap&lt;String, String&gt;, // property:: value
pub block_refs: Vec&lt;String&gt;, // ((uuid))
pub page_refs: Vec&lt;String&gt;, // [[page]]
}
<span class="boring">}</span></code></pre></pre>
<h3 id="2-extend-node-model"><a class="header" href="#2-extend-node-model">2. Extend Node Model</a></h3>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub struct Node {
// ... existing fields ...
pub content: String, // Source of truth (markdown)
pub blocks: Option&lt;Vec&lt;Block&gt;&gt;, // Cached structure (optional)
}
<span class="boring">}</span></code></pre></pre>
<h3 id="3-bidirectional-parser"><a class="header" href="#3-bidirectional-parser">3. Bidirectional Parser</a></h3>
<ul>
<li><strong>Parse</strong>: Markdown → <code>Vec&lt;Block&gt;</code> (lazy, on-demand)</li>
<li><strong>Serialize</strong>: <code>Vec&lt;Block&gt;</code> → Markdown (for export)</li>
</ul>
<h3 id="4-storage-strategy"><a class="header" href="#4-storage-strategy">4. Storage Strategy</a></h3>
<p><strong>Filesystem</strong> (git-friendly, Logseq-compatible):</p>
<pre><code class="language-markdown">- Block 1 #card
- Nested answer
- TODO Block 2
priority:: high
</code></pre>
<p><strong>SurrealDB</strong> (queryable):</p>
<pre><code class="language-sql">DEFINE TABLE block;
DEFINE FIELD node_id ON block TYPE record(node);
DEFINE FIELD block_id ON block TYPE string;
DEFINE FIELD properties ON block TYPE object;
DEFINE INDEX block_tags ON block COLUMNS properties.tags;
</code></pre>
<h3 id="5-query-extensions"><a class="header" href="#5-query-extensions">5. Query Extensions</a></h3>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>// Find all flashcards
graph.find_blocks_by_tag("card")
// Find all TODOs
graph.find_all_todos()
// Find blocks with custom property
node.find_blocks_by_property("priority", "high")
<span class="boring">}</span></code></pre></pre>
<h2 id="consequences"><a class="header" href="#consequences">Consequences</a></h2>
<h3 id="positive"><a class="header" href="#positive">Positive</a></h3>
<p><strong>Full Logseq Compatibility</strong> - Import/export preserves all block features
<strong>Queryable Blocks</strong> - Find #card, TODO, custom properties across KOGRAL
<strong>Backward Compatible</strong> - Existing nodes without blocks still work
<strong>Type-Safe</strong> - Structured data instead of regex parsing everywhere
<strong>Extensible</strong> - Custom block properties supported
<strong>Hierarchy Preserved</strong> - Nested blocks maintain parent-child relationships</p>
<h3 id="negative"><a class="header" href="#negative">Negative</a></h3>
<p>⚠️ <strong>Added Complexity</strong> - New data structures, parser, sync logic
⚠️ <strong>Dual Representation</strong> - Must keep <code>content</code> and <code>blocks</code> in sync
⚠️ <strong>Storage Overhead</strong> - SurrealDB stores both markdown and structure
⚠️ <strong>Migration Required</strong> - Existing data needs parsing to populate blocks</p>
<h3 id="neutral"><a class="header" href="#neutral">Neutral</a></h3>
<p>⚙️ <strong>Lazy Parsing</strong> - Blocks parsed on-demand (not stored by default)
⚙️ <strong>Opt-In</strong> - Config flag <code>blocks.enabled</code> to activate features
⚙️ <strong>Gradual Adoption</strong> - Can implement in phases</p>
<h2 id="implementation-phases"><a class="header" href="#implementation-phases">Implementation Phases</a></h2>
<p><strong>Phase 1: Foundation</strong> (No behavior change)</p>
<ul>
<li>Add <code>Block</code> struct to <code>models/block.rs</code></li>
<li>Add optional <code>blocks</code> field to <code>Node</code></li>
<li>Add config: <code>blocks.enabled = false</code> (default off)</li>
</ul>
<p><strong>Phase 2: Parser</strong></p>
<ul>
<li>Implement <code>BlockParser::parse()</code> (markdown → blocks)</li>
<li>Implement <code>BlockParser::serialize()</code> (blocks → markdown)</li>
<li>Add <code>Node::get_blocks()</code> method (lazy parsing)</li>
</ul>
<p><strong>Phase 3: Logseq Integration</strong></p>
<ul>
<li>Update <code>LogseqImporter</code> to parse blocks</li>
<li>Update <code>LogseqExporter</code> to serialize blocks</li>
<li>Test round-trip (Logseq → KB → Logseq)</li>
</ul>
<p><strong>Phase 4: Query API</strong></p>
<ul>
<li>Add <code>Graph::find_blocks_by_tag()</code></li>
<li>Add <code>Graph::find_all_todos()</code></li>
<li>Add <code>Node::find_blocks_by_property()</code></li>
</ul>
<p><strong>Phase 5: MCP/CLI Integration</strong></p>
<ul>
<li>Add <code>kb/find_blocks</code> MCP tool</li>
<li>Add <code>kb find-cards</code> CLI command</li>
<li>Add <code>kb find-todos</code> CLI command</li>
</ul>
<p><strong>Phase 6: SurrealDB Backend</strong></p>
<ul>
<li>Create <code>block</code> table schema</li>
<li>Index on tags, status, properties</li>
<li>Store blocks alongside nodes</li>
</ul>
<h2 id="alternatives-considered"><a class="header" href="#alternatives-considered">Alternatives Considered</a></h2>
<h3 id="alternative-1-blocks-as-first-class-nodes"><a class="header" href="#alternative-1-blocks-as-first-class-nodes">Alternative 1: Blocks as First-Class Nodes</a></h3>
<p>Convert each Logseq block to a separate KOGRAL Node.</p>
<p><strong>Rejected</strong>: Too granular, explosion of nodes, loses document context.</p>
<h3 id="alternative-2-parser-only-no-storage"><a class="header" href="#alternative-2-parser-only-no-storage">Alternative 2: Parser-Only (No Storage)</a></h3>
<p>Keep <code>content: String</code>, parse blocks on every access.</p>
<p><strong>Rejected</strong>: Can't query blocks in database, parse overhead, can't index.</p>
<h3 id="alternative-3-metadata-field"><a class="header" href="#alternative-3-metadata-field">Alternative 3: Metadata Field</a></h3>
<p>Store blocks in <code>metadata: HashMap&lt;String, Value&gt;</code>.</p>
<p><strong>Rejected</strong>: Not type-safe, harder to query, no schema validation.</p>
<h2 id="references"><a class="header" href="#references">References</a></h2>
<ul>
<li><a href="https://docs.logseq.com/#/page/blocks">Logseq Block Format</a></li>
<li><a href="../logseq-blocks-design.html">Full Design Document</a></li>
<li><a href="https://github.com/.../issues/XXX">Implementation Tracking</a></li>
</ul>
<h2 id="notes"><a class="header" href="#notes">Notes</a></h2>
<p><strong>Backward Compatibility Strategy</strong>:</p>
<ul>
<li><code>content</code> remains source of truth</li>
<li><code>blocks</code> is optional enhancement</li>
<li>Old code works unchanged</li>
<li>New features opt-in via config</li>
</ul>
<p><strong>Migration Path</strong>:</p>
<ul>
<li>Existing users: blocks disabled by default</li>
<li>New users: blocks enabled, parsed on import</li>
<li>Manual: <code>kb reindex --parse-blocks</code> to populate</li>
</ul>
<hr />
<p><strong>Decision Date</strong>: 2026-01-17
<strong>Approvers</strong>: TBD
<strong>Review Date</strong>: After Phase 2 implementation</p>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../../architecture/adrs/003-hybrid-storage.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../architecture/adrs/005-mcp-protocol.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../../architecture/adrs/003-hybrid-storage.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../architecture/adrs/005-mcp-protocol.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="../../elasticlunr.min.js"></script>
<script src="../../mark.min.js"></script>
<script src="../../searcher.js"></script>
<script src="../../clipboard.min.js"></script>
<script src="../../highlight.js"></script>
<script src="../../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>