Skip to content

Commit 8245bda

Browse files
committed
feat: enhance blockquoteCompiler to support callouts with empty lines
1 parent 5ccf910 commit 8245bda

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/core/render/compiler/blockquote.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
export const blockquoteCompiler = ({ renderer }) =>
22
(renderer.blockquote = function ({ tokens }) {
3-
const calloutData =
4-
tokens[0].type === 'paragraph' &&
5-
// 0: Match "[!TIP] My Title"
6-
// 1: Mark "[!TIP]"
7-
// 2: Type "TIP"
8-
tokens[0].raw.match(/^(\[!(\w+)\])/);
9-
103
let openTag = '<blockquote>';
114
let closeTag = '</blockquote>';
125

13-
if (calloutData) {
14-
const calloutMark = calloutData[1]; // "[!TIP]"
15-
const calloutType = calloutData[2].toLowerCase(); // "tip"
16-
const token = tokens[0].tokens[0];
6+
// Find the first paragraph token in the blockquote
7+
const firstParagraphIndex = tokens.findIndex(t => t.type === 'paragraph');
8+
const firstParagraph = tokens[firstParagraphIndex];
9+
10+
if (firstParagraph) {
11+
// Check if the paragraph starts with a callout like [!TIP] or [!NOTE]
12+
const calloutData = firstParagraph.raw.match(/^(\[!(\w+)\])/);
13+
14+
if (calloutData) {
15+
const calloutMark = calloutData[1]; // "[!TIP]"
16+
const calloutType = calloutData[2].toLowerCase(); // "tip"
17+
18+
// Remove the callout mark from the paragraph raw text
19+
firstParagraph.raw = firstParagraph.raw.replace(calloutMark, '').trimStart();
20+
if (firstParagraph.tokens && firstParagraph.tokens.length > 0) {
21+
firstParagraph.tokens.forEach(t => {
22+
if (t.raw) {t.raw = t.raw.replace(calloutMark, '').trimStart();}
23+
if (t.text) {t.text = t.text.replace(calloutMark, '').trimStart();}
24+
});
25+
}
1726

18-
// Remove callout mark from tokens
19-
['raw', 'text'].forEach(key => {
20-
token[key] = token[key].replace(calloutMark, '').trimStart();
21-
});
27+
// If the first paragraph is now empty after removing [!TIP], remove it
28+
if (!firstParagraph.raw.trim()) {
29+
tokens.splice(firstParagraphIndex, 1);
30+
}
2231

23-
openTag = `<div class="callout ${calloutType}">`;
24-
closeTag = `</div>`;
32+
openTag = `<div class="callout ${calloutType}">`;
33+
closeTag = `</div>`;
34+
}
2535
}
2636

2737
const body = this.parser.parse(tokens);

0 commit comments

Comments
 (0)