-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeepsync_note_text.py
More file actions
149 lines (123 loc) · 4.98 KB
/
Copy pathkeepsync_note_text.py
File metadata and controls
149 lines (123 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""Reminder parsing and markdown preview helpers."""
from datetime import datetime, timezone
import re
from typing import Any, Dict, List, Optional
def parse_reminder_datetime(value: str) -> Optional[datetime]:
text = (value or "").strip()
if not text:
return None
candidates = [text, text.replace("T", " ")]
for candidate in candidates:
try:
parsed = datetime.fromisoformat(candidate)
break
except ValueError:
parsed = None
else:
parsed = None
if parsed is None:
for fmt in ("%Y-%m-%d %H:%M", "%Y-%m-%d %H:%M:%S", "%Y-%m-%d"):
try:
parsed = datetime.strptime(text, fmt)
break
except ValueError:
continue
if parsed is None:
raise ValueError("Use YYYY-MM-DD HH:MM for reminders.")
if parsed.tzinfo is None:
parsed = parsed.replace(tzinfo=datetime.now().astimezone().tzinfo)
return parsed.astimezone(timezone.utc)
def format_reminder_datetime(value: Optional[datetime]) -> str:
if not value:
return ""
return value.astimezone().strftime("%Y-%m-%d %H:%M")
def is_markdown_label(value: Any) -> bool:
"""Return True when a note label enables markdown preview mode."""
return str(value or "").strip().lower() == ".md"
def note_uses_markdown(labels: List[str]) -> bool:
return any(is_markdown_label(label) for label in labels)
def split_inline_markdown(text: str) -> List[Dict[str, str]]:
"""Split a markdown line into display segments with lightweight styles."""
pattern = re.compile(r"(`([^`]+)`|\*\*([^*]+)\*\*|__([^_]+)__|\*([^*]+)\*|_([^_]+)_)")
segments = []
cursor = 0
for match in pattern.finditer(text or ""):
if match.start() > cursor:
segments.append({"text": text[cursor:match.start()], "style": "plain"})
if match.group(2) is not None:
segments.append({"text": match.group(2), "style": "inline_code"})
elif match.group(3) is not None or match.group(4) is not None:
segments.append({"text": match.group(3) or match.group(4), "style": "bold"})
else:
segments.append({"text": match.group(5) or match.group(6), "style": "italic"})
cursor = match.end()
if cursor < len(text or ""):
segments.append({"text": text[cursor:], "style": "plain"})
return segments
def markdown_preview_blocks(markdown_text: str) -> List[Dict[str, Any]]:
"""Convert a conservative markdown subset into styled preview blocks."""
blocks = []
in_code_block = False
for raw_line in (markdown_text or "").splitlines():
stripped = raw_line.strip()
if stripped.startswith("```"):
in_code_block = not in_code_block
continue
if in_code_block:
blocks.append({
"style": "code_block",
"segments": [{"text": raw_line, "style": "plain"}],
})
continue
if not stripped:
blocks.append({"style": "blank", "segments": []})
continue
heading = re.match(r"^(#{1,6})\s+(.+)$", stripped)
if heading:
level = min(len(heading.group(1)), 3)
blocks.append({
"style": f"heading_{level}",
"segments": split_inline_markdown(heading.group(2).strip()),
})
continue
task = re.match(r"^\s*[-*+]\s+\[([ xX])\]\s+(.+)$", raw_line)
if task:
mark = "[x]" if task.group(1).lower() == "x" else "[ ]"
blocks.append({
"style": "task",
"segments": split_inline_markdown(f"{mark} {task.group(2).strip()}"),
})
continue
bullet = re.match(r"^\s*[-*+]\s+(.+)$", raw_line)
if bullet:
blocks.append({
"style": "list_item",
"segments": split_inline_markdown(f"- {bullet.group(1).strip()}"),
})
continue
numbered = re.match(r"^\s*(\d+)[.)]\s+(.+)$", raw_line)
if numbered:
blocks.append({
"style": "list_item",
"segments": split_inline_markdown(f"{numbered.group(1)}. {numbered.group(2).strip()}"),
})
continue
if stripped.startswith(">"):
blocks.append({
"style": "quote",
"segments": split_inline_markdown(stripped.lstrip("> ").strip()),
})
continue
blocks.append({
"style": "paragraph",
"segments": split_inline_markdown(raw_line.strip()),
})
return blocks
def markdown_preview_text(markdown_text: str, limit: int = 150) -> str:
preview_parts = []
for block in markdown_preview_blocks(markdown_text):
text = "".join(segment["text"] for segment in block["segments"]).strip()
if text:
preview_parts.append(text)
preview = " ".join(preview_parts)
return preview[:limit] + ("..." if len(preview) > limit else "")