-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathblockquote.js
More file actions
50 lines (43 loc) · 1.64 KB
/
blockquote.js
File metadata and controls
50 lines (43 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
export const blockquoteCompiler = ({ renderer, compiler }) =>
(renderer.blockquote = function ({ tokens }) {
let openTag = '<blockquote>';
let closeTag = '</blockquote>';
// Find the first paragraph token in the blockquote
const firstParagraphIndex = tokens.findIndex(t => t.type === 'paragraph');
const firstParagraph = tokens[firstParagraphIndex];
if (firstParagraph) {
// Check if the paragraph starts with a callout like [!TIP] or [!NOTE]
const calloutData = firstParagraph.raw.match(/^(\[!(\w+)\])/);
if (calloutData) {
const calloutMark = calloutData[1]; // "[!TIP]"
const calloutType = calloutData[2].toLowerCase(); // "tip"
// Remove the callout mark from the paragraph raw text
firstParagraph.raw = firstParagraph.raw
.replace(calloutMark, '')
.trimStart();
if (firstParagraph.tokens && firstParagraph.tokens.length > 0) {
firstParagraph.tokens.forEach(t => {
if (t.raw) {
t.raw = t.raw.replace(calloutMark, '');
}
if (t.text) {
t.text = t.text.replace(calloutMark, '');
}
});
}
// If the first paragraph is now empty after removing [!TIP], remove it
if (!firstParagraph.raw.trim()) {
tokens.splice(firstParagraphIndex, 1);
}
openTag = `<div class="callout ${calloutType}">`;
closeTag = `</div>`;
}
}
compiler.blockquoteDepth++;
try {
const body = this.parser.parse(tokens);
return `${openTag}${body}${closeTag}`;
} finally {
compiler.blockquoteDepth--;
}
});