test
This commit is contained in:
+42
-24
@@ -49,7 +49,7 @@ async function fetchPost(filename) {
|
|||||||
|
|
||||||
// generate preview: up to 3 sentences, but not exceeding ~300 chars
|
// generate preview: up to 3 sentences, but not exceeding ~300 chars
|
||||||
function getPreview(text, maxLen = 300) {
|
function getPreview(text, maxLen = 300) {
|
||||||
if (text.length <= maxLen) return text; // not needed, but safe
|
if (text.length <= maxLen) return text;
|
||||||
|
|
||||||
// try to split on sentence boundaries (. ! ?) followed by space or newline
|
// try to split on sentence boundaries (. ! ?) followed by space or newline
|
||||||
const sentences = text.match(/[^.!?]+[.!?]+/g) || [];
|
const sentences = text.match(/[^.!?]+[.!?]+/g) || [];
|
||||||
@@ -116,40 +116,58 @@ function renderPost(filename, content) {
|
|||||||
bodyDiv.textContent = body;
|
bodyDiv.textContent = body;
|
||||||
postDiv.appendChild(bodyDiv);
|
postDiv.appendChild(bodyDiv);
|
||||||
} else {
|
} else {
|
||||||
// long post – use <details> with preview
|
// long post – show preview with toggle
|
||||||
const details = document.createElement('details');
|
const container = document.createElement('div');
|
||||||
details.className = 'blog-collapse';
|
container.className = 'blog-collapsible';
|
||||||
|
|
||||||
const summary = document.createElement('summary');
|
// preview
|
||||||
// preview container
|
|
||||||
const previewDiv = document.createElement('div');
|
const previewDiv = document.createElement('div');
|
||||||
previewDiv.className = 'blog-preview';
|
previewDiv.className = 'blog-preview';
|
||||||
previewDiv.style.whiteSpace = 'pre-line';
|
previewDiv.style.whiteSpace = 'pre-line';
|
||||||
const previewText = getPreview(body);
|
let previewText = getPreview(body);
|
||||||
previewDiv.textContent = previewText + (previewText.endsWith('…') ? '' : '…'); // add ellipsis if not already
|
// ensure ellipsis if preview is truncated
|
||||||
|
if (previewText.length < body.length && !previewText.endsWith('…')) {
|
||||||
|
previewText += '…';
|
||||||
|
}
|
||||||
|
previewDiv.textContent = previewText;
|
||||||
|
container.appendChild(previewDiv);
|
||||||
|
|
||||||
const readMoreSpan = document.createElement('span');
|
// Read more button
|
||||||
readMoreSpan.className = 'read-more-text';
|
const readMoreBtn = document.createElement('button');
|
||||||
readMoreSpan.textContent = 'Read more';
|
readMoreBtn.className = 'blog-toggle read-more';
|
||||||
|
readMoreBtn.textContent = 'Read more';
|
||||||
|
container.appendChild(readMoreBtn);
|
||||||
|
|
||||||
summary.appendChild(previewDiv);
|
// Full content (hidden initially)
|
||||||
summary.appendChild(readMoreSpan);
|
|
||||||
details.appendChild(summary);
|
|
||||||
|
|
||||||
// full body (hidden until opened)
|
|
||||||
const fullDiv = document.createElement('div');
|
const fullDiv = document.createElement('div');
|
||||||
fullDiv.className = 'blog-body';
|
fullDiv.className = 'blog-full';
|
||||||
fullDiv.style.whiteSpace = 'pre-line';
|
fullDiv.style.whiteSpace = 'pre-line';
|
||||||
fullDiv.textContent = body;
|
fullDiv.textContent = body;
|
||||||
details.appendChild(fullDiv);
|
fullDiv.style.display = 'none';
|
||||||
|
container.appendChild(fullDiv);
|
||||||
|
|
||||||
// toggle text when details opens/closes
|
// Show less button (hidden initially) – appears at the bottom
|
||||||
details.addEventListener('toggle', function() {
|
const showLessBtn = document.createElement('button');
|
||||||
const span = this.querySelector('.read-more-text');
|
showLessBtn.className = 'blog-toggle show-less';
|
||||||
span.textContent = this.open ? 'Show less' : 'Read more';
|
showLessBtn.textContent = '✕ Show less';
|
||||||
});
|
showLessBtn.style.display = 'none';
|
||||||
|
container.appendChild(showLessBtn);
|
||||||
|
|
||||||
postDiv.appendChild(details);
|
// Toggle logic
|
||||||
|
function expand() {
|
||||||
|
fullDiv.style.display = 'block';
|
||||||
|
readMoreBtn.style.display = 'none';
|
||||||
|
showLessBtn.style.display = 'inline-block';
|
||||||
|
}
|
||||||
|
function collapse() {
|
||||||
|
fullDiv.style.display = 'none';
|
||||||
|
readMoreBtn.style.display = 'inline-block';
|
||||||
|
showLessBtn.style.display = 'none';
|
||||||
|
}
|
||||||
|
readMoreBtn.addEventListener('click', expand);
|
||||||
|
showLessBtn.addEventListener('click', collapse);
|
||||||
|
|
||||||
|
postDiv.appendChild(container);
|
||||||
}
|
}
|
||||||
|
|
||||||
return postDiv;
|
return postDiv;
|
||||||
|
|||||||
Reference in New Issue
Block a user