tags working now. rust parser works pretty good. next is cache
This commit is contained in:
@@ -21,6 +21,16 @@ const parserStats = {
|
||||
lastRustError: '',
|
||||
};
|
||||
|
||||
// Add a slugify function that matches Rust's slug::slugify
|
||||
function slugify(text: string): string {
|
||||
return text
|
||||
.toLowerCase()
|
||||
.normalize('NFKD')
|
||||
.replace(/[\u0300-\u036F]/g, '') // Remove diacritics
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '');
|
||||
}
|
||||
|
||||
export default function PostPage({ params }: { params: { slug: string[] } }) {
|
||||
const [post, setPost] = useState<Post | null>(null);
|
||||
// Modal state for zoomed image
|
||||
@@ -315,35 +325,49 @@ export default function PostPage({ params }: { params: { slug: string[] } }) {
|
||||
}
|
||||
}
|
||||
|
||||
// Find the element, but only consider visible ones
|
||||
const allElements = document.querySelectorAll(`#${id}`);
|
||||
// Try to find the element by the raw ID first
|
||||
let allElements = document.querySelectorAll(`#${id}`);
|
||||
let element: HTMLElement | null = null;
|
||||
|
||||
// Check if we're on desktop or mobile
|
||||
const isDesktop = window.innerWidth >= 640;
|
||||
|
||||
for (const el of Array.from(allElements)) {
|
||||
const htmlEl = el as HTMLElement;
|
||||
// Check if the element is visible (not hidden by CSS)
|
||||
const rect = htmlEl.getBoundingClientRect();
|
||||
const isVisible = rect.width > 0 && rect.height > 0;
|
||||
|
||||
if (isVisible) {
|
||||
element = htmlEl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (element) {
|
||||
console.log('Found target element:', element.textContent?.substring(0, 50));
|
||||
console.log('Found target element (raw id):', element.textContent?.substring(0, 50));
|
||||
scrollToElement(element);
|
||||
} else if (retryCount < 5) {
|
||||
return;
|
||||
}
|
||||
// If not found, try slugified version
|
||||
const slugId = slugify(id);
|
||||
if (slugId !== id) {
|
||||
allElements = document.querySelectorAll(`#${slugId}`);
|
||||
for (const el of Array.from(allElements)) {
|
||||
const htmlEl = el as HTMLElement;
|
||||
const rect = htmlEl.getBoundingClientRect();
|
||||
const isVisible = rect.width > 0 && rect.height > 0;
|
||||
if (isVisible) {
|
||||
element = htmlEl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (element) {
|
||||
console.log('Found target element (slugified id):', element.textContent?.substring(0, 50));
|
||||
scrollToElement(element);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (retryCount < 5) {
|
||||
console.log(`Element not found for anchor: ${id}, retrying... (${retryCount + 1}/5)`);
|
||||
setTimeout(() => {
|
||||
findAndScrollToElement(id, retryCount + 1);
|
||||
}, 100);
|
||||
} else {
|
||||
console.warn(`Element with id "${id}" not found after retries`);
|
||||
console.warn(`Element with id "${id}" (or slugified "${slugId}") not found after retries`);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user