Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/bases/basesFilterDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ function applyFilterRuleDefault(
return;
}

// Generated relationship views (e.g. the default Subtasks tab) normalize each
// link before comparing, producing a rule such as:
// file.hasLink(this.file) && list(note.projects).map(...).contains(this.file.asLink())
// The generic matcher below cannot reduce that prefix to a field name, so detect
// the normalized list/map form explicitly and recover the wrapped property.
const mappedListContainsCurrentFileMatch = trimmedRule.match(
/(?:^|&&\s*)(list\((?:note\.|task\.)?[\w-]+\))\.map\([\s\S]*\)\.contains\(this\.file\.asLink\(\)\)$/
);
if (mappedListContainsCurrentFileMatch) {
const property = normalizeFilterProperty(mappedListContainsCurrentFileMatch[1], options);
const currentFileLink = resolveCurrentFileLink(options.currentFileLink);
if (property && currentFileLink) {
addFrontmatterDefault(defaults, property, currentFileLink, options.fieldMapper);
}
return;
}

const currentFileContainsMatch = trimmedRule.match(
/^(.+?)\.contains\(this\.file\.asLink\(\)\)$/
);
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/bases/basesFilterDefaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,51 @@ describe("Bases filter defaults", () => {

expect(defaults).toEqual({});
});

it("extracts the project default from the generated normalized relationship filter", () => {
// This is the filter the default Subtasks relationship view generates.
const generatedSubtasksFilter =
'file.hasLink(this.file) && list(note.projects).map(file(value.replace(/^\\[[^\\]]+\\]\\((.*)\\)$/, "$1").replace(/%20/g, " ")).asLink()).contains(this.file.asLink())';

const defaults = extractBasesFilterDefaults({
config: {
query: {
filters: {
conjunction: "and",
filters: [
{ rule: { text: 'file.hasTag("task")' } },
{ rule: { text: generatedSubtasksFilter } },
],
},
},
},
fieldMapper: createFieldMapper(),
taskTag: "task",
currentFileLink: "[[Current]]",
});

expect(defaults).toEqual({
projects: ["[[Current]]"],
});
});

it("honors field mapping for the generated normalized relationship filter", () => {
const generatedSubtasksFilter =
'file.hasLink(this.file) && list(note.projectLinks).map(file(value.replace(/^\\[[^\\]]+\\]\\((.*)\\)$/, "$1").replace(/%20/g, " ")).asLink()).contains(this.file.asLink())';

const defaults = extractBasesFilterDefaults({
config: {
filters: {
rule: { text: generatedSubtasksFilter },
},
},
fieldMapper: createFieldMapper({ projects: "projectLinks" }),
taskTag: "task",
currentFileLink: "[[Current]]",
});

expect(defaults).toEqual({
projectLinks: ["[[Current]]"],
});
});
});