revised callout content parsing, which made nested callouts automagically work

This commit is contained in:
Amy G. Bowersox 2024-08-11 02:17:43 -06:00
parent dd01597ac5
commit aa431fe361
2 changed files with 36 additions and 10 deletions

View File

@ -867,11 +867,13 @@ class ObsidianStyleBlockquotes(Extension):
CALLOUT = re.compile(r'^\[!([a-z]+)\]([-+])?(?:[ ]+(.*))?')
CALLOUT_DEFICON = 'pencil'
CALLOUT_ICONS = {
'example': 'list',
'faq': 'circle-help',
'help': 'circle-help',
'info': 'info',
'question': 'circle-help',
'tip': 'flame'
'tip': 'flame',
'todo': 'circle-check'
}
def normal_blockquote(self, parent: etree.Element, block: str) -> None:
@ -920,15 +922,9 @@ class ObsidianStyleBlockquotes(Extension):
if folding:
styleval = 'display: none;' if folding == '-' else ''
content_div.attrib['style'] = styleval
first = True
for line in lines:
if first:
first = False
else:
etree.SubElement(content_div, 'br')
self.parser.state.set('list')
self.parser.parseBlocks(content_div, [line])
self.parser.state.reset()
self.parser.state.set('callout')
self.parser.parseBlocks(content_div, ['\n'.join(lines)])
self.parser.state.reset()
def run(self, parent: etree.Element, blocks: list[str]) -> None:
block = blocks.pop(0)
@ -948,8 +944,32 @@ class ObsidianStyleBlockquotes(Extension):
else:
self.normal_blockquote(parent, block)
class CalloutLinesProcessor(BlockProcessor):
def test(self, parent: etree.Element, block: str) -> bool:
return self.parser.state.isstate('callout')
def run(self, parent: etree.Element, blocks: list[str]) -> bool:
block = blocks.pop(0)
if block.strip():
sibling = self.lastChild(parent)
lines = block.split('\n')
for line in lines:
if sibling is not None:
if sibling.tail:
sibling = etree.SubElement(parent, 'br')
else:
if parent.text:
sibling = etree.SubElement(parent, 'br')
else:
parent.text = line
continue
sibling.tail = line
return True
def extendMarkdown(self, md) -> None:
md.parser.blockprocessors.register(ObsidianStyleBlockquotes.ObsidianBlockQuote(md.parser), 'quote', 20)
md.parser.blockprocessors.register(ObsidianStyleBlockquotes.CalloutLinesProcessor(md.parser),
'callout-text', 11)
def create_markdown_parser(context: Context) -> markdown.Markdown:

View File

@ -63,6 +63,9 @@ th {
background-color: rgba(var(--callout-color), 0.1);
padding: 12px 12px 12px 24px;
}
.callout[data-callout="example"] {
--callout-color: 120, 82, 238;
}
.callout[data-callout="question"], .callout[data-callout="help"], .callout[data-callout="faq"] {
--callout-color: 236, 117, 0;
}
@ -72,6 +75,9 @@ th {
.callout[data-callout="tip"] {
--callout-color: 0, 191, 188;
}
.callout[data-callout="todo"] {
--callout-color: 8, 109, 221;
}
.callout-title {
padding: 0;
display: flex;