Skip to content

Commit 51e390e

Browse files
authored
[DO-567] Change logic of setting ToC (#37)
* Change logic of setting ToC * Wrap ToC update logic into composable
1 parent 96fc9f2 commit 51e390e

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

composables/states.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Toc, ParsedContent } from '@nuxt/content/dist/runtime/types'
17+
import { ParsedContent } from '@nuxt/content/dist/runtime/types'
1818
import { Ref } from 'vue'
1919

20-
export const useToc = () => useState<Toc | null>('toc', () => null)
2120
export const useShowContentTree = () =>
2221
useState<boolean>('showContentTree', () => false)
2322
export const useTermPopup = () => {

composables/table-of-contents.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2023 Exactpro (Exactpro Systems Limited)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { Toc, MarkdownParsedContent } from '@nuxt/content/dist/runtime/types'
18+
19+
/**
20+
*
21+
* Provides table of contents reference
22+
*
23+
* @returns Table of contents reference
24+
*/
25+
export function useToc() {
26+
return useState<Toc | null>('toc', () => null)
27+
}
28+
29+
/**
30+
*
31+
* Automatically updates table of contents when document is changed
32+
*
33+
* @param doc Document to extract table of contents from
34+
* @returns Function to stop watching document
35+
*/
36+
export function useTocUpdate(doc: Ref<MarkdownParsedContent | null>) {
37+
const toc = useToc()
38+
39+
function updateToc() {
40+
if (doc.value) {
41+
toc.value = doc.value?.body?.toc ?? null
42+
}
43+
}
44+
45+
const stopWatchDoc = watch(doc, () => {
46+
updateToc()
47+
})
48+
49+
onMounted(() => {
50+
updateToc()
51+
})
52+
53+
onBeforeUnmount(() => {
54+
stopWatchDoc()
55+
})
56+
57+
return stopWatchDoc
58+
}

pages/[...slug].vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default defineComponent({
6666
layout: 'docs'
6767
})
6868
const route = useRoute()
69-
const toc = useToc()
69+
7070
const { data: doc } = useAsyncData('page-data' + route.path, async () => {
7171
const docPromise = queryContent<DocParsedContent>(route.path).findOne()
7272
const surroundPromise = queryContent()
@@ -83,7 +83,8 @@ export default defineComponent({
8383
after: surround[1] as ParsedContent
8484
}
8585
})
86-
toc.value = doc.value?.body?.toc ?? null
86+
87+
useTocUpdate(doc)
8788
8889
return {
8990
doc

0 commit comments

Comments
 (0)