Skip to content

Commit 18b7bcc

Browse files
committed
Add task metadata and reindex all command
1 parent 47a2c88 commit 18b7bcc

7 files changed

Lines changed: 40 additions & 62 deletions

File tree

datacore.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ export class Datacore extends Component {
366366
//
367367
// @internal
368368
reads: EmbedQueue;
369+
reindex(): Promise<void>;
369370
reload(file: TFile): Promise<Indexable>;
370371
get revision(): number;
371372
// Warning: (ae-incompatible-release-tags) The symbol "settings" is marked as @public, but its signature references "Settings" which is marked as @internal

src/index/datacore.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ export class Datacore extends Component {
110110
this.index();
111111
}
112112

113+
/** Clears all current state and caches and reindexes the entire vault from scratch. */
114+
async reindex() {
115+
this.initialized = false;
116+
this.datastore.clear();
117+
118+
await this.persister.recreate();
119+
120+
await this.index();
121+
}
122+
113123
/** Indexes all documents in the vault. Wait on this if you want to wait for the whole index to be ready. */
114124
async index() {
115125
// Asynchronously initialize actual content in the background using a lifecycle-respecting object.

src/index/import/inline-field.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export function parseInlineValue(value: string): Literal {
201201
* - Look for any wrappers ('[' and '(') in the line, trying to parse whatever comes after it as an inline key::.
202202
* - If successful, scan until you find a matching end bracket, and parse whatever remains as an inline value.
203203
*/
204-
export function extractInlineFields(line: string, includeTaskFields: boolean = false): LocalInlineField[] {
204+
export function extractInlineFields(line: string): LocalInlineField[] {
205205
let fields: LocalInlineField[] = [];
206206
for (let wrapper of Object.keys(INLINE_FIELD_WRAPPERS)) {
207207
let foundIndex = line.indexOf(wrapper);
@@ -217,8 +217,6 @@ export function extractInlineFields(line: string, includeTaskFields: boolean = f
217217
}
218218
}
219219

220-
if (includeTaskFields) fields = fields.concat(extractSpecialTaskFields(line));
221-
222220
fields.sort((a, b) => a.start - b.start);
223221

224222
let filteredFields: LocalInlineField[] = [];
@@ -267,6 +265,7 @@ export const DONE_DATE_REGEX = /\u{2705}\s*(\d{4}-\d{2}-\d{2})/u;
267265
export const SCHEDULED_DATE_REGEX = /[\u{23F3}\u{231B}]\s*(\d{4}-\d{2}-\d{2})/u;
268266
export const START_DATE_REGEX = /\u{1F6EB}\s*(\d{4}-\d{2}-\d{2})/u;
269267

268+
/** All supported emoji metadata fields for a task. */
270269
export const EMOJI_REGEXES = [
271270
{ regex: CREATED_DATE_REGEX, key: "created" },
272271
{ regex: START_DATE_REGEX, key: "start" },
@@ -276,7 +275,7 @@ export const EMOJI_REGEXES = [
276275
];
277276

278277
/** Parse special completed/due/done task fields which are marked via emoji. */
279-
function extractSpecialTaskFields(line: string): LocalInlineField[] {
278+
export function extractSpecialTaskFields(line: string): LocalInlineField[] {
280279
let results: LocalInlineField[] = [];
281280

282281
for (let { regex, key } of EMOJI_REGEXES) {
@@ -319,8 +318,9 @@ export function setInlineField(source: string, key: string, value?: string): str
319318
return source;
320319
}
321320

321+
/** Updates a task item line with the 'completion' date emoji on being checked. */
322322
export function setEmojiShorthandCompletionField(source: string, value?: string): string {
323-
const existing = extractInlineFields(source, true);
323+
const existing = extractInlineFields(source);
324324
const existingKeys = existing.filter((f) => f.key === "completion" && f.wrapping === "emoji-shorthand");
325325

326326
// Don't do anything if there are duplicate keys OR the key already doesn't exist.

src/index/import/markdown.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
asInlineField,
1010
extractFullLineField,
1111
extractInlineFields,
12+
extractSpecialTaskFields,
1213
jsonInlineField,
1314
} from "./inline-field";
1415
import {
@@ -184,7 +185,7 @@ export function markdownSourceImport(
184185
// In the second list pass, actually construct the list heirarchy.
185186
for (const item of listItems.values()) {
186187
if (item.parentLine < 0) {
187-
const listBlock = blocks.getPairOrNextHigher(-item.parentLine)![1];
188+
const listBlock = blocks.getPairOrNextHigher(-item.parentLine - 1)![1];
188189
if (!listBlock || !(listBlock.type === "list")) continue;
189190

190191
(listBlock as ListBlockData).items.push(item);
@@ -243,6 +244,7 @@ export function markdownSourceImport(
243244
// Inline Fields //
244245
///////////////////
245246

247+
// All generic inline fields.
246248
for (const field of iterateInlineFields(lines)) {
247249
const line = field.position.line;
248250
markdownMetadata.inlineField(field);
@@ -251,6 +253,16 @@ export function markdownSourceImport(
251253
lookup(line, blocks)?.metadata.inlineField(field);
252254
lookup(line, listItems)?.metadata.inlineField(field);
253255
}
256+
257+
for (const item of listItems.values()) {
258+
for (let lineno = item.start; lineno < item.end; lineno++) {
259+
const taskInlineFields = extractSpecialTaskFields(lines[lineno]);
260+
for (const field of taskInlineFields) {
261+
item.metadata.inlineField(asInlineField(field, lineno));
262+
}
263+
}
264+
}
265+
254266
sectionArray.push(...sections.values());
255267
return {
256268
lines,

src/main.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ export default class DatacorePlugin extends Plugin {
4747
-100
4848
);
4949

50+
// Useful debug and non-debug commands.
51+
// Drops the current index and reindexes all items.
52+
this.addCommand({
53+
id: "datacore-reindex-vault",
54+
name: "Reindex entire vault",
55+
callback: async () => {
56+
console.log("Datacore: dropping the datastore and reindexing all items.");
57+
await this.core.reindex();
58+
},
59+
});
60+
5061
// Register JS highlighting for codeblocks.
5162
this.register(this.registerCodeblockHighlighting());
5263

src/ui/fields/editable.tsx

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/ui/fields/select.tsx

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)