nibbles-and-bites 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
{
|
2
2
|
"name": "nibbles-and-bites",
|
3
3
|
"description": "A composable and accessible component framework",
|
4
|
-
"version": "0.1.
|
5
|
-
"homepage": "https://
|
4
|
+
"version": "0.1.5",
|
5
|
+
"homepage": "https://nibbles-and-bites.polycule.li",
|
6
|
+
"repository": "AliciaBytes/nibbles-and-bites",
|
6
7
|
"type": "module",
|
7
8
|
"exports": {
|
8
9
|
".": "./src/components/index.js",
|
@@ -0,0 +1,30 @@
|
|
1
|
+
---
|
2
|
+
import generateTocStructure from "../../util/generateTocStructure";
|
3
|
+
import type { MarkdownHeading } from "astro";
|
4
|
+
import TocLevel from "./TocLevel.astro";
|
5
|
+
export interface Props {
|
6
|
+
headings: MarkdownHeading[];
|
7
|
+
}
|
8
|
+
|
9
|
+
let { headings } = Astro.props;
|
10
|
+
const toc = generateTocStructure(
|
11
|
+
headings.filter((heading) => heading.depth > 1 && heading.depth < 4)
|
12
|
+
)!;
|
13
|
+
|
14
|
+
const hasToc = toc && toc.length !== 0;
|
15
|
+
---
|
16
|
+
|
17
|
+
{
|
18
|
+
hasToc && (
|
19
|
+
<details>
|
20
|
+
<summary>Table of Contents</summary>
|
21
|
+
<TocLevel items={toc} />
|
22
|
+
</details>
|
23
|
+
)
|
24
|
+
}
|
25
|
+
|
26
|
+
<style>
|
27
|
+
li {
|
28
|
+
list-style-type: none;
|
29
|
+
}
|
30
|
+
</style>
|
@@ -0,0 +1,29 @@
|
|
1
|
+
---
|
2
|
+
import type { TocItem } from "@src/util/generateTocStructure.ts";
|
3
|
+
export interface Props {
|
4
|
+
items: TocItem[];
|
5
|
+
}
|
6
|
+
|
7
|
+
const { items } = Astro.props;
|
8
|
+
---
|
9
|
+
|
10
|
+
{
|
11
|
+
items && items.length > 0 && (
|
12
|
+
<ul>
|
13
|
+
{items.map((item) => (
|
14
|
+
<>
|
15
|
+
<li>
|
16
|
+
<a href={`#${item.slug}`}>{item.text}</a>
|
17
|
+
</li>
|
18
|
+
{item.children && <Astro.self items={item.children} />}
|
19
|
+
</>
|
20
|
+
))}
|
21
|
+
</ul>
|
22
|
+
)
|
23
|
+
}
|
24
|
+
|
25
|
+
<style>
|
26
|
+
li {
|
27
|
+
list-style-type: none;
|
28
|
+
}
|
29
|
+
</style>
|
package/src/components/astro.js
CHANGED
@@ -9,4 +9,6 @@ export { default as SectionDivider } from "./astro/SectionDivider.astro"
|
|
9
9
|
export { default as Spoiler } from "./astro/Spoiler.astro"
|
10
10
|
export { default as ThemeSwitcher } from "./astro/ThemeSwitcher.astro"
|
11
11
|
export { default as ThemeToggle } from "./astro/ThemeToggle.astro"
|
12
|
+
export { default as Toc } from "./astro/Toc.astro"
|
13
|
+
export { default as TocLevel } from "./astro/TocLevel.astro"
|
12
14
|
export { default as Warning } from "./astro/Warning.astro"
|
@@ -0,0 +1,62 @@
|
|
1
|
+
import type { MarkdownHeading } from "astro";
|
2
|
+
export interface TocItem extends MarkdownHeading {
|
3
|
+
children: TocItem[];
|
4
|
+
}
|
5
|
+
|
6
|
+
function findCorrectChildrenLevel(item: TocItem, depth: number): TocItem[] {
|
7
|
+
if (depth <= 0) {
|
8
|
+
throw new Error("Invalid ToC levels.");
|
9
|
+
}
|
10
|
+
|
11
|
+
if (depth === 1) {
|
12
|
+
return item.children;
|
13
|
+
}
|
14
|
+
|
15
|
+
return findCorrectChildrenLevel(
|
16
|
+
item.children[item.children.length - 1],
|
17
|
+
depth - 1,
|
18
|
+
);
|
19
|
+
}
|
20
|
+
|
21
|
+
export default function generateTocStructure(headings: MarkdownHeading[]) {
|
22
|
+
let toc: TocItem[] = [];
|
23
|
+
|
24
|
+
if (headings.length < 1) {
|
25
|
+
return toc;
|
26
|
+
}
|
27
|
+
|
28
|
+
const firstItem = headings.shift()!;
|
29
|
+
|
30
|
+
if (headings.some((heading) => heading.depth < firstItem.depth)) {
|
31
|
+
throw new Error(
|
32
|
+
"Problewm in the structure of the ToC, check the heading levels.",
|
33
|
+
);
|
34
|
+
}
|
35
|
+
|
36
|
+
toc.push({
|
37
|
+
...firstItem,
|
38
|
+
children: [],
|
39
|
+
});
|
40
|
+
|
41
|
+
for (const heading of headings) {
|
42
|
+
if (heading.depth === firstItem.depth) {
|
43
|
+
toc.push({
|
44
|
+
...heading,
|
45
|
+
children: [],
|
46
|
+
});
|
47
|
+
} else {
|
48
|
+
const lastItem = toc[toc.length - 1];
|
49
|
+
const target = findCorrectChildrenLevel(
|
50
|
+
lastItem,
|
51
|
+
heading.depth - firstItem.depth,
|
52
|
+
);
|
53
|
+
|
54
|
+
target.push({
|
55
|
+
...heading,
|
56
|
+
children: [],
|
57
|
+
});
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
return toc;
|
62
|
+
}
|