allPagesList.ts
code:allPagesList.ts
interface ScrapboxPagesJson {
count: number;
pages: { title: string }[];
}
const getPage = (projectName: string, offset: number) => {
return fetch(
projectName +
"/?limit=1000&skip=" +
encodeURIComponent(offset),
).then((pageRes) => {
if (pageRes.ok) {
return pageRes.json();
} else {
return { pages: [] };
}
}).then((pageJson: Readonly<ScrapboxPagesJson>) => {
return pageJson.pages;
});
};
const allpagelist = (pjname: string) => {
const spinner = Spinner.getInstance();
spinner.start("Fetching All Page List...");
const projectName: string = pjname;
const marked: string[] = [];
(pageRes) => {
if (!pageRes.ok) {
throw Error;
} else {
return pageRes.json();
}
},
).then((pageJson: Readonly<ScrapboxPagesJson>) => {
const arrayRange = (start: number, stop: number, step: number) =>
Array.from(
{ length: (stop - start) / step + 1 },
(_, index) => start + index * step,
);
const fetchs = arrayRange(0, pageJson.count, 1000);
function fetchtick() {
const element = fetchs.shift();
if (typeof element == "undefined") {
return Promise.resolve();
}
spinner.setText(
"Fetching All Page List...[" +
element.toString().padStart(pageJson.count.toString().length) +
"/" +
pageJson.count +
"]",
);
getPage(projectName, element).then((element3) => {
for (const element2 of element3) {
marked.push(element2.title);
}
});
return fetchtick();
}
return fetchtick();
}).then(() => {
spinner.succeed("Fetched All Page List");
spinner.stop();
return marked;
});
};
const pageCount = (pjname: string) => {
const spinner = Spinner.getInstance();
spinner.start("Fetching Page Count...");
if (!pageRes.ok) {
throw Error;
}
return pageRes.json();
}).then((pageJson: ScrapboxPagesJson) => {
spinner.succeed("Fetched Page Count");
spinner.stop();
return pageJson.count;
});
};
export { allpagelist, pageCount };