Skip to content

Commit d903821

Browse files
committed
refactor: - Add non_task example, to show these are not in the CachedMetadata
This involved making getTasksFromFileContent2() accept CachedMetadata with no ListItemCache.
1 parent 9c056b0 commit d903821

File tree

5 files changed

+192
-4
lines changed

5 files changed

+192
-4
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# non_tasks
2+
3+
```tasks
4+
path includes {{query.file.path}}
5+
```
6+
7+
```
8+
- [ ] I look like a task but am in a code block
9+
```
10+
11+
Below is an HTML comment in `<!-- .... ->`, so the "task" inside is not seen by Obsidian or any of its task-based plugins.
12+
13+
<!--
14+
- [ ] I look like a task but am in an HTML comment
15+
-->
16+
17+
Below is an Obsidian comment in `%% ... %%`, so the "task" inside is not seen by Obsidian or any of its task-based plugins.
18+
19+
%%
20+
- [ ] I look like a task but am in an Obsidian (percent) comment
21+
%%

src/Obsidian/Cache.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ export enum State {
2222
export function getTasksFromFileContent2(
2323
filePath: string,
2424
fileContent: string,
25-
listItems: ListItemCache[],
25+
listItems: ListItemCache[] | undefined,
2626
logger: Logger,
2727
fileCache: CachedMetadata,
2828
errorReporter: (e: any, filePath: string, listItem: ListItemCache, line: string) => void,
2929
) {
30-
const tasksFile = new TasksFile(filePath, fileCache);
3130
const tasks: Task[] = [];
31+
if (listItems === undefined) {
32+
// When called via Cache, this function would never be called or files without list items.
33+
// It is useful for tests to be act gracefully on sample Markdown files with no list items, however.
34+
return tasks;
35+
}
36+
37+
const tasksFile = new TasksFile(filePath, fileCache);
3238
const fileLines = fileContent.split('\n');
3339
const linesInFile = fileLines.length;
3440

tests/Obsidian/AllCacheSampleData.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { multi_line_task_and_list_item } from './__test_data__/multi_line_task_a
4444
import { multiple_headings } from './__test_data__/multiple_headings';
4545
import { no_heading } from './__test_data__/no_heading';
4646
import { no_yaml } from './__test_data__/no_yaml';
47+
import { non_tasks } from './__test_data__/non_tasks';
4748
import { one_task } from './__test_data__/one_task';
4849
import { yaml_1_alias } from './__test_data__/yaml_1_alias';
4950
import { yaml_2_aliases } from './__test_data__/yaml_2_aliases';
@@ -108,6 +109,7 @@ export function allCacheSampleData() {
108109
multiple_headings,
109110
no_heading,
110111
no_yaml,
112+
non_tasks,
111113
one_task,
112114
yaml_1_alias,
113115
yaml_2_aliases,

tests/Obsidian/Cache.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,13 @@ describe('all mock files', () => {
627627

628628
it.each(listPathAndData(files))(
629629
'should be able to read tasks from all mock files: "%s"',
630-
(_path: string, file: any) => {
630+
(path: string, file: any) => {
631631
const tasks = readTasksFromSimulatedFile(file);
632-
expect(tasks.length).toBeGreaterThan(0);
632+
if (path === 'Test Data/non_tasks.md') {
633+
expect(tasks.length).toEqual(0);
634+
} else {
635+
expect(tasks.length).toBeGreaterThan(0);
636+
}
633637
},
634638
);
635639
});
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
export const non_tasks = {
2+
filePath: 'Test Data/non_tasks.md',
3+
fileContents:
4+
'# non_tasks\n' +
5+
'\n' +
6+
'```tasks\n' +
7+
'path includes {{query.file.path}}\n' +
8+
'```\n' +
9+
'\n' +
10+
'```\n' +
11+
'- [ ] I look like a task but am in a code block\n' +
12+
'```\n' +
13+
'\n' +
14+
'Below is an HTML comment in `<!-- .... ->`, so the "task" inside is not seen by Obsidian or any of its task-based plugins.\n' +
15+
'\n' +
16+
'<!--\n' +
17+
'- [ ] I look like a task but am in an HTML comment\n' +
18+
'-->\n' +
19+
'\n' +
20+
'Below is an Obsidian comment in `%% ... %%`, so the "task" inside is not seen by Obsidian or any of its task-based plugins.\n' +
21+
'\n' +
22+
'%%\n' +
23+
'- [ ] I look like a task but am in an Obsidian (percent) comment\n' +
24+
'%%\n',
25+
cachedMetadata: {
26+
headings: [
27+
{
28+
position: {
29+
start: {
30+
line: 0,
31+
col: 0,
32+
offset: 0,
33+
},
34+
end: {
35+
line: 0,
36+
col: 11,
37+
offset: 11,
38+
},
39+
},
40+
heading: 'non_tasks',
41+
level: 1,
42+
},
43+
],
44+
sections: [
45+
{
46+
type: 'heading',
47+
position: {
48+
start: {
49+
line: 0,
50+
col: 0,
51+
offset: 0,
52+
},
53+
end: {
54+
line: 0,
55+
col: 11,
56+
offset: 11,
57+
},
58+
},
59+
},
60+
{
61+
type: 'code',
62+
position: {
63+
start: {
64+
line: 2,
65+
col: 0,
66+
offset: 13,
67+
},
68+
end: {
69+
line: 4,
70+
col: 3,
71+
offset: 59,
72+
},
73+
},
74+
},
75+
{
76+
type: 'code',
77+
position: {
78+
start: {
79+
line: 6,
80+
col: 0,
81+
offset: 61,
82+
},
83+
end: {
84+
line: 8,
85+
col: 3,
86+
offset: 116,
87+
},
88+
},
89+
},
90+
{
91+
type: 'paragraph',
92+
position: {
93+
start: {
94+
line: 10,
95+
col: 0,
96+
offset: 118,
97+
},
98+
end: {
99+
line: 10,
100+
col: 122,
101+
offset: 240,
102+
},
103+
},
104+
},
105+
{
106+
type: 'html',
107+
position: {
108+
start: {
109+
line: 12,
110+
col: 0,
111+
offset: 242,
112+
},
113+
end: {
114+
line: 14,
115+
col: 3,
116+
offset: 301,
117+
},
118+
},
119+
},
120+
{
121+
type: 'paragraph',
122+
position: {
123+
start: {
124+
line: 16,
125+
col: 0,
126+
offset: 303,
127+
},
128+
end: {
129+
line: 16,
130+
col: 123,
131+
offset: 426,
132+
},
133+
},
134+
},
135+
{
136+
type: 'comment',
137+
position: {
138+
start: {
139+
line: 18,
140+
col: 0,
141+
offset: 428,
142+
},
143+
end: {
144+
line: 20,
145+
col: 2,
146+
offset: 498,
147+
},
148+
},
149+
},
150+
],
151+
},
152+
obsidianApiVersion: '1.7.5',
153+
getAllTags: [],
154+
parseFrontMatterTags: null,
155+
};

0 commit comments

Comments
 (0)