Skip to content

Commit 7fc1178

Browse files
author
DidierRLopes
committed
adjust to remove images and code from blog size count
1 parent 48ce875 commit 7fc1178

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/components/BlogHistory.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,24 @@ export default function BlogHistory({ posts = [] }: BlogHistoryProps) {
8989
// Create an object with individual post sizes
9090
const postSizes = postsInMonth.reduce(
9191
(acc: { [key: string]: number }, post, idx) => {
92-
acc[`post${idx + 1}`] = post.content_html.length / 1024;
92+
// Parse the HTML content
93+
const parser = new DOMParser();
94+
const doc = parser.parseFromString(post.content_html, 'text/html');
95+
96+
// Select all text nodes, excluding those within <code>, <pre>, and <img> tags
97+
const textNodes = Array.from(doc.body.childNodes).filter(node =>
98+
node.nodeType === Node.TEXT_NODE ||
99+
(node.nodeType === Node.ELEMENT_NODE &&
100+
!['CODE', 'PRE', 'IMG'].includes(node.nodeName))
101+
);
102+
103+
// Calculate the total length of text content
104+
const textContent = textNodes.reduce((text, node) =>
105+
text + (node.textContent || ''), ''
106+
);
107+
108+
// Store the size in KB
109+
acc[`post${idx + 1}`] = textContent.length / 1024;
93110
return acc;
94111
},
95112
{},

0 commit comments

Comments
 (0)