-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockquoteCitations.js
More file actions
63 lines (50 loc) · 2.38 KB
/
blockquoteCitations.js
File metadata and controls
63 lines (50 loc) · 2.38 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
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Automatically adds <cite> tags to blockquotes by parsing citation patterns.
* Replicates Hugo partial logic for extracting citations from blockquote text.
* Uses regex-based string manipulation for SSR compatibility.
*/
/**
* Post-processor function that can be chained after marked processing
* @param {string} html - HTML output from marked
* @returns {string} - HTML with processed blockquote citations
*/
export function postProcessCitations(html) {
const dashPatterns = ['~ '];
const citationLengthLimit = 2048;
// Match blockquote tags and their content
const blockquoteRegex = /<blockquote>([\s\S]*?)<\/blockquote>/g;
return html.replace(blockquoteRegex, (match, content) => {
let modifiedContent = content;
let cite = '';
// Try each dash pattern to find a citation
for (const dashPattern of dashPatterns) {
if (!cite && content.includes(dashPattern)) {
const parts = content.split(dashPattern);
if (parts.length > 1) {
let lastPart = parts[parts.length - 1];
// Remove closing </p> tag if present
lastPart = lastPart.replace(/<\/p>\s*$/, '');
// Trim whitespace
lastPart = lastPart.trim();
// Check if this looks like a valid citation
if (lastPart && lastPart.length < citationLengthLimit) {
cite = lastPart;
// Rebuild content without the citation
const contentParts = parts.slice(0, -1);
modifiedContent = contentParts.join(dashPattern);
// Ensure content ends with </p> if it doesn't already
if (!modifiedContent.trim().endsWith('</p>')) {
modifiedContent = modifiedContent + '</p>';
}
break;
}
}
}
}
// If we found a citation, rebuild the blockquote
if (cite) {
return `<blockquote>${modifiedContent}\n<cite>${cite}</cite></blockquote>`;
}
return match;
});
}