Fixes #771
parent
15be3c3444
commit
63eb99e0d3
|
@ -1,34 +1,62 @@
|
||||||
import { collectNodesMatching } from "$sb/lib/tree.ts";
|
import {
|
||||||
|
collectNodesMatching,
|
||||||
|
collectNodesOfType,
|
||||||
|
renderToText,
|
||||||
|
} from "$sb/lib/tree.ts";
|
||||||
import type { CompleteEvent, IndexTreeEvent } from "../../plug-api/types.ts";
|
import type { CompleteEvent, IndexTreeEvent } from "../../plug-api/types.ts";
|
||||||
import { ObjectValue } from "../../plug-api/types.ts";
|
import { ObjectValue } from "../../plug-api/types.ts";
|
||||||
import { indexObjects, queryObjects } from "./api.ts";
|
import { indexObjects, queryObjects } from "./api.ts";
|
||||||
import { parsePageRef } from "$sb/lib/page_ref.ts";
|
import { parsePageRef } from "$sb/lib/page_ref.ts";
|
||||||
|
import { extractAttributes } from "$sb/lib/attribute.ts";
|
||||||
|
|
||||||
type HeaderObject = ObjectValue<{
|
type HeaderObject = ObjectValue<
|
||||||
name: string;
|
{
|
||||||
page: string;
|
name: string;
|
||||||
level: number;
|
page: string;
|
||||||
pos: number;
|
level: number;
|
||||||
}>;
|
pos: number;
|
||||||
|
} & Record<string, any>
|
||||||
|
>;
|
||||||
|
|
||||||
export async function indexHeaders({ name: pageName, tree }: IndexTreeEvent) {
|
export async function indexHeaders({ name: pageName, tree }: IndexTreeEvent) {
|
||||||
const headers: ObjectValue<HeaderObject>[] = [];
|
const headers: ObjectValue<HeaderObject>[] = [];
|
||||||
|
|
||||||
collectNodesMatching(tree, (t) => !!t.type?.startsWith("ATXHeading")).forEach(
|
for (
|
||||||
(n) => {
|
const n of collectNodesMatching(
|
||||||
const level = +n.type!.substring("ATXHeading".length);
|
tree,
|
||||||
const name = n.children![1].text!.trim();
|
(t) => !!t.type?.startsWith("ATXHeading"),
|
||||||
headers.push({
|
)
|
||||||
ref: `${pageName}#${name}@${n.from}`,
|
) {
|
||||||
tag: "header",
|
const level = +n.type!.substring("ATXHeading".length);
|
||||||
level,
|
const tags = new Set<string>();
|
||||||
name,
|
|
||||||
page: pageName,
|
collectNodesOfType(n, "Hashtag").forEach((h) => {
|
||||||
pos: n.from!,
|
// Push tag to the list, removing the initial #
|
||||||
});
|
tags.add(h.children![0].text!.substring(1));
|
||||||
},
|
h.children = [];
|
||||||
);
|
});
|
||||||
// console.log("Found", headers.length, "headers(s)");
|
|
||||||
|
// Extract attributes and remove from tree
|
||||||
|
const extractedAttributes = await extractAttributes(
|
||||||
|
["header", ...tags],
|
||||||
|
n,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
const name = n.children!.slice(1).map(renderToText).join("").trim();
|
||||||
|
|
||||||
|
headers.push({
|
||||||
|
ref: `${pageName}#${name}@${n.from}`,
|
||||||
|
tag: "header",
|
||||||
|
tags: [...tags],
|
||||||
|
level,
|
||||||
|
name,
|
||||||
|
page: pageName,
|
||||||
|
pos: n.from!,
|
||||||
|
...extractedAttributes,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Found", headers, "headers(s)");
|
||||||
await indexObjects(pageName, headers);
|
await indexObjects(pageName, headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -140,7 +140,7 @@ SilverBullet indexes various types of content as [[Objects]]. There are various
|
||||||
The `silverbullet.registerAttributeExtractor` API takes two arguments:
|
The `silverbullet.registerAttributeExtractor` API takes two arguments:
|
||||||
|
|
||||||
* `def` with currently just one option:
|
* `def` with currently just one option:
|
||||||
* `tags`: Array of tags this extractor should be applied to, could be a built-in tag such as `item`, `page` or `task`, but also any custom tags you define.
|
* `tags`: Array of tags this extractor should be applied to, could be a built-in tag such as `item`, `page`, `paragraph`, `header`, or `task`, but also any custom tags you define.
|
||||||
* `callback`: the callback function to invoke (can be `async` or not). This callback is passed the following arguments:
|
* `callback`: the callback function to invoke (can be `async` or not). This callback is passed the following arguments:
|
||||||
* `text`: the text of the object to extract attributes for
|
* `text`: the text of the object to extract attributes for
|
||||||
* return value: an object of attribute mappings, possibly overriding built-in ones.
|
* return value: an object of attribute mappings, possibly overriding built-in ones.
|
||||||
|
|
Loading…
Reference in New Issue