mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-27 07:46:25 +00:00
feat(core): tags and types are names, one type per page implying a tag
Tags no longer have pages. A page has at most one type, which implies a tag of the same name. Tags are stored sorted. Adds rename_tag and remove_tag. Schema v6 turns tag pages into notes and trims types to the first one. History exports synapse-type:: and reads the old forms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
39e7e43071
commit
e2ed9054c6
11 files changed
+373
-328
No files matched your search
@@ -23,7 +23,9 @@ 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`).
|
||||
/// Page property with the page's type (`synapse-type:: persona`).
|
||||
pub const TYPE_PROP: &str = "synapse-type";
|
||||
/// Retired list form of the type, still read on restore (the first entry wins).
|
||||
pub const TYPES_PROP: &str = "synapse-types";
|
||||
|
||||
/// Encodes a page title as a file name ("triple-lowbar" format: `/` becomes `___`,
|
||||
@@ -185,8 +187,6 @@ pub fn render_page(graph: &Graph, page: &Page, referenced: &HashSet<BlockId>) ->
|
||||
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(
|
||||
@@ -209,11 +209,8 @@ pub fn render_page(graph: &Graph, page: &Page, referenced: &HashSet<BlockId>) ->
|
||||
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()),
|
||||
);
|
||||
if let Some(t) = page.page_type() {
|
||||
props.insert(TYPE_PROP.into(), Value::String(t.to_string()));
|
||||
}
|
||||
let mut list = sorted_props(&props);
|
||||
let aliases = graph.aliases_of(page.id)?;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
//! Parser for the Markdown files produced by [`crate::export`] (and plain outline pages).
|
||||
|
||||
use crate::export::{DESCRIPTION_PROP, HUBS_PROP, KIND_PROP, TAGS_PROP, TYPES_PROP, parse_value};
|
||||
use crate::export::{
|
||||
DESCRIPTION_PROP, HUBS_PROP, KIND_PROP, TAGS_PROP, TYPE_PROP, TYPES_PROP, parse_value,
|
||||
};
|
||||
use serde_json::Value;
|
||||
use synapse_core::{BlockId, Properties, parse::property_split};
|
||||
|
||||
@@ -21,15 +23,13 @@ pub struct ParsedPage {
|
||||
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::`.
|
||||
/// Tag names from `synapse-tags::`.
|
||||
pub tags: Vec<String>,
|
||||
/// Names from `synapse-types::`.
|
||||
/// The type from `synapse-type::` (or the first of the old `synapse-types::`): 0 or 1 name.
|
||||
pub types: Vec<String>,
|
||||
pub blocks: Vec<ParsedBlock>,
|
||||
}
|
||||
@@ -120,7 +120,6 @@ 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(),
|
||||
@@ -148,17 +147,17 @@ pub fn parse_page(text: &str) -> ParsedPage {
|
||||
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(),
|
||||
let old_types = page.properties.remove(TYPES_PROP);
|
||||
let ty = match (page.properties.remove(TYPE_PROP), old_types) {
|
||||
(Some(Value::String(s)), _) => Some(s),
|
||||
(_, Some(Value::Array(items))) => items.into_iter().find_map(|v| match v {
|
||||
Value::String(s) => Some(s),
|
||||
_ => None,
|
||||
}),
|
||||
(_, Some(Value::String(s))) => Some(s),
|
||||
_ => None,
|
||||
};
|
||||
page.types = ty.into_iter().filter(|s| !s.trim().is_empty()).collect();
|
||||
|
||||
for raw in raws {
|
||||
let mut lines = raw.lines;
|
||||
|
||||
@@ -53,7 +53,6 @@ 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>,
|
||||
@@ -172,7 +171,6 @@ fn plan(graph: &Graph, title: &str, parsed: &ParsedPage, steal: bool) -> Result<
|
||||
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(),
|
||||
@@ -223,8 +221,6 @@ fn apply_kind(graph: &Graph, plan: &Plan) -> Result<bool> {
|
||||
};
|
||||
let kind = if plan.is_hub {
|
||||
PageKind::Hub
|
||||
} else if plan.is_tag {
|
||||
PageKind::Tag
|
||||
} else {
|
||||
PageKind::Note
|
||||
};
|
||||
|
||||
@@ -348,25 +348,25 @@ fn tags_round_trip_through_snapshots() {
|
||||
let note = add(&g, "Note", &["body"]);
|
||||
g.add_page_tag(note.id, "tecnología").unwrap();
|
||||
g.add_page_tag(note.id, "idea").unwrap();
|
||||
g.add_page_type(note.id, "concepto").unwrap();
|
||||
g.set_page_type(note.id, Some("concepto")).unwrap();
|
||||
let s1 = h.snapshot(&g, T0 + 500).unwrap().unwrap();
|
||||
let tag_md = h.page_at(&s1.id, "idea").unwrap().unwrap();
|
||||
assert!(tag_md.contains("synapse-kind:: tag"), "{tag_md}");
|
||||
assert!(h.page_at(&s1.id, "idea").unwrap().is_none());
|
||||
let md = h.page_at(&s1.id, "Note").unwrap().unwrap();
|
||||
assert!(
|
||||
md.contains(r#"synapse-tags:: ["tecnología","idea"]"#),
|
||||
md.contains(r#"synapse-tags:: ["idea","tecnología"]"#),
|
||||
"{md}"
|
||||
);
|
||||
assert!(md.contains("synapse-type:: concepto"), "{md}");
|
||||
|
||||
at(&g, T0 + 1000);
|
||||
g.set_page_tags(note.id, vec![]).unwrap();
|
||||
g.set_page_types(note.id, vec![]).unwrap();
|
||||
g.set_page_type(note.id, None).unwrap();
|
||||
h.restore_graph(&g, &s1.id, T0 + 2000).unwrap();
|
||||
assert_eq!(g.get_page(note.id).unwrap().unwrap().types, ["concepto"]);
|
||||
assert!(g.page_by_title("idea").unwrap().unwrap().is_tag());
|
||||
assert!(g.page_by_title("idea").unwrap().is_none());
|
||||
assert_eq!(
|
||||
g.get_page(note.id).unwrap().unwrap().tags,
|
||||
["tecnología", "idea"]
|
||||
["idea", "tecnología"]
|
||||
);
|
||||
assert_eq!(g.pages_with_tag("idea").unwrap(), [note.id]);
|
||||
}
|
||||
@@ -446,3 +446,19 @@ fn hubs_round_trip_kind_description_and_memberships() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn old_tag_kind_exports_parse_as_plain_notes() {
|
||||
let parsed = synapse_history::parse::parse_page("synapse-kind:: tag\n\n- body\n");
|
||||
assert!(!parsed.is_hub);
|
||||
assert!(!parsed.properties.contains_key("synapse-kind"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn old_types_list_restores_its_first_entry() {
|
||||
let parsed =
|
||||
synapse_history::parse::parse_page("synapse-types:: [\"persona\",\"lugar\"]\n\n- x\n");
|
||||
assert_eq!(parsed.types, ["persona"]);
|
||||
let parsed = synapse_history::parse::parse_page("synapse-type:: lugar\n\n- x\n");
|
||||
assert_eq!(parsed.types, ["lugar"]);
|
||||
}
|
||||
Reference in new issue
Block a user