1
0

Add frontmatter with aliases to cards. Frontmatter is stripped before sending to API

This commit is contained in:
Jon Michael Aanes 2024-09-27 16:41:26 +02:00
parent cb9593b744
commit 53355a4549
2 changed files with 40 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import stat
from collections.abc import Iterator from collections.abc import Iterator
from logging import getLogger from logging import getLogger
import frontmatter
import marko import marko
import marko.md_renderer import marko.md_renderer
@ -18,17 +19,43 @@ class CardContents:
markdown = marko.Markdown() markdown = marko.Markdown()
renderer = marko.md_renderer.MarkdownRenderer() renderer = marko.md_renderer.MarkdownRenderer()
OBSIDIAN_MODE = True
def format_card_contents(card: CardContents) -> str: def format_card_contents(card: CardContents) -> str:
ls = [] ls = []
# Frontmatter
if OBSIDIAN_MODE:
if card.name:
ls.append('---\n')
# TODO: Tags
ls.append('aliases:\n')
ls.append(' - ')
ls.append(card.name)
ls.append('\n')
ls.append('---\n\n')
# Card name
if card.name: if card.name:
ls.append('# ') ls.append('# ')
ls.append(card.name) ls.append(card.name)
ls.append('\n\n') ls.append('\n\n')
ls.append(card.description or '')
# Card contents
if card.description:
ls.append(card.description)
return ''.join(ls) return ''.join(ls)
def parse_card_contents(contents: str) -> CardContents: def parse_card_contents(contents: str) -> CardContents:
document = markdown.parse(contents.strip()) """
1. Strips frontmatter
2. Parses header
3. Finds content.
"""
fm = frontmatter.loads(contents)
del contents
document = markdown.parse(fm.content.strip())
name = None name = None
for elem in document.children: for elem in document.children:
if isinstance(elem, marko.block.Heading): if isinstance(elem, marko.block.Heading):
@ -43,5 +70,3 @@ def parse_card_contents(contents: str) -> CardContents:
def format_card(card: Card) -> str: def format_card(card: Card) -> str:
return format_card_contents(CardContents(card.name, card.detailed_description)) return format_card_contents(CardContents(card.name, card.detailed_description))

View File

@ -1,8 +1,12 @@
from favro_sync.favro_markdown import parse_card_contents, format_card_contents from favro_sync.favro_markdown import parse_card_contents, format_card_contents
EXAMPLE_TEXT_1 = ''' EXAMPLE_TEXT_1 = '''
---
aliases:
- Hello World
---
# Hello world # Hello World
Test description Test description
@ -15,5 +19,10 @@ Test description
def test_parse_and_render(): def test_parse_and_render():
card_contents = parse_card_contents(EXAMPLE_TEXT_1) card_contents = parse_card_contents(EXAMPLE_TEXT_1)
assert card_contents.name == 'Hello World'
assert '---' not in card_contents.description
print(card_contents) print(card_contents)
assert False
assert format_card_contents(card_contents) == EXAMPLE_TEXT_1 assert format_card_contents(card_contents) == EXAMPLE_TEXT_1