Add frontmatter with aliases to cards. Frontmatter is stripped before sending to API
This commit is contained in:
parent
cb9593b744
commit
53355a4549
|
@ -5,6 +5,7 @@ import stat
|
|||
from collections.abc import Iterator
|
||||
from logging import getLogger
|
||||
|
||||
import frontmatter
|
||||
import marko
|
||||
import marko.md_renderer
|
||||
|
||||
|
@ -18,17 +19,43 @@ class CardContents:
|
|||
markdown = marko.Markdown()
|
||||
renderer = marko.md_renderer.MarkdownRenderer()
|
||||
|
||||
OBSIDIAN_MODE = True
|
||||
|
||||
def format_card_contents(card: CardContents) -> str:
|
||||
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:
|
||||
ls.append('# ')
|
||||
ls.append(card.name)
|
||||
ls.append('\n\n')
|
||||
ls.append(card.description or '')
|
||||
ls.append('\n\n')
|
||||
|
||||
# Card contents
|
||||
if card.description:
|
||||
ls.append(card.description)
|
||||
return ''.join(ls)
|
||||
|
||||
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
|
||||
for elem in document.children:
|
||||
if isinstance(elem, marko.block.Heading):
|
||||
|
@ -43,5 +70,3 @@ def parse_card_contents(contents: str) -> CardContents:
|
|||
|
||||
def format_card(card: Card) -> str:
|
||||
return format_card_contents(CardContents(card.name, card.detailed_description))
|
||||
|
||||
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
from favro_sync.favro_markdown import parse_card_contents, format_card_contents
|
||||
|
||||
EXAMPLE_TEXT_1 = '''
|
||||
---
|
||||
aliases:
|
||||
- Hello World
|
||||
---
|
||||
|
||||
# Hello world
|
||||
# Hello World
|
||||
|
||||
Test description
|
||||
|
||||
|
@ -15,5 +19,10 @@ Test description
|
|||
|
||||
def test_parse_and_render():
|
||||
card_contents = parse_card_contents(EXAMPLE_TEXT_1)
|
||||
|
||||
assert card_contents.name == 'Hello World'
|
||||
assert '---' not in card_contents.description
|
||||
print(card_contents)
|
||||
assert False
|
||||
|
||||
assert format_card_contents(card_contents) == EXAMPLE_TEXT_1
|
||||
|
|
Loading…
Reference in New Issue
Block a user