diff --git a/package.json b/package.json index 020667c..3bf2607 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,9 @@ "@pinia/nuxt": "^0.7.0", "github-markdown-css": "^5.8.1", "markdown-it": "^14.1.0", + "markdown-it-anchor": "^9.2.0", "markdown-it-mathjax3": "^4.3.2", + "markdown-it-toc-done-right": "^4.2.0", "mitt": "^3.0.1", "nuxt": "^3.14.159", "vue": "latest", diff --git a/pages/article/[cid].vue b/pages/article/[cid].vue index 4b96ca2..28e0a38 100644 --- a/pages/article/[cid].vue +++ b/pages/article/[cid].vue @@ -2,6 +2,7 @@ const route = useRoute(), cid = route.params.cid as string, content = ref>(), + toc = ref(null), getPost = get, unknown>('/blog/index.php/api/post', { params: { @@ -11,44 +12,81 @@ const route = useRoute(), }), title = computed(() => content.value?.data.title || 'Can not see me... can not see me...'), - text = computed(() => content.value?.data.text || '## Ops!'), + text = computed(() => content.value ? content.value.data.text + '[[toc]]' : '## Ops!'), date = computed(() => new Date((content.value?.data.date.timeStamp || 0) * 1000) .toISOString() .split('T')[0], ), category = computed(() => content.value?.data.category || 'default'), - url = computed(() => content.value?.data.url || '/error/404') + url = computed(() => content.value?.data.url || '/error/404'), + bindAnchorClickEvent = () => { + const autoGenTOC = document.querySelector('#toc') + if (!autoGenTOC || !toc.value) return + + toc.value.innerHTML = autoGenTOC.innerHTML + autoGenTOC.remove() + + toc.value.querySelectorAll('a').forEach((elem: HTMLElement) => { + elem.onclick = (event: MouseEvent) => { + event.preventDefault() + + let id = elem.getAttribute('href') || '' + if (id.startsWith('#')) + id = id.replace('#', '') + + const element = document.getElementById(id) + if (element) { + const offsetTop = element.offsetTop - 70 + window.scrollTo({ top: offsetTop, behavior: 'smooth' }) + } + } + }) + } content.value = await getPost() + +onMounted(() => { + bindAnchorClickEvent() +}) - diff --git a/pages/article/index.vue b/pages/article/index.vue index 33f8792..6ffbcea 100644 --- a/pages/article/index.vue +++ b/pages/article/index.vue @@ -2,10 +2,12 @@ definePageMeta({ maintenance: true, }) + +const s = '# markdown-it rulezz!\n\n[[toc]]\n## with markdown-it-toc-done-right rulezz even more!'