Parsing a note (vaultkg.parse)¶
vaultkg.parse
¶
parse.py -- read one Obsidian note into its structural parts.
Everything here is syntax, not interpretation: frontmatter is YAML, a heading
is an ATX # line, a link is [[...]] or [...](x.md), a typed link is
a Dataview inline field (key:: [[X]]) or a frontmatter key whose value holds
wikilinks. Nothing is inferred from prose, so the same file always yields the
same parts.
Code is not markup: wikilinks, tags and fields inside fenced blocks, inline
code spans and %% comments are masked out before any of them are matched.
Masking replaces characters with spaces, so match positions still line up with
the original text.
Heading(level, text, line, end_line)
dataclass
¶
One ATX heading and the section it opens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int
|
1-6. |
required |
text
|
str
|
Heading text with trailing |
required |
line
|
int
|
1-based line of the heading. |
required |
end_line
|
int
|
Last line of its section (before the next heading of the same or a higher level, or the end of the file). |
required |
Link(target, anchor, alias, line, embed=False, relation='', markdown=False)
dataclass
¶
One link occurrence in a note.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
Page part of the target, as written (no |
required |
anchor
|
str
|
Heading or |
required |
alias
|
str
|
Display text after |
required |
line
|
int
|
1-based line the link sits on (0 for frontmatter links). |
required |
embed
|
bool
|
True for |
False
|
relation
|
str
|
Typed-link key ( |
''
|
markdown
|
bool
|
True for a |
False
|
ParsedNote(frontmatter, frontmatter_error, body_start, lines, headings=list(), links=list(), tags=list())
dataclass
¶
Structural parts of one note.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frontmatter
|
dict[str, Any]
|
Parsed YAML mapping; empty when absent or invalid. |
required |
frontmatter_error
|
str
|
YAML error message, or empty. |
required |
body_start
|
int
|
1-based line where the body begins (after frontmatter). |
required |
lines
|
list[str]
|
All lines of the file, without newlines. |
required |
headings
|
list[Heading]
|
Headings in file order. |
list()
|
links
|
list[Link]
|
Links in file order; frontmatter links first. |
list()
|
tags
|
list[str]
|
Distinct tags, frontmatter first, without |
list()
|
body
property
¶
The note text after the frontmatter.
parse_note(text)
¶
Parse one note's text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Full file contents. |
required |
Returns:
| Type | Description |
|---|---|
ParsedNote
|
The note's frontmatter, headings, links and tags. |
Source code in src/vaultkg/parse.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
relation_name(key)
¶
Normalise a typed-link key to an edge relation: part-of -> PART_OF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Frontmatter key or inline-field key. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Upper-case relation name. |
Source code in src/vaultkg/parse.py
113 114 115 116 117 118 119 | |
split_target(raw)
¶
Split a wikilink body into (target, anchor, alias).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
str
|
Text between |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, str, str]
|
Page target, anchor after |
Source code in src/vaultkg/parse.py
122 123 124 125 126 127 128 129 130 131 | |