mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 23:36:20 +00:00
fix(import): handle real-world Logseq graphs
- empty bullets no longer panic the outline parser - weekday names with non-ASCII letters (miércoles) no longer panic the date parser - journal refs are recognised in the configured title format and in Logseq's default one, so graphs whose format changed keep their date links Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6a4a76670b
commit
d536192603
4 files changed
+57
-25
No files matched your search
@@ -7,17 +7,20 @@ use std::path::Path;
|
|||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// `:journal/file-name-format` (default `yyyy_MM_dd`).
|
/// `:journal/file-name-format` (default `yyyy_MM_dd`).
|
||||||
pub journal_file_format: DateFormat,
|
pub journal_file_format: DateFormat,
|
||||||
/// `:journal/page-title-format` (default `MMM do, yyyy`).
|
/// `:journal/page-title-format` first, then Logseq's default `MMM do, yyyy`: graphs whose
|
||||||
pub journal_title_format: DateFormat,
|
/// format was changed still contain references written in the old one.
|
||||||
|
pub journal_title_formats: Vec<DateFormat>,
|
||||||
/// `:file/name-format :triple-lowbar` (otherwise legacy: `.` separates namespaces).
|
/// `:file/name-format :triple-lowbar` (otherwise legacy: `.` separates namespaces).
|
||||||
pub triple_lowbar: bool,
|
pub triple_lowbar: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DEFAULT_TITLE_FORMAT: &str = "MMM do, yyyy";
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
journal_file_format: DateFormat::new("yyyy_MM_dd"),
|
journal_file_format: DateFormat::new("yyyy_MM_dd"),
|
||||||
journal_title_format: DateFormat::new("MMM do, yyyy"),
|
journal_title_formats: vec![DateFormat::new(DEFAULT_TITLE_FORMAT)],
|
||||||
triple_lowbar: false,
|
triple_lowbar: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,8 +44,10 @@ impl Config {
|
|||||||
if let Some(f) = edn_string(&text, ":journal/file-name-format") {
|
if let Some(f) = edn_string(&text, ":journal/file-name-format") {
|
||||||
c.journal_file_format = DateFormat::new(&f);
|
c.journal_file_format = DateFormat::new(&f);
|
||||||
}
|
}
|
||||||
if let Some(f) = edn_string(&text, ":journal/page-title-format") {
|
if let Some(f) = edn_string(&text, ":journal/page-title-format")
|
||||||
c.journal_title_format = DateFormat::new(&f);
|
&& f != DEFAULT_TITLE_FORMAT
|
||||||
|
{
|
||||||
|
c.journal_title_formats.insert(0, DateFormat::new(&f));
|
||||||
}
|
}
|
||||||
c.triple_lowbar = text
|
c.triple_lowbar = text
|
||||||
.split_once(":file/name-format")
|
.split_once(":file/name-format")
|
||||||
@@ -144,20 +149,20 @@ impl DateFormat {
|
|||||||
}
|
}
|
||||||
Tok::DayOrd => {
|
Tok::DayOrd => {
|
||||||
let (v, r) = num(rest, 1, 2)?;
|
let (v, r) = num(rest, 1, 2)?;
|
||||||
let r = r.get(2..).filter(|_| {
|
let suffix = r.get(..2)?.to_ascii_lowercase();
|
||||||
matches!(
|
if !matches!(suffix.as_str(), "st" | "nd" | "rd" | "th") {
|
||||||
r[..2].to_ascii_lowercase().as_str(),
|
return None;
|
||||||
"st" | "nd" | "rd" | "th"
|
}
|
||||||
)
|
let r = &r[2..];
|
||||||
})?;
|
|
||||||
(d, rest) = (Some(v), r);
|
(d, rest) = (Some(v), r);
|
||||||
}
|
}
|
||||||
Tok::Weekday => {
|
Tok::Weekday => {
|
||||||
let n = rest.chars().take_while(|c| c.is_alphabetic()).count();
|
let letters: Vec<char> =
|
||||||
if n < 3 {
|
rest.chars().take_while(|c| c.is_alphabetic()).collect();
|
||||||
|
if letters.len() < 3 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
rest = &rest[n..];
|
rest = &rest[letters.iter().map(|c| c.len_utf8()).sum::<usize>()..];
|
||||||
}
|
}
|
||||||
Tok::Lit(c) => rest = rest.strip_prefix(*c)?,
|
Tok::Lit(c) => rest = rest.strip_prefix(*c)?,
|
||||||
}
|
}
|
||||||
@@ -192,6 +197,20 @@ fn month_name(s: &str, short: bool) -> Option<(u32, &str)> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn non_ascii_weekdays_and_suffixes_do_not_panic() {
|
||||||
|
let f = DateFormat::new("E, dd-MM-yyyy");
|
||||||
|
assert_eq!(
|
||||||
|
f.parse("mié, 15-03-2024"),
|
||||||
|
NaiveDate::from_ymd_opt(2024, 3, 15)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
f.parse("miércoles, 15-03-2024"),
|
||||||
|
NaiveDate::from_ymd_opt(2024, 3, 15)
|
||||||
|
);
|
||||||
|
assert_eq!(DateFormat::new("MMM do, yyyy").parse("Mar 1é, 2024"), None);
|
||||||
|
}
|
||||||
|
|
||||||
fn d(y: i32, m: u32, d: u32) -> Option<NaiveDate> {
|
fn d(y: i32, m: u32, d: u32) -> Option<NaiveDate> {
|
||||||
NaiveDate::from_ymd_opt(y, m, d)
|
NaiveDate::from_ymd_opt(y, m, d)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -389,11 +389,11 @@ fn parse_doc(f: &FileEntry, cfg: &Config, assets: &HashMap<String, String>) -> D
|
|||||||
|
|
||||||
let mut missing = Vec::new();
|
let mut missing = Vec::new();
|
||||||
for b in &mut blocks {
|
for b in &mut blocks {
|
||||||
b.content = rewrite::rewrite_dates(&b.content, &cfg.journal_title_format);
|
b.content = rewrite::rewrite_dates(&b.content, &cfg.journal_title_formats);
|
||||||
b.content = rewrite::rewrite_assets(&b.content, assets, &mut missing);
|
b.content = rewrite::rewrite_assets(&b.content, assets, &mut missing);
|
||||||
for v in b.properties.values_mut() {
|
for v in b.properties.values_mut() {
|
||||||
if let Some(s) = v.as_str() {
|
if let Some(s) = v.as_str() {
|
||||||
let s = rewrite::rewrite_dates(s, &cfg.journal_title_format);
|
let s = rewrite::rewrite_dates(s, &cfg.journal_title_formats);
|
||||||
*v = serde_json::Value::String(rewrite::rewrite_assets(&s, assets, &mut missing));
|
*v = serde_json::Value::String(rewrite::rewrite_assets(&s, assets, &mut missing));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ fn split_props(mut lines: Vec<String>) -> (String, Properties) {
|
|||||||
}
|
}
|
||||||
let mut props = Properties::new();
|
let mut props = Properties::new();
|
||||||
let first_is_prop = lines.first().is_some_and(|l| property_split(l).is_some());
|
let first_is_prop = lines.first().is_some_and(|l| property_split(l).is_some());
|
||||||
let start = usize::from(!first_is_prop);
|
let start = usize::from(!first_is_prop).min(lines.len());
|
||||||
let mut end = start;
|
let mut end = start;
|
||||||
while end < lines.len() {
|
while end < lines.len() {
|
||||||
match property_split(&lines[end]) {
|
match property_split(&lines[end]) {
|
||||||
@@ -330,6 +330,13 @@ pub fn split_aliases(value: &str) -> Vec<String> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn empty_bullets_do_not_panic() {
|
||||||
|
for text in ["-", "-\n- a", "- a\n\t-\n", "\n-\n\n"] {
|
||||||
|
parse_markdown(text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn nesting_and_multiline() {
|
fn nesting_and_multiline() {
|
||||||
let o = parse_markdown(
|
let o = parse_markdown(
|
||||||
|
|||||||
@@ -53,9 +53,9 @@ pub fn rewrite_assets(
|
|||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Rewrites `[[Mar 15th, 2024]]` (the configured journal title format) to `[[2024-03-15]]`,
|
/// Rewrites `[[Mar 15th, 2024]]` (any of the journal title formats) to `[[2024-03-15]]`,
|
||||||
/// the title Synapse gives journal pages. Code fences are left alone.
|
/// the title Synapse gives journal pages. Code fences are left alone.
|
||||||
pub fn rewrite_dates(content: &str, fmt: &DateFormat) -> String {
|
pub fn rewrite_dates(content: &str, fmts: &[DateFormat]) -> String {
|
||||||
if !content.contains("[[") {
|
if !content.contains("[[") {
|
||||||
return content.to_string();
|
return content.to_string();
|
||||||
}
|
}
|
||||||
@@ -68,13 +68,13 @@ pub fn rewrite_dates(content: &str, fmt: &DateFormat) -> String {
|
|||||||
lines.push(if fence || !line.contains("[[") {
|
lines.push(if fence || !line.contains("[[") {
|
||||||
line.to_string()
|
line.to_string()
|
||||||
} else {
|
} else {
|
||||||
rewrite_line(line, fmt)
|
rewrite_line(line, fmts)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
lines.join("\n")
|
lines.join("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rewrite_line(line: &str, fmt: &DateFormat) -> String {
|
fn rewrite_line(line: &str, fmts: &[DateFormat]) -> String {
|
||||||
let mut out = String::with_capacity(line.len());
|
let mut out = String::with_capacity(line.len());
|
||||||
let mut rest = line;
|
let mut rest = line;
|
||||||
while let Some(open) = rest.find("[[") {
|
while let Some(open) = rest.find("[[") {
|
||||||
@@ -82,7 +82,10 @@ fn rewrite_line(line: &str, fmt: &DateFormat) -> String {
|
|||||||
let Some(close) = after.find("]]") else { break };
|
let Some(close) = after.find("]]") else { break };
|
||||||
let inner = &after[..close];
|
let inner = &after[..close];
|
||||||
out.push_str(&rest[..open + 2]);
|
out.push_str(&rest[..open + 2]);
|
||||||
match fmt.parse(inner).filter(|_| !inner.contains("[[")) {
|
let date = (!inner.contains("[["))
|
||||||
|
.then(|| fmts.iter().find_map(|f| f.parse(inner)))
|
||||||
|
.flatten();
|
||||||
|
match date {
|
||||||
Some(d) => out.push_str(&d.format("%Y-%m-%d").to_string()),
|
Some(d) => out.push_str(&d.format("%Y-%m-%d").to_string()),
|
||||||
None => out.push_str(inner),
|
None => out.push_str(inner),
|
||||||
}
|
}
|
||||||
@@ -141,13 +144,16 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rewrites_dates() {
|
fn rewrites_dates() {
|
||||||
let f = DateFormat::new("MMM do, yyyy");
|
let f = [
|
||||||
|
DateFormat::new("E, dd-MM-yyyy"),
|
||||||
|
DateFormat::new("MMM do, yyyy"),
|
||||||
|
];
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
rewrite_dates(
|
rewrite_dates(
|
||||||
"see [[Mar 15th, 2024]] and [[Other]] #[[Jan 1st, 2020]]",
|
"see [[Mar 15th, 2024]] and [[Other]] #[[Jan 1st, 2020]] [[vie, 15-03-2024]]",
|
||||||
&f
|
&f
|
||||||
),
|
),
|
||||||
"see [[2024-03-15]] and [[Other]] #[[2020-01-01]]"
|
"see [[2024-03-15]] and [[Other]] #[[2020-01-01]] [[2024-03-15]]"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user