@marvalt/wparser 0.1.66 → 0.1.67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +51 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.esm.js +51 -16
- package/dist/index.esm.js.map +1 -1
- package/dist/utils/contentExtractor.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1519,29 +1519,64 @@ function renderTextWithShortcodes(text, registry) {
|
|
|
1519
1519
|
* Content extraction utilities for WordPress blocks
|
|
1520
1520
|
* Extracts text content from various block formats
|
|
1521
1521
|
*/
|
|
1522
|
+
/**
|
|
1523
|
+
* Decode HTML entities in a string
|
|
1524
|
+
* Handles both named entities (&, ") and numeric entities (', ’)
|
|
1525
|
+
*/
|
|
1526
|
+
function decodeHtmlEntities(text) {
|
|
1527
|
+
if (!text)
|
|
1528
|
+
return '';
|
|
1529
|
+
// Use browser's built-in decoder if available (most efficient)
|
|
1530
|
+
if (typeof document !== 'undefined') {
|
|
1531
|
+
const textarea = document.createElement('textarea');
|
|
1532
|
+
textarea.innerHTML = text;
|
|
1533
|
+
return textarea.value;
|
|
1534
|
+
}
|
|
1535
|
+
// Fallback for server-side or when document is not available
|
|
1536
|
+
// Decode numeric entities (', ’, etc.)
|
|
1537
|
+
let decoded = text.replace(/&#(\d+);/g, (match, dec) => {
|
|
1538
|
+
return String.fromCharCode(parseInt(dec, 10));
|
|
1539
|
+
});
|
|
1540
|
+
// Decode hex entities (', etc.)
|
|
1541
|
+
decoded = decoded.replace(/&#x([0-9a-fA-F]+);/g, (match, hex) => {
|
|
1542
|
+
return String.fromCharCode(parseInt(hex, 16));
|
|
1543
|
+
});
|
|
1544
|
+
// Decode common named entities
|
|
1545
|
+
const namedEntities = {
|
|
1546
|
+
'&': '&',
|
|
1547
|
+
'<': '<',
|
|
1548
|
+
'>': '>',
|
|
1549
|
+
'"': '"',
|
|
1550
|
+
''': "'",
|
|
1551
|
+
' ': ' ',
|
|
1552
|
+
'©': '©',
|
|
1553
|
+
'®': '®',
|
|
1554
|
+
'™': '™',
|
|
1555
|
+
'…': '…',
|
|
1556
|
+
'—': '—',
|
|
1557
|
+
'–': '–',
|
|
1558
|
+
'‘': '\u2018', // Left single quotation mark
|
|
1559
|
+
'’': '\u2019', // Right single quotation mark
|
|
1560
|
+
'“': '\u201C', // Left double quotation mark
|
|
1561
|
+
'”': '\u201D', // Right double quotation mark
|
|
1562
|
+
};
|
|
1563
|
+
Object.entries(namedEntities).forEach(([entity, char]) => {
|
|
1564
|
+
decoded = decoded.replace(new RegExp(entity, 'g'), char);
|
|
1565
|
+
});
|
|
1566
|
+
return decoded;
|
|
1567
|
+
}
|
|
1522
1568
|
/**
|
|
1523
1569
|
* Extract text content from a block's innerHTML by stripping HTML tags
|
|
1524
1570
|
*/
|
|
1525
1571
|
function extractTextFromHTML(html) {
|
|
1526
1572
|
if (!html)
|
|
1527
1573
|
return '';
|
|
1528
|
-
// Remove HTML tags
|
|
1529
|
-
let text = html
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
.replace(/’/g, "'") // Replace apostrophe entity
|
|
1533
|
-
.replace(/“/g, '"') // Replace left double quote
|
|
1534
|
-
.replace(/”/g, '"') // Replace right double quote
|
|
1535
|
-
.replace(/…/g, '...') // Replace ellipsis
|
|
1536
|
-
.replace(/&/g, '&') // Replace &
|
|
1537
|
-
.replace(/</g, '<') // Replace <
|
|
1538
|
-
.replace(/>/g, '>') // Replace >
|
|
1539
|
-
.replace(/"/g, '"') // Replace "
|
|
1540
|
-
.replace(/–/g, '–') // Replace en dash
|
|
1541
|
-
.replace(/—/g, '—') // Replace em dash
|
|
1542
|
-
.trim();
|
|
1574
|
+
// Remove HTML tags first
|
|
1575
|
+
let text = html.replace(/<[^>]*>/g, '');
|
|
1576
|
+
// Decode all HTML entities (comprehensive)
|
|
1577
|
+
text = decodeHtmlEntities(text);
|
|
1543
1578
|
// Clean up extra whitespace
|
|
1544
|
-
text = text.replace(/\s+/g, ' ');
|
|
1579
|
+
text = text.replace(/\s+/g, ' ').trim();
|
|
1545
1580
|
return text;
|
|
1546
1581
|
}
|
|
1547
1582
|
/**
|