2024-01-08 16:08:26 +00:00
|
|
|
import { editor, markdown, YAML } from "$sb/syscalls.ts";
|
2023-12-27 17:05:47 +00:00
|
|
|
import { CodeWidgetContent } from "$sb/types.ts";
|
2024-01-08 16:08:26 +00:00
|
|
|
import { renderToText, traverseTree } from "$sb/lib/tree.ts";
|
2023-11-25 12:40:56 +00:00
|
|
|
|
2024-01-08 16:08:26 +00:00
|
|
|
const defaultHeaderThreshold = 0;
|
2023-11-25 12:40:56 +00:00
|
|
|
|
|
|
|
type Header = {
|
|
|
|
name: string;
|
|
|
|
pos: number;
|
|
|
|
level: number;
|
|
|
|
};
|
|
|
|
|
2024-01-08 16:08:26 +00:00
|
|
|
type TocConfig = {
|
|
|
|
minHeaders?: number;
|
|
|
|
header?: boolean;
|
|
|
|
};
|
2023-11-27 15:29:19 +00:00
|
|
|
|
2024-01-08 16:08:26 +00:00
|
|
|
export async function widget(
|
|
|
|
bodyText: string,
|
|
|
|
): Promise<CodeWidgetContent | null> {
|
|
|
|
let config: TocConfig = {};
|
|
|
|
if (bodyText.trim() !== "") {
|
|
|
|
config = await YAML.parse(bodyText);
|
2023-11-25 12:40:56 +00:00
|
|
|
}
|
2024-01-08 16:08:26 +00:00
|
|
|
|
2023-11-25 12:40:56 +00:00
|
|
|
const page = await editor.getCurrentPage();
|
|
|
|
const text = await editor.getText();
|
|
|
|
const tree = await markdown.parseMarkdown(text);
|
|
|
|
const headers: Header[] = [];
|
2023-12-28 15:14:30 +00:00
|
|
|
traverseTree(tree, (n) => {
|
2023-11-25 12:40:56 +00:00
|
|
|
if (n.type?.startsWith("ATXHeading")) {
|
|
|
|
headers.push({
|
2023-12-27 17:05:47 +00:00
|
|
|
name: n.children!.slice(1).map(renderToText).join("").trim(),
|
2023-11-25 12:40:56 +00:00
|
|
|
pos: n.from!,
|
|
|
|
level: +n.type[n.type.length - 1],
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2024-01-08 16:08:26 +00:00
|
|
|
|
|
|
|
let headerThreshold = defaultHeaderThreshold;
|
|
|
|
if (config.minHeaders) {
|
|
|
|
headerThreshold = config.minHeaders;
|
|
|
|
}
|
2023-11-25 12:40:56 +00:00
|
|
|
if (headers.length < headerThreshold) {
|
2024-01-04 19:08:12 +00:00
|
|
|
// Not enough headers, not showing TOC
|
2023-12-27 17:05:47 +00:00
|
|
|
return null;
|
2023-11-25 12:40:56 +00:00
|
|
|
}
|
2024-01-08 16:08:26 +00:00
|
|
|
let headerText = "# Table of Contents\n";
|
|
|
|
if (config.header === false) {
|
|
|
|
headerText = "";
|
|
|
|
}
|
2023-12-27 17:05:47 +00:00
|
|
|
// console.log("Headers", headers);
|
2024-01-02 10:40:59 +00:00
|
|
|
// Adjust level down if only sub-headers are used
|
|
|
|
const minLevel = headers.reduce(
|
|
|
|
(min, header) => Math.min(min, header.level),
|
|
|
|
6,
|
|
|
|
);
|
2024-01-08 16:08:26 +00:00
|
|
|
const renderedMd = headerText +
|
2023-12-27 17:05:47 +00:00
|
|
|
headers.map((header) =>
|
|
|
|
`${
|
2024-01-02 10:40:59 +00:00
|
|
|
" ".repeat((header.level - minLevel) * 2)
|
2023-12-27 17:05:47 +00:00
|
|
|
}* [[${page}@${header.pos}|${header.name}]]`
|
|
|
|
).join("\n");
|
|
|
|
// console.log("Markdown", renderedMd);
|
|
|
|
return {
|
|
|
|
markdown: renderedMd,
|
2023-12-28 15:14:30 +00:00
|
|
|
buttons: [
|
|
|
|
{
|
2024-01-08 16:08:26 +00:00
|
|
|
description: "Edit",
|
2023-12-28 15:14:30 +00:00
|
|
|
svg:
|
2024-01-08 16:08:26 +00:00
|
|
|
`<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-edit"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"></path><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"></path></svg>`,
|
|
|
|
invokeFunction: "query.editButton",
|
2023-12-28 15:14:30 +00:00
|
|
|
},
|
|
|
|
{
|
2024-01-08 16:08:26 +00:00
|
|
|
description: "Reload",
|
2023-12-28 15:14:30 +00:00
|
|
|
svg:
|
2024-01-08 16:08:26 +00:00
|
|
|
`<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-refresh-cw"><polyline points="23 4 23 10 17 10"></polyline><polyline points="1 20 1 14 7 14"></polyline><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"></path></svg>`,
|
|
|
|
invokeFunction: "index.refreshWidgets",
|
2023-12-28 15:14:30 +00:00
|
|
|
},
|
|
|
|
],
|
2023-12-27 17:05:47 +00:00
|
|
|
};
|
2023-11-25 12:40:56 +00:00
|
|
|
}
|