1
0

Fixed favro_markdown

This commit is contained in:
Jon Michael Aanes 2024-09-28 13:21:11 +02:00
parent 7d435e04d8
commit 139fb9b63d
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
3 changed files with 36 additions and 6 deletions

View File

@ -159,6 +159,8 @@ class FavroClient:
card,
detailed_description=card_contents.description,
name=card_contents.name,
tags=[], # TODO?
assignments=[],
)
self.cache.add_card(card)
return card

View File

@ -1,5 +1,6 @@
import dataclasses
import re
import frontmatter
import marko
import marko.md_renderer
@ -30,13 +31,11 @@ class CardFileFormatter:
frontmatter_data['aliases'] = [card.name]
if card.tags:
frontmatter_data['tags'] = card.tags
if self.obsidian_mode:
frontmatter_data['tags'] = ['#' + t for t in frontmatter_data['tags']]
if card.assignments:
frontmatter_data['assignments'] = card.assignments
if self.obsidian_mode:
frontmatter_data['assignments'] = [
f'[[{name}]]' for name in frontmatter_data['assignments']
f'"[[{name}]]"' for name in frontmatter_data['assignments']
]
# Frontmatter
@ -79,10 +78,15 @@ class CardFileFormatter:
document.children.remove(elem)
break
assignments: list[str] = fm.metadata.get('assignments', [])
for idx in range(0, len(assignments)):
if m := re.match(r'^\[\[(.*)\]\]$', assignments[idx]):
assignments[idx] = m.group(1)
description = self.renderer.render_children(document).strip()
return CardContents(
name,
description,
tags=[],
assignments=[],
tags=fm.metadata.get('tags', []),
assignments=assignments,
)

View File

@ -17,8 +17,24 @@ Test description
3. Derp
""".strip()
FORMATTER = CardFileFormatter()
EXAMPLE_TEXT_2 = """
---
aliases:
- Name of Card
tags:
- tag1
- tag2
assignments:
- "[[Gunnar Gunnarson]]"
- "[[Alice Alicedottor]]"
---
# Name of Card
Great news everyone! My weird program actually works!
""".strip()
FORMATTER = CardFileFormatter()
def test_parse_and_render():
card_contents = FORMATTER.parse_card_contents(EXAMPLE_TEXT_1)
@ -26,3 +42,11 @@ def test_parse_and_render():
assert card_contents.name == 'Hello World'
assert '---' not in card_contents.description
assert FORMATTER.format_card_contents(card_contents) == EXAMPLE_TEXT_1
def test_parse_and_render_2():
card_contents = FORMATTER.parse_card_contents(EXAMPLE_TEXT_2)
print(card_contents)
assert card_contents.name == 'Respond to Carstens comments, and perform any relevant fixing'
assert '---' not in card_contents.description
assert FORMATTER.format_card_contents(card_contents) == EXAMPLE_TEXT_2