feat(core): hubs, tag pages and types, with ffi, history and import support

Hubs and templates are page kinds; tags are pages of kind tag linked by a TAGGED
edge; types are plain names on the page. Schema v5. Snapshots carry
synapse-kind/tags/types. Also adapts search, import and server tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Sonnet 5.5 committed 2026-10-26 02:45:22 +01:00
1 parent 91a8276966
commit b800248d67
35 files changed
+7983 -141

No files matched your search

+43 -1
View File
@@ -3,7 +3,8 @@
//! Layout: `pages/<encoded title>.md`, `journals/YYYY_MM_DD.md`, `assets.json`.
//! A page file starts with page properties (`key:: value`, aliases as `alias:: a, b`),
//! then one `- ` bullet per block, nested with tabs. Block properties follow the content;
//! blocks referenced by `((uuid))` carry an `id::` property.
//! blocks referenced by `((uuid))` carry an `id::` property. Hubs carry `synapse-kind:: hub`
//! and `synapse-description::`; a page in hubs lists their titles in `synapse-hubs::`.
use crate::Result;
use crate::error::Error;
@@ -15,6 +16,15 @@ use synapse_core::{Block, BlockId, Graph, Page, Properties};
pub const PAGES_DIR: &str = "pages";
pub const JOURNALS_DIR: &str = "journals";
pub const MANIFEST: &str = "assets.json";
/// Page property marking a hub or tag page (`synapse-kind:: hub|tag`); notes omit it.
pub const KIND_PROP: &str = "synapse-kind";
/// Page property holding a hub's description.
pub const DESCRIPTION_PROP: &str = "synapse-description";
/// Page property listing the titles of the hubs a page belongs to (JSON array).
pub const HUBS_PROP: &str = "synapse-hubs";
pub const TAGS_PROP: &str = "synapse-tags";
/// Page property with the type names (`synapse-types:: persona, lugar`).
pub const TYPES_PROP: &str = "synapse-types";
/// Encodes a page title as a file name (Logseq "triple-lowbar" format: `/` becomes `___`,
/// characters illegal on Windows and `%` are percent-encoded).
@@ -173,6 +183,38 @@ pub fn render_block(block: &Block, depth: usize, referenced: &HashSet<BlockId>)
pub fn render_page(graph: &Graph, page: &Page, referenced: &HashSet<BlockId>) -> Result<String> {
let mut props = page.properties.clone();
props.remove("alias");
if page.is_hub() {
props.insert(KIND_PROP.into(), Value::String("hub".into()));
} else if page.is_tag() {
props.insert(KIND_PROP.into(), Value::String("tag".into()));
}
if !page.description.is_empty() {
props.insert(
DESCRIPTION_PROP.into(),
Value::String(page.description.clone()),
);
}
let mut hubs = Vec::new();
for h in graph.hubs_of(page.id)? {
if let Some(h) = graph.get_page(h)? {
hubs.push(Value::String(h.title));
}
}
if !hubs.is_empty() {
props.insert(HUBS_PROP.into(), Value::Array(hubs));
}
if !page.tags.is_empty() {
props.insert(
TAGS_PROP.into(),
Value::Array(page.tags.iter().cloned().map(Value::String).collect()),
);
}
if !page.types.is_empty() {
props.insert(
TYPES_PROP.into(),
Value::Array(page.types.iter().cloned().map(Value::String).collect()),
);
}
let mut list = sorted_props(&props);
let aliases = graph.aliases_of(page.id)?;
if !aliases.is_empty() {
+7 -1
View File
@@ -189,14 +189,20 @@ impl History {
for rec in ops {
match rec.op {
Op::UpsertPage { page } => {
// Members list their hubs by title: a renamed hub, or one that
// became a note, changes their files.
touched.extend(graph.hub_members(page.id)?);
touched.insert(page.id);
}
Op::UpsertBlock { block } => {
touched.insert(block.page_id);
}
// A deleted hub drops out of its members' lists (their `page_hubs`
// rows outlive it when no member upsert came along).
Op::DeletePage { id } => touched.extend(graph.hub_members(id)?),
// Deletions and moves do not say which page lost a block; those
// pages are caught by the `updated_at` scan below.
Op::DeleteBlock { .. } | Op::DeletePage { .. } | Op::PutAsset { .. } => {}
Op::DeleteBlock { .. } | Op::PutAsset { .. } => {}
// The previous owner of an alias is unknown.
Op::SetAlias { .. } => everything = true,
}
+56 -2
View File
@@ -1,6 +1,6 @@
//! Parser for the Markdown files produced by [`crate::export`] (and plain Logseq pages).
use crate::export::parse_value;
use crate::export::{DESCRIPTION_PROP, HUBS_PROP, KIND_PROP, TAGS_PROP, TYPES_PROP, parse_value};
use serde_json::Value;
use synapse_core::{BlockId, Properties, parse::property_split};
@@ -16,9 +16,21 @@ pub struct ParsedBlock {
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ParsedPage {
/// Page properties except `alias`.
/// Page properties except `alias` and the `synapse-*` hub properties.
pub properties: Properties,
pub aliases: Vec<String>,
/// `synapse-kind:: hub`.
pub is_hub: bool,
/// `synapse-kind:: tag`.
pub is_tag: bool,
/// `synapse-description::`.
pub description: String,
/// Titles from `synapse-hubs::`.
pub hubs: Vec<String>,
/// Tag page titles from `synapse-tags::`.
pub tags: Vec<String>,
/// Names from `synapse-types::`.
pub types: Vec<String>,
pub blocks: Vec<ParsedBlock>,
}
@@ -106,6 +118,48 @@ pub fn parse_page(text: &str) -> ParsedPage {
}
}
let kind = page.properties.remove(KIND_PROP);
page.is_hub = matches!(&kind, Some(Value::String(k)) if k == "hub");
page.is_tag = matches!(&kind, Some(Value::String(k)) if k == "tag");
page.description = match page.properties.remove(DESCRIPTION_PROP) {
Some(Value::String(d)) => d,
Some(other) => other.to_string(),
None => String::new(),
};
page.hubs = match page.properties.remove(HUBS_PROP) {
Some(Value::Array(items)) => items
.into_iter()
.filter_map(|v| match v {
Value::String(s) => Some(s),
_ => None,
})
.collect(),
Some(Value::String(s)) => vec![s],
_ => Vec::new(),
};
page.tags = match page.properties.remove(TAGS_PROP) {
Some(Value::Array(items)) => items
.into_iter()
.filter_map(|v| match v {
Value::String(s) => Some(s),
_ => None,
})
.collect(),
Some(Value::String(s)) => vec![s],
_ => Vec::new(),
};
page.types = match page.properties.remove(TYPES_PROP) {
Some(Value::Array(items)) => items
.into_iter()
.filter_map(|v| match v {
Value::String(s) => Some(s),
_ => None,
})
.collect(),
Some(Value::String(s)) => vec![s],
_ => Vec::new(),
};
for raw in raws {
let mut lines = raw.lines;
// Blank separator lines between blocks are not content.
+154 -13
View File
@@ -8,7 +8,7 @@ use crate::parse::{ParsedPage, parse_page};
use std::cell::Cell;
use std::collections::{HashMap, HashSet};
use synapse_core::position::between;
use synapse_core::{Block, BlockId, Graph, NewPage, Page, new_id};
use synapse_core::{Block, BlockId, Graph, NewPage, Page, PageId, PageKind, new_id};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RestoreReport {
@@ -51,6 +51,14 @@ fn atomic<T>(graph: &Graph, f: impl FnOnce() -> Result<T>) -> Result<T> {
}
struct Plan {
page: PageId,
is_hub: bool,
is_tag: bool,
description: String,
/// Titles of the hubs the page belongs to.
hubs: Vec<String>,
tags: Vec<String>,
types: Vec<String>,
blocks: Vec<Block>,
delete: Vec<BlockId>,
}
@@ -67,8 +75,20 @@ fn find_or_create(graph: &Graph, title: &str) -> Result<Page> {
/// Matches parsed blocks to existing ones and decides what to write and delete.
fn plan(graph: &Graph, title: &str, parsed: &ParsedPage, steal: bool) -> Result<Plan> {
let page = find_or_create(graph, title)?;
let mut page = find_or_create(graph, title)?;
let now = graph.now_ms();
// A hub cannot hold blocks: turn it back into a note before any block is written.
if page.is_hub() && !parsed.is_hub {
page.kind = PageKind::Note;
page.updated_at = now;
graph.put_page(&page)?;
// Like `convert_to_note`: members leave it, so it never regains them by surprise.
for m in graph.hub_members(page.id)? {
graph.remove_from_hub(m, page.id)?;
}
}
// A hub file has no blocks; any left over in the page are deleted.
let parsed_blocks: &[_] = if parsed.is_hub { &[] } else { &parsed.blocks };
if page.properties != parsed.properties {
graph.set_page_properties(page.id, parsed.properties.clone())?;
@@ -89,9 +109,9 @@ fn plan(graph: &Graph, title: &str, parsed: &ParsedPage, steal: bool) -> Result<
.collect();
let by_id: HashMap<BlockId, &Block> = existing.iter().map(|b| (b.id, b)).collect();
let mut claimed: HashSet<BlockId> = HashSet::new();
let mut ids: Vec<Option<BlockId>> = vec![None; parsed.blocks.len()];
let mut ids: Vec<Option<BlockId>> = vec![None; parsed_blocks.len()];
for (i, pb) in parsed.blocks.iter().enumerate() {
for (i, pb) in parsed_blocks.iter().enumerate() {
let Some(id) = pb.id else { continue };
let usable = by_id.contains_key(&id)
|| match graph.get_block(id)? {
@@ -102,7 +122,7 @@ fn plan(graph: &Graph, title: &str, parsed: &ParsedPage, steal: bool) -> Result<
ids[i] = Some(id);
}
}
for (i, pb) in parsed.blocks.iter().enumerate() {
for (i, pb) in parsed_blocks.iter().enumerate() {
if ids[i].is_some() {
continue;
}
@@ -115,10 +135,10 @@ fn plan(graph: &Graph, title: &str, parsed: &ParsedPage, steal: bool) -> Result<
}
// Build the tree: parents from depth, positions kept when still ordered.
let mut blocks: Vec<Block> = Vec::with_capacity(parsed.blocks.len());
let mut blocks: Vec<Block> = Vec::with_capacity(parsed_blocks.len());
let mut stack: Vec<BlockId> = Vec::new();
let mut last_pos: HashMap<Option<BlockId>, String> = HashMap::new();
for (i, pb) in parsed.blocks.iter().enumerate() {
for (i, pb) in parsed_blocks.iter().enumerate() {
let id = ids[i].unwrap();
stack.truncate(pb.depth);
let parent = stack.last().copied();
@@ -149,7 +169,17 @@ fn plan(graph: &Graph, title: &str, parsed: &ParsedPage, steal: bool) -> Result<
.filter(|b| !claimed.contains(&b.id))
.map(|b| b.id)
.collect();
Ok(Plan { blocks, delete })
Ok(Plan {
page: page.id,
is_hub: parsed.is_hub,
is_tag: parsed.is_tag,
description: parsed.description.clone(),
hubs: parsed.hubs.clone(),
tags: parsed.tags.clone(),
types: parsed.types.clone(),
blocks,
delete,
})
}
fn same(a: &Block, b: &Block) -> bool {
@@ -186,6 +216,72 @@ fn apply_puts(graph: &Graph, plan: &Plan, fault: Fault) -> Result<bool> {
Ok(wrote || !plan.delete.is_empty())
}
/// Sets kind and description once the page's blocks are gone; returns whether it changed.
fn apply_kind(graph: &Graph, plan: &Plan) -> Result<bool> {
let Some(mut page) = graph.get_page(plan.page)? else {
return Ok(false);
};
let kind = if plan.is_hub {
PageKind::Hub
} else if plan.is_tag {
PageKind::Tag
} else {
PageKind::Note
};
if page.kind == kind
&& page.description == plan.description
&& page.tags == plan.tags
&& page.types == plan.types
{
return Ok(false);
}
page.kind = kind;
page.tags = plan.tags.clone();
page.types = plan.types.clone();
page.description = plan.description.clone();
page.updated_at = graph.now_ms();
graph.put_page(&page)?;
Ok(true)
}
/// Puts the page back in the hubs named by the snapshot (once every page has its kind).
/// Hubs that no longer exist or would now be invalid (e.g. a cycle) are skipped.
fn apply_hubs(graph: &Graph, plan: &Plan) -> Result<bool> {
let Some(page) = graph.get_page(plan.page)? else {
return Ok(false);
};
let mut wanted = Vec::new();
for t in &plan.hubs {
if let Some(h) = graph.page_by_title(t)?
&& h.is_hub()
&& !wanted.contains(&h.id)
{
wanted.push(h.id);
}
}
wanted.truncate(1);
if page.hubs == wanted {
return Ok(false);
}
if skip_invalid(graph.set_page_hubs(page.id, wanted.clone()))?.is_none() {
graph.set_page_hubs(page.id, Vec::new())?;
for h in wanted {
skip_invalid(graph.add_to_hub(page.id, h))?;
}
}
Ok(true)
}
/// Rule violations (cycles, pages that are no longer hubs) become `None`; storage errors
/// still abort the restore.
fn skip_invalid<T>(r: synapse_core::Result<T>) -> Result<Option<T>> {
match r {
Ok(v) => Ok(Some(v)),
Err(synapse_core::Error::Invalid(_) | synapse_core::Error::NotFound(_)) => Ok(None),
Err(e) => Err(e.into()),
}
}
fn clear_or_delete(graph: &Graph, page: &Page) -> Result<bool> {
if page.is_journal() {
let roots: Vec<_> = graph
@@ -223,12 +319,19 @@ impl History {
..Default::default()
};
let fault = &self.fault;
let this = &*self;
atomic(graph, || {
match text {
Some(text) => {
let p = plan(graph, title, &parse_page(&text), false)?;
let parsed = parse_page(&text);
let p = plan(graph, title, &parsed, false)?;
apply_deletes(graph, &p, fault)?;
apply_puts(graph, &p, fault)?;
apply_kind(graph, &p)?;
apply_hubs(graph, &p)?;
if parsed.is_hub {
this.rejoin_members(graph, commit, title, p.page)?;
}
report.restored = 1;
}
None => {
@@ -245,6 +348,38 @@ impl History {
Ok(report)
}
/// Membership lives on the members: after restoring a hub alone, puts back the existing
/// pages that were in it at `commit` (other memberships are left alone).
fn rejoin_members(
&self,
graph: &Graph,
commit: gix::ObjectId,
title: &str,
hub: PageId,
) -> Result<()> {
let key = synapse_core::title_key(title);
for (path, oid) in &self.snapshot_files(&commit.to_string())? {
let Some(member) = export::title_from_path(path) else {
continue;
};
let parsed = parse_page(&self.read_blob(*oid)?);
if !parsed
.hubs
.iter()
.any(|h| synapse_core::title_key(h) == key)
{
continue;
}
if let Some(m) = graph.page_by_title(&member)?
&& m.id != hub
{
// Skipped when it would now be invalid (e.g. a cycle).
skip_invalid(graph.add_to_hub(m.id, hub))?;
}
}
Ok(())
}
/// Restores every page to its state at `rev`; pages created since are removed.
pub fn restore_graph(
&mut self,
@@ -275,11 +410,17 @@ impl History {
for p in &plans {
apply_deletes(graph, p, fault)?;
}
for p in &plans {
if apply_puts(graph, p, fault)? {
report.restored += 1;
}
let mut changed = vec![false; plans.len()];
for (i, p) in plans.iter().enumerate() {
changed[i] = apply_puts(graph, p, fault)?;
}
for (i, p) in plans.iter().enumerate() {
changed[i] |= apply_kind(graph, p)?;
}
for (i, p) in plans.iter().enumerate() {
changed[i] |= apply_hubs(graph, p)?;
}
report.restored += changed.iter().filter(|c| **c).count();
let mut offset = 0;
let mut extra = Vec::new();
loop {