-
Notifications
You must be signed in to change notification settings - Fork 20
/
table-of-contents.vue
55 lines (50 loc) · 1.7 KB
/
table-of-contents.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<script setup lang="ts">
import { Block } from "../lib/types";
import { useNotionBlock, defineNotionProps } from "../lib/blockable";
import NotionTableOfContentsItem from "../blocks/helpers/table-of-contents-item.vue";
import { computed } from "vue";
interface BlockLevel extends Block {
level: number;
}
const props = defineProps({ ...defineNotionProps });
//@ts-ignore
const { block, parent, hasPageLinkOptions, pageLinkProps } = useNotionBlock(props);
const headers = ["header", "sub_header", "sub_sub_header"];
const headerObjects = computed(() => {
if (!props.blockMap) return;
let temp: BlockLevel[] = [];
Object.entries(props.blockMap).forEach(([key, value]) => {
if (headers.includes(value.value.type) && value.value.parent_id == parent.value.value.id) {
let level = 0;
if (temp.length) {
let prevBlock = temp[temp.length - 1];
if (value.value.type == "header") {
} else if (value.value.type == "sub_header") {
level = 1;
} else if (value.value.type == prevBlock.value.type) {
level = prevBlock.level;
} else if (value.value.type != prevBlock.value.type) {
level = prevBlock.level + 1;
}
}
temp.push({ ...value, level });
}
});
return temp;
});
console.log(headerObjects);
</script>
<script lang="ts">
export default {
name: "NotionTableOfContent",
};
</script>
<template>
<div class="notion-table-of-contents">
<template v-for="header in headerObjects">
<a class="notion-page-link" :target="props.pageLinkTarget" :href="`#${header.value.id}`">
<NotionTableOfContentsItem :text="header.value.properties.title" :level="header.level" />
</a>
</template>
</div>
</template>