forked from akai/readest
27 lines
607 B
TypeScript
27 lines
607 B
TypeScript
import { TOCItem } from '@/libs/document';
|
|
|
|
export const findParentPath = (toc: TOCItem[], href: string): TOCItem[] => {
|
|
for (const item of toc) {
|
|
if (item.href === href) {
|
|
return [item];
|
|
}
|
|
if (item.subitems) {
|
|
const path = findParentPath(item.subitems, href);
|
|
if (path.length) {
|
|
return [item, ...path];
|
|
}
|
|
}
|
|
}
|
|
return [];
|
|
};
|
|
|
|
export const updateTocID = (items: TOCItem[], index = 0): number => {
|
|
items.forEach((item) => {
|
|
item.id ??= index++;
|
|
if (item.subitems) {
|
|
index = updateTocID(item.subitems, index);
|
|
}
|
|
});
|
|
return index;
|
|
};
|