Markdown Module
Content
Represents the body of a note, organized into titled sections.
Attributes:
| Name | Type | Description |
|---|---|---|
sections |
Dict[str, ContentSection]
|
A dictionary mapping section titles to ContentSection objects. |
Source code in ures/markdown/manipulator.py
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 | |
__init__()
Initializes an empty Content container.
Source code in ures/markdown/manipulator.py
49 50 51 | |
add_content(content, section_title='default', section_level=1)
Adds content to a specific section, creating the section if it does not exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
(str, list)
|
The text content to add. |
required |
section_title
|
str
|
The title of the target section. Defaults to "default". |
'default'
|
section_level
|
int
|
The heading level if a new section needs to be created. Defaults to 1. |
1
|
Source code in ures/markdown/manipulator.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
add_section(section)
Adds an existing ContentSection object to the content.
If a section with the same title already exists, this operation is ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
section
|
ContentSection
|
The section object to add. |
required |
Source code in ures/markdown/manipulator.py
63 64 65 66 67 68 69 70 71 72 | |
new_section(title, level)
Creates a new empty section if it does not already exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
The unique title of the section. |
required |
level
|
int
|
The Markdown heading level for the section. |
required |
Source code in ures/markdown/manipulator.py
53 54 55 56 57 58 59 60 61 | |
to_string()
Serializes the entire content into a Markdown-formatted string.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The complete Markdown content with section headers. |
Source code in ures/markdown/manipulator.py
91 92 93 94 95 96 97 98 99 100 101 102 103 | |
ContentSection
Represents a specific section within a note, containing a title, hierarchy level, and text content.
Attributes:
| Name | Type | Description |
|---|---|---|
title |
str
|
The title of the section (e.g., "Introduction"). |
level |
int
|
The heading level of the section (e.g., 1 for #, 2 for ##). |
content |
List[str]
|
A list of strings representing the lines of content in this section. |
Source code in ures/markdown/manipulator.py
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 | |
__init__(title, level)
Initializes a ContentSection with a title and a heading level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
The title of the section. |
required |
level
|
int
|
The Markdown heading level (e.g., 1, 2, 3). |
required |
Source code in ures/markdown/manipulator.py
19 20 21 22 23 24 25 26 27 28 | |
add_content(content)
Appends text content to this section.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
Union[str, List[str]]
|
A single string line or a list of string lines to add. |
required |
Source code in ures/markdown/manipulator.py
30 31 32 33 34 35 36 37 38 39 | |
MarkdownDocument
A low-level class for manipulating Markdown files with front matter.
This class provides methods to add and modify Markdown content and front matter, supporting nested structures in front matter (e.g., dictionaries within YAML front matter).
Source code in ures/markdown/manipulator.py
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 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 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | |
content
property
writable
Retrieves the Markdown content.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The Markdown content. |
Example
doc = MarkdownDocument(content="# Hello World") doc.content "# Hello World"
metadata
property
writable
Retrieves the front matter metadata.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: The metadata dictionary. |
Example
doc = MarkdownDocument(metadata={"title": "Greeting", "tags": ["intro", "welcome"]}) doc.metadata {"title": "Greeting", "tags": ["intro", "welcome"]}
__init__(content='', metadata=None)
Initializes a new MarkdownDocument instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
The Markdown content. Defaults to an empty string. |
''
|
metadata
|
Optional[Dict[str, Any]]
|
The front matter metadata as a dictionary. Defaults to None. |
None
|
Example
doc = MarkdownDocument( ... content="# Hello World", ... metadata={"title": "Greeting", "tags": ["intro", "welcome"]} ... )
Source code in ures/markdown/manipulator.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
add_content(content, append=True)
Adds content to the Markdown document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
The Markdown content to add. |
required |
append
|
bool
|
If True, appends to existing content; otherwise, prepends. Defaults to True. |
True
|
Example
doc = MarkdownDocument() doc.add_content("# Introduction") doc.add_content("Some introductory text.", append=True)
Source code in ures/markdown/manipulator.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
clear_content()
Clears all Markdown content, leaving only the front matter.
Example
doc = MarkdownDocument(content="# Hello World") doc.clear_content() print(doc.content) ""
Source code in ures/markdown/manipulator.py
582 583 584 585 586 587 588 589 590 591 592 | |
clear_frontmatter()
Clears all front matter metadata, leaving only the Markdown content.
Example
doc = MarkdownDocument( ... content="# Hello World", ... metadata={"title": "Greeting", "tags": ["intro", "welcome"]} ... ) doc.clear_frontmatter() print(doc.metadata) {}
Source code in ures/markdown/manipulator.py
594 595 596 597 598 599 600 601 602 603 604 605 606 607 | |
from_file(file_path)
classmethod
Creates a MarkdownDocument instance by loading a Markdown file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The path to the Markdown file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
MarkdownDocument |
MarkdownDocument
|
An instance representing the loaded Markdown file. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified file does not exist. |
InvalidFrontMatterError
|
If the front matter is malformed. |
Source code in ures/markdown/manipulator.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
get_frontmatter(key_path)
Retrieves the value of a front matter key. Supports nested keys using dot notation, including list indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_path
|
str
|
The front matter key path. Use dot notation for nested keys (e.g., "author.name" or "sections.0.title"). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The value associated with the key, or None if the key does not exist. |
Example
doc = MarkdownDocument( ... metadata={ ... "author": {"name": "John Doe", "contact": {"email": "john@example.com"}}, ... "sections": [ ... {"title": "Introduction", "content": "Welcome."}, ... {"title": "Conclusion", "content": "Goodbye."} ... ] ... } ... ) doc.get_frontmatter("author.name") "John Doe" doc.get_frontmatter("sections.0.title") "Introduction" doc.get_frontmatter("sections.1.content") "Goodbye."
Source code in ures/markdown/manipulator.py
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 400 401 402 403 404 405 406 407 408 409 | |
load_from_file(file_path)
Loads Markdown content and front matter from a specified file into the current instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The path to the Markdown file to load. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified file does not exist. |
InvalidFrontMatterError
|
If the front matter is malformed. |
Example
doc = MarkdownDocument() doc.load_from_file("existing.md")
Source code in ures/markdown/manipulator.py
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | |
parse_content()
Parses the raw note content into a structured Content object.
Iterates through the raw text line by line, identifying Markdown headers (e.g., '# Title') to delimit sections. Text found before the first header is assigned to a 'default' section.
Returns:
| Name | Type | Description |
|---|---|---|
Content |
Content
|
An object containing the parsed sections, their hierarchy levels, |
Content
|
and associated text content. |
Source code in ures/markdown/manipulator.py
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | |
remove_frontmatter(key_path)
Removes a front matter key. Supports nested keys using dot notation, including list indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_path
|
str
|
The front matter key path to remove. Use dot notation for nested keys (e.g., "author.contact.email" or "sections.0.title"). |
required |
Example
doc = MarkdownDocument( ... metadata={ ... "author": {"name": "John Doe", "contact": {"email": "john@example.com"}}, ... "sections": [ ... {"title": "Introduction", "content": "Welcome."}, ... {"title": "Conclusion", "content": "Goodbye."} ... ] ... } ... ) doc.remove_frontmatter("author.contact.email") doc.get_frontmatter("author.contact.email") is None True doc.remove_frontmatter("sections.1.title") doc.get_frontmatter("sections.1.title") is None True
Source code in ures/markdown/manipulator.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | |
save(file_path)
Saves the MarkdownDocument to a specified file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The path where the Markdown file will be saved. |
required |
Example
doc = MarkdownDocument( ... content="# Hello World", ... metadata={"title": "Greeting", "tags": ["intro", "welcome"]} ... ) doc.save_to_file("greeting.md")
Source code in ures/markdown/manipulator.py
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | |
set_frontmatter(key_path, value, overwrite=True)
Sets a front matter key to a specified value. Supports nested keys using dot notation, including mixed types such as dictionaries within lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_path
|
str
|
The front matter key path. Use dot notation for nested keys (e.g., "author.name" or "sections.0.title"). |
required |
value
|
Any
|
The value to set for the key. |
required |
overwrite
|
bool
|
If True, overwrites the existing value; otherwise, appends to lists or creates new entries in lists. Defaults to True. |
True
|
Example
doc = MarkdownDocument() doc.set_frontmatter("author.name", "John Doe") doc.set_frontmatter("author.contact.email", "john@example.com") doc.set_frontmatter("sections.0.title", "Introduction") doc.set_frontmatter("sections.0.content", "Welcome to the introduction.") doc.set_frontmatter("sections.1.title", "Conclusion") doc.set_frontmatter("sections.1.content", "Wrapping up.")
Source code in ures/markdown/manipulator.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |
to_markdown()
Serializes the MarkdownDocument to a Markdown-formatted string, including front matter.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The complete Markdown content with front matter. |
ERROR
ValueError: If the front matter is missing mandatory fields.
Example
doc = MarkdownDocument( ... content="# Hello World", ... metadata={"title": "Greeting", "tags": ["intro", "welcome"]} ... ) print(doc.to_markdown())
title: Greeting tags: - intro - welcome
Hello World
Source code in ures/markdown/manipulator.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
validation_frontmatter()
Validate the frontmatter metadata against the mandatory fields.
Source code in ures/markdown/manipulator.py
609 610 611 612 613 614 615 616 617 618 619 620 | |
Zettelkasten
Bases: MarkdownDocument
Class for handling Zettelkasten markdown files. In my case, a list of mandatory fields is defined in a class variable.
In Zettelkasten note-taking system, only three types of notes are supported: 'fleeting', 'literature', 'permanent', but I added 'atom' type for my own use.
Source code in ures/markdown/zettelkasten.py
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
__init__(title, n_type, url=None, tags=None, aliases=None, **kwargs)
Initialize a Zettelkasten object Args: title (str): The title of the note. n_type (str): The type of the note, only support 'fleeting', 'literature', 'permanent' and 'atom'. url (str): The url of the note. tags (list): The tags of the note. aliases
Source code in ures/markdown/zettelkasten.py
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 | |
from_file(file_path)
classmethod
Creates a MarkdownDocument instance by loading a Markdown file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The path to the Markdown file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
MarkdownDocument |
Union[MarkdownDocument, Zettelkasten]
|
An instance representing the loaded Markdown file. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified file does not exist. |
InvalidFrontMatterError
|
If the front matter is malformed. |
Source code in ures/markdown/zettelkasten.py
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 | |
to_llm_friendly_content(prop_ignores=None, context=None)
Generates an LLM-friendly representation of the note, filtering metadata and merging context.
This method creates a structured content object designed for LLM consumption. It first adds a 'Metadata' section (filtering out specified keys), then appends the note's main body, and finally merges any external context provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_ignores
|
Optional[List[str]]
|
A list of metadata keys to exclude from the output. Defaults to None. The keys "aliases", "url", "id", and "title" are always ignored by default. |
None
|
context
|
Optional[Content]
|
Additional contextual content to append to the note. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Content |
Content
|
A new Content object organized into 'Metadata', 'Main Content', and |
Content
|
optionally 'Context' sections. |
Source code in ures/markdown/zettelkasten.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |