Skip to content

Commit

Permalink
🐛 Resolve parsing errors if elements are received as the first item i…
Browse files Browse the repository at this point in the history
…n the callout

Signed-off-by: Sumin <robolindasoo@gmail.com>
  • Loading branch information
milk717 committed Mar 3, 2024
1 parent 2da09ab commit f265fd3
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/obsidian-callouts-markdown/src/core/calloutParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@ const defaultResponse = {
};

const parseCallout = (children: ReactNode) => {
const str = Array.isArray(children) ? children.at(0) : children;
if (
isValidElement(children) ||
(Array.isArray(children) && isValidElement(children?.at(0)))
)
return {
type: NORMAL_CALLOUT_TYPE,
title: undefined,
content: children,
};

const calloutTypeString = Array.isArray(children) ? children.at(0) : children;
const pattern = `\\[!([^\\n]+)\\](?:[ ]+([^\\n]+))?`;

const regex = new RegExp(pattern);
const matches = str.match(regex);
const matches = calloutTypeString.match(regex);

if (!matches)
return {
type: NORMAL_CALLOUT_TYPE,
title: undefined,
content: str,
content: calloutTypeString,
};

const [, type, title] = matches;
Expand All @@ -29,7 +39,7 @@ const parseCallout = (children: ReactNode) => {
title: Array.isArray(children) ? children.at(1) : title,
content: Array.isArray(children)
? [children.at(2).trim(), ...children.slice(3)]
: str.replace(regex, '').trim(),
: calloutTypeString.replace(regex, '').trim(),
};
};

Expand Down

0 comments on commit f265fd3

Please sign in to comment.