Implemented queries for tables (#729)
parent
aaad3dbf0a
commit
4056c4f2f2
|
@ -25,11 +25,11 @@ functions:
|
||||||
objectSourceProvider:
|
objectSourceProvider:
|
||||||
path: api.ts:objectSourceProvider
|
path: api.ts:objectSourceProvider
|
||||||
events:
|
events:
|
||||||
- query:*
|
- query:*
|
||||||
discoverSources:
|
discoverSources:
|
||||||
path: api.ts:discoverSources
|
path: api.ts:discoverSources
|
||||||
events:
|
events:
|
||||||
- query_
|
- query_
|
||||||
clearIndex:
|
clearIndex:
|
||||||
path: api.ts:clearIndex
|
path: api.ts:clearIndex
|
||||||
env: server
|
env: server
|
||||||
|
@ -55,9 +55,9 @@ functions:
|
||||||
processIndexQueue:
|
processIndexQueue:
|
||||||
path: ./command.ts:processIndexQueue
|
path: ./command.ts:processIndexQueue
|
||||||
mqSubscriptions:
|
mqSubscriptions:
|
||||||
- queue: indexQueue
|
- queue: indexQueue
|
||||||
batchSize: 10
|
batchSize: 10
|
||||||
autoAck: true
|
autoAck: true
|
||||||
reindexSpace:
|
reindexSpace:
|
||||||
path: "./command.ts:reindexSpace"
|
path: "./command.ts:reindexSpace"
|
||||||
env: server
|
env: server
|
||||||
|
@ -70,45 +70,51 @@ functions:
|
||||||
indexParagraphs:
|
indexParagraphs:
|
||||||
path: "./paragraph.ts:indexParagraphs"
|
path: "./paragraph.ts:indexParagraphs"
|
||||||
events:
|
events:
|
||||||
- page:index
|
- page:index
|
||||||
|
|
||||||
# Backlinks
|
# Backlinks
|
||||||
indexLinks:
|
indexLinks:
|
||||||
path: "./page_links.ts:indexLinks"
|
path: "./page_links.ts:indexLinks"
|
||||||
events:
|
events:
|
||||||
- page:index
|
- page:index
|
||||||
|
|
||||||
attributeComplete:
|
attributeComplete:
|
||||||
path: "./attributes.ts:attributeComplete"
|
path: "./attributes.ts:attributeComplete"
|
||||||
events:
|
events:
|
||||||
- editor:complete
|
- editor:complete
|
||||||
|
|
||||||
objectAttributeCompleter:
|
objectAttributeCompleter:
|
||||||
path: ./attributes.ts:objectAttributeCompleter
|
path: ./attributes.ts:objectAttributeCompleter
|
||||||
events:
|
events:
|
||||||
- attribute:complete:*
|
- attribute:complete:*
|
||||||
|
|
||||||
# Item indexing
|
# Item indexing
|
||||||
indexItem:
|
indexItem:
|
||||||
path: "./item.ts:indexItems"
|
path: "./item.ts:indexItems"
|
||||||
events:
|
events:
|
||||||
- page:index
|
- page:index
|
||||||
|
|
||||||
# Anchors
|
# Anchors
|
||||||
indexAnchors:
|
indexAnchors:
|
||||||
path: "./anchor.ts:indexAnchors"
|
path: "./anchor.ts:indexAnchors"
|
||||||
events:
|
events:
|
||||||
- page:index
|
- page:index
|
||||||
anchorComplete:
|
anchorComplete:
|
||||||
path: "./anchor.ts:anchorComplete"
|
path: "./anchor.ts:anchorComplete"
|
||||||
events:
|
events:
|
||||||
- editor:complete
|
- editor:complete
|
||||||
|
|
||||||
|
# Tables
|
||||||
|
indexTables:
|
||||||
|
path: "table.ts:indexTables"
|
||||||
|
events:
|
||||||
|
- page:index
|
||||||
|
|
||||||
# Headers
|
# Headers
|
||||||
indexHeaders:
|
indexHeaders:
|
||||||
path: header.ts:indexHeaders
|
path: header.ts:indexHeaders
|
||||||
events:
|
events:
|
||||||
- page:index
|
- page:index
|
||||||
headerComplete:
|
headerComplete:
|
||||||
path: header.ts:headerComplete
|
path: header.ts:headerComplete
|
||||||
events:
|
events:
|
||||||
|
@ -118,20 +124,19 @@ functions:
|
||||||
indexData:
|
indexData:
|
||||||
path: data.ts:indexData
|
path: data.ts:indexData
|
||||||
events:
|
events:
|
||||||
- page:index
|
- page:index
|
||||||
|
|
||||||
# Script
|
# Script
|
||||||
indexSpaceScript:
|
indexSpaceScript:
|
||||||
path: script.ts:indexSpaceScript
|
path: script.ts:indexSpaceScript
|
||||||
events:
|
events:
|
||||||
- page:index
|
- page:index
|
||||||
|
|
||||||
|
|
||||||
# Hashtags
|
# Hashtags
|
||||||
indexTags:
|
indexTags:
|
||||||
path: tags.ts:indexTags
|
path: tags.ts:indexTags
|
||||||
events:
|
events:
|
||||||
- page:index
|
- page:index
|
||||||
tagComplete:
|
tagComplete:
|
||||||
path: tags.ts:tagComplete
|
path: tags.ts:tagComplete
|
||||||
events:
|
events:
|
||||||
|
@ -181,7 +186,7 @@ functions:
|
||||||
lintYAML:
|
lintYAML:
|
||||||
path: lint.ts:lintYAML
|
path: lint.ts:lintYAML
|
||||||
events:
|
events:
|
||||||
- editor:lint
|
- editor:lint
|
||||||
|
|
||||||
# Tag file system
|
# Tag file system
|
||||||
readFileTag:
|
readFileTag:
|
||||||
|
@ -205,9 +210,9 @@ functions:
|
||||||
syscallSourceProvider:
|
syscallSourceProvider:
|
||||||
path: builtins.ts:syscallSourceProvider
|
path: builtins.ts:syscallSourceProvider
|
||||||
events:
|
events:
|
||||||
- query:syscall
|
- query:syscall
|
||||||
|
|
||||||
commandSourceProvider:
|
commandSourceProvider:
|
||||||
path: builtins.ts:commandSourceProvider
|
path: builtins.ts:commandSourceProvider
|
||||||
events:
|
events:
|
||||||
- query:command
|
- query:command
|
||||||
|
|
|
@ -0,0 +1,76 @@
|
||||||
|
import { CompleteEvent, IndexTreeEvent, ObjectValue } from "$type/types.ts";
|
||||||
|
import { collectNodesMatching, ParseTree } from "$lib/tree.ts";
|
||||||
|
import { indexObjects } from "./api.ts";
|
||||||
|
import { collectNodesOfType } from "$lib/tree.ts";
|
||||||
|
|
||||||
|
type TableRowObject =
|
||||||
|
& ObjectValue<{
|
||||||
|
tableref: string;
|
||||||
|
page: string;
|
||||||
|
pos: number;
|
||||||
|
}>
|
||||||
|
& Record<string, any>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace any invalid characters in a string so it can serve as indexed field name in a query
|
||||||
|
* @param str the input string
|
||||||
|
* @returns All lowercase string with special chars replaced with underscore
|
||||||
|
*/
|
||||||
|
function cleanHeaderFieldName(str: string): string {
|
||||||
|
return str.replace(/[\W_]+/g, "_").toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Concat text properties of all child nodes
|
||||||
|
* @param nodes
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
function concatChildrenTexts(nodes: ParseTree[]): string {
|
||||||
|
return nodes.map((c) => c.text).join("").trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function indexTables({ name: pageName, tree }: IndexTreeEvent) {
|
||||||
|
console.log(`Indexing tables in '${pageName}'`);
|
||||||
|
const result: ObjectValue<TableRowObject>[] = [];
|
||||||
|
|
||||||
|
collectNodesMatching(tree, (t) => !!t.type?.startsWith("Table")).forEach(
|
||||||
|
(table) => {
|
||||||
|
const rows = collectNodesOfType(table, "TableRow");
|
||||||
|
const header = collectNodesOfType(table, "TableHeader")[0]; //Use first header. As per markdown spec there can only be exactly one
|
||||||
|
const headerLabels = collectNodesOfType(header, "TableCell").map((cell) =>
|
||||||
|
concatChildrenTexts(cell.children!)
|
||||||
|
).map(cleanHeaderFieldName);
|
||||||
|
//console.log("Header labels", headerLabels);
|
||||||
|
|
||||||
|
for (const row of rows) {
|
||||||
|
const tags = new Set<string>();
|
||||||
|
collectNodesOfType(row, "Hashtag").forEach((h) => {
|
||||||
|
// Push tag to the list, removing the initial #
|
||||||
|
tags.add(h.children![0].text!.substring(1));
|
||||||
|
});
|
||||||
|
|
||||||
|
const cells = collectNodesOfType(row, "TableCell");
|
||||||
|
|
||||||
|
const tableRow: TableRowObject = {
|
||||||
|
tableref: `${pageName}@${table.from}`,
|
||||||
|
ref: `${pageName}@${row.from}`,
|
||||||
|
tag: "table",
|
||||||
|
tags: [...tags],
|
||||||
|
page: pageName,
|
||||||
|
pos: row.from!,
|
||||||
|
};
|
||||||
|
cells.forEach((c, i) => {
|
||||||
|
const content = concatChildrenTexts(c.children!);
|
||||||
|
const label = headerLabels[i];
|
||||||
|
tableRow[label!] = content;
|
||||||
|
});
|
||||||
|
result.push(tableRow);
|
||||||
|
|
||||||
|
//console.log("Cells", cells);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log("Found", result.length, "row(s)");
|
||||||
|
await indexObjects(pageName, result);
|
||||||
|
}
|
|
@ -37,6 +37,22 @@ Note that you can also query this page using the `example-tag` directly:
|
||||||
example-tag
|
example-tag
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## table
|
||||||
|
Markdown table rows are indexed using the `table` tag, any additional tags can be added using [[Tags]] in any of its cells.
|
||||||
|
|
||||||
|
| Title | Description Text |
|
||||||
|
| --- | ----- |
|
||||||
|
| This is some key | The value contains a #table-tag |
|
||||||
|
| Some Row | This is an example row in between two others |
|
||||||
|
| Another key | This time without a tag |
|
||||||
|
|
||||||
|
|
||||||
|
```query
|
||||||
|
table
|
||||||
|
```
|
||||||
|
|
||||||
|
Table headers will be normalized by converting them to lowercase and replacing all non alphanumeric characters with `_`.
|
||||||
|
|
||||||
## task
|
## task
|
||||||
|
|
||||||
task
|
task
|
||||||
|
|
Loading…
Reference in New Issue