1
0

[Obsidian mode]: Improve formatting of task lists

This commit is contained in:
Jon Michael Aanes 2024-10-01 11:28:44 +02:00
parent 435b1fd514
commit a962170436
2 changed files with 19 additions and 3 deletions

View File

@ -31,11 +31,19 @@ class CardFileFormatter:
"""Component for formatting and parsing card files."""
def __init__(self, obsidian_mode=True):
"""Initialize card formatter.
Arguments:
- `obsidian_mode`: Configure formatter to enable maximum compatibility
with [Obsidian](https://obsidian.md/). Enables internal links, and
exposes certain aliases.
"""
self.obsidian_mode = obsidian_mode
self.markdown = marko.Markdown()
self.renderer = marko.md_renderer.MarkdownRenderer()
def format_card_contents(self, card: CardContents) -> str:
"""Formats card contents. Mostly the inverse of [`parse_card_contents`]."""
# Choose frontmatter data
frontmatter_data = {}
if self.obsidian_mode:
@ -74,13 +82,18 @@ class CardFileFormatter:
ls.append('\n\n')
# Card contents
if card.description:
ls.append(card.description)
if description := card.description:
if self.obsidian_mode:
description = re.sub(r'\-\s*\[\s*\]', '- [ ]', description,
flags=re.MULTILINE)
ls.append(description)
del description
fm = frontmatter.Post(''.join(ls), **frontmatter_data)
return frontmatter.dumps(fm)
def parse_card_contents(self, contents: str) -> CardContents:
"""Parses card contents.
"""Parses card contents. Mostly the inverse of [`parse_card_contents`].
1. Strips frontmatter and parses certain fields from the header.
2. Parses header

View File

@ -15,6 +15,9 @@ Test description
1. Derp
2. Derp
3. Derp
- [ ] Task 1
- [ ] Task 2
""".strip()
EXAMPLE_TEXT_2 = """