|
| 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 | +} |
0 commit comments