diff --git a/src/components/blogs/HomePage.vue b/src/components/blogs/HomePage.vue index e97f281..44f8d7a 100644 --- a/src/components/blogs/HomePage.vue +++ b/src/components/blogs/HomePage.vue @@ -493,22 +493,22 @@ onMounted(() => { listMaxHeight: 90, lrcType: 3, audio: [ - { - name: 'name1', - artist: 'artist1', - url: 'url1.mp3', - cover: 'cover1.jpg', - lrc: 'lrc1.lrc', - theme: '#ebd0c2' - }, - { - name: 'name2', - artist: 'artist2', - url: 'url2.mp3', - cover: 'cover2.jpg', - lrc: 'lrc2.lrc', - theme: '#46718b' - } + // { + // name: 'name1', + // artist: 'artist1', + // url: 'url1.mp3', + // cover: 'cover1.jpg', + // lrc: 'lrc1.lrc', + // theme: '#ebd0c2' + // }, + // { + // name: 'name2', + // artist: 'artist2', + // url: 'url2.mp3', + // cover: 'cover2.jpg', + // lrc: 'lrc2.lrc', + // theme: '#46718b' + // } ] }); } diff --git a/src/views/blog/blogcontent/BlogDetailView.vue b/src/views/blog/blogcontent/BlogDetailView.vue index 52dedee..d471d07 100644 --- a/src/views/blog/blogcontent/BlogDetailView.vue +++ b/src/views/blog/blogcontent/BlogDetailView.vue @@ -1,6 +1,6 @@ + } catch (error) { + console.error("Error fetching blog content:", error); + } +}; + +// Process headings after blog content is fetched +const processHeadings = async () => { + await nextTick(); + const anchors = document.querySelectorAll('#blogDetail h1, #blogDetail h2, #blogDetail h3, #blogDetail h4, #blogDetail h5, #blogDetail h6'); + const titlesArray = Array.from(anchors).filter((title:any) => !!title.innerText.trim()); + + if (!titlesArray.length) { + titles.value = []; + return; + } + + const hTags = Array.from(new Set(titlesArray.map((title: any) => title.tagName))).sort(); + + titles.value = titlesArray.map((el: any) => ({ + title: el.innerText, + lineIndex: el.getAttribute('data-v-md-line') || '', + indent: hTags.indexOf(el.tagName), + })); +}; + +// Handle anchor click +const handleAnchorClick = (anchor: { title: string; lineIndex: string }) => { + const heading = document.querySelector(`#blogDetail [data-v-md-line="${anchor.lineIndex}"]`); + + if (heading) { + heading.scrollIntoView({ behavior: 'smooth', block: 'start' }); + } else { + // Use MutationObserver to wait for the target element to be available + const observer = new MutationObserver((mutations, obs) => { + const heading = document.querySelector(`#blogDetail [data-v-md-line="${anchor.lineIndex}"]`); + if (heading) { + heading.scrollIntoView({ behavior: 'smooth', block: 'start' }); + obs.disconnect(); // Stop observing once the target element is found + } + }); + const blogDetail = document.querySelector('#blogDetail') as HTMLElement; + observer.observe(blogDetail, { childList: true, subtree: true }); + } +}; + +// Lifecycle hook to fetch blog content and process headings +onMounted(async () => { + await blogOneList(); + await processHeadings(); +}); + +