|
4 | 4 | import os |
5 | 5 | from pathlib import Path |
6 | 6 | import pickle |
| 7 | +import re |
7 | 8 | import sqlite3 |
8 | 9 | import tempfile |
9 | 10 | import time |
@@ -506,11 +507,139 @@ def add_notes_with_editor( |
506 | 507 | return self.add_notes_from_file(tf.name) |
507 | 508 |
|
508 | 509 | def add_notes_from_file( |
509 | | - self, filename: str, tags: str = "", deck: Optional[str] = None |
| 510 | + self, |
| 511 | + filename: str, |
| 512 | + tags: str = "", |
| 513 | + deck: Optional[str] = None, |
| 514 | + update_file: bool = False, |
510 | 515 | ) -> list[Note]: |
511 | | - """Add new notes to collection from Markdown file""" |
512 | | - notes = markdown_file_to_notes(filename) |
513 | | - return self.add_notes_from_list(notes, tags, deck) |
| 516 | + """Add new notes to collection from Markdown file |
| 517 | +
|
| 518 | + Args: |
| 519 | + filename: Path to the markdown file containing notes |
| 520 | + tags: Additional tags to add to the notes |
| 521 | + deck: Default deck for notes without a deck specified |
| 522 | + update_file: If True, update the original file with note IDs |
| 523 | +
|
| 524 | + Returns: |
| 525 | + List of notes that were added |
| 526 | + """ |
| 527 | + # Reuse update_notes_from_file since it handles both adding new notes and updating existing ones |
| 528 | + # For add_notes_from_file, we're essentially just adding new notes |
| 529 | + return self.update_notes_from_file(filename, tags, deck, update_file) |
| 530 | + |
| 531 | + def update_notes_from_file( |
| 532 | + self, |
| 533 | + filename: str, |
| 534 | + tags: str = "", |
| 535 | + deck: Optional[str] = None, |
| 536 | + update_file: bool = False, |
| 537 | + ) -> list[Note]: |
| 538 | + """Update existing notes or add new notes from Markdown file |
| 539 | +
|
| 540 | + This function looks for nid: or cid: headers in the file to determine |
| 541 | + if a note should be updated rather than added. |
| 542 | +
|
| 543 | + Args: |
| 544 | + filename: Path to the markdown file containing notes |
| 545 | + tags: Additional tags to add to the notes |
| 546 | + deck: Default deck for notes without a deck specified |
| 547 | + update_file: If True, update the original file with note IDs |
| 548 | +
|
| 549 | + Returns: |
| 550 | + List of notes that were updated or added |
| 551 | + """ |
| 552 | + with open(filename, "r", encoding="utf-8") as f: |
| 553 | + original_content = f.read() |
| 554 | + |
| 555 | + notes_data = markdown_file_to_notes(filename) |
| 556 | + updated_notes = [] |
| 557 | + |
| 558 | + # Track if any notes were added that need IDs |
| 559 | + needs_update = False |
| 560 | + |
| 561 | + for note_data in notes_data: |
| 562 | + if tags: |
| 563 | + note_data.tags = f"{tags} {note_data.tags}" |
| 564 | + |
| 565 | + if deck and not note_data.deck: |
| 566 | + note_data.deck = deck |
| 567 | + |
| 568 | + # Check if this note already has an ID |
| 569 | + had_id = bool(note_data.nid) |
| 570 | + |
| 571 | + note = note_data.update_or_add_to_collection(self) |
| 572 | + updated_notes.append(note) |
| 573 | + |
| 574 | + # Mark for file update if this was a new note without an ID |
| 575 | + if not had_id and update_file: |
| 576 | + needs_update = True |
| 577 | + |
| 578 | + # Update the original file with note IDs if requested |
| 579 | + if update_file and needs_update: |
| 580 | + self._update_file_with_note_ids(filename, original_content, updated_notes) |
| 581 | + |
| 582 | + return updated_notes |
| 583 | + |
| 584 | + def _update_file_with_note_ids( |
| 585 | + self, filename: str, content: str, notes: list[Note] |
| 586 | + ) -> None: |
| 587 | + """Update the original markdown file with note IDs |
| 588 | +
|
| 589 | + This function adds nid: headers to notes in the file that don't have them. |
| 590 | +
|
| 591 | + Args: |
| 592 | + filename: Path to the markdown file |
| 593 | + content: Original content of the file |
| 594 | + notes: List of notes that were added/updated |
| 595 | + """ |
| 596 | + # Find all '# Note' or similar headers in the file |
| 597 | + note_headers = re.finditer(r"^# .*$", content, re.MULTILINE) |
| 598 | + note_positions = [match.start() for match in note_headers] |
| 599 | + |
| 600 | + if not note_positions: |
| 601 | + return # No notes found in file |
| 602 | + |
| 603 | + # Add an extra position at the end to simplify boundary handling |
| 604 | + note_positions.append(len(content)) |
| 605 | + |
| 606 | + # Extract each note's section and check if it needs to be updated |
| 607 | + updated_content = [] |
| 608 | + for i in range(len(note_positions) - 1): |
| 609 | + start = note_positions[i] |
| 610 | + end = note_positions[i + 1] |
| 611 | + |
| 612 | + # Get the section for this note |
| 613 | + section = content[start:end] |
| 614 | + |
| 615 | + # Check if this section already has an nid |
| 616 | + if re.search(r"^nid:", section, re.MULTILINE): |
| 617 | + # Already has an ID, keep as is |
| 618 | + updated_content.append(section) |
| 619 | + else: |
| 620 | + # No ID, add the note ID from our updated notes |
| 621 | + # We need to find where to insert the ID line (after model, tags, etc.) |
| 622 | + lines = section.split("\n") |
| 623 | + |
| 624 | + # Find a good position to insert the ID (after model, tags, deck) |
| 625 | + insert_pos = 1 # Default: after the first line (the title) |
| 626 | + for j, line in enumerate(lines[1:], 1): |
| 627 | + # Look for model:, tags:, deck: lines |
| 628 | + if re.match(r"^(model|tag[s]?|deck|markdown|md):", line): |
| 629 | + insert_pos = j + 1 # Insert after this line |
| 630 | + |
| 631 | + # If we have a note ID for this position, insert it |
| 632 | + if i < len(notes): |
| 633 | + note_id = notes[i].n.id |
| 634 | + lines.insert(insert_pos, f"nid: {note_id}") |
| 635 | + updated_content.append("\n".join(lines)) |
| 636 | + else: |
| 637 | + # Couldn't match this section to a note, keep unchanged |
| 638 | + updated_content.append(section) |
| 639 | + |
| 640 | + # Write back the updated content |
| 641 | + with open(filename, "w", encoding="utf-8") as f: |
| 642 | + f.write("".join(updated_content)) |
514 | 643 |
|
515 | 644 | def add_notes_from_list( |
516 | 645 | self, |
|
0 commit comments