Skip to content

Commit dfa35a8

Browse files
committed
feat: support inherit from frontmatter
1 parent 483e733 commit dfa35a8

5 files changed

Lines changed: 71 additions & 11032 deletions

File tree

.claude/settings.local.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(npm test)",
5+
"Bash(npm test:*)"
6+
],
7+
"deny": []
8+
}
9+
}

src/__tests__/FileMetadataInheritance.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ describe("File Metadata Inheritance", () => {
457457
const tasks = parser.parseLegacy(content, "test.md", fileMetadata);
458458

459459
expect(tasks).toHaveLength(1);
460-
// Should inherit as a single tag
461-
expect(tasks[0].metadata.tags).toContain("single-tag");
460+
// Should inherit as a single tag with # prefix
461+
expect(tasks[0].metadata.tags).toContain("#single-tag");
462462
});
463463
});
464464

src/components/task-view/TagViewComponent.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
3939
super(parentEl, app, plugin, config);
4040
}
4141

42+
/**
43+
* Normalize a tag to ensure it has a # prefix
44+
* @param tag The tag to normalize
45+
* @returns Normalized tag with # prefix
46+
*/
47+
private normalizeTag(tag: string): string {
48+
if (typeof tag !== 'string') {
49+
return tag;
50+
}
51+
52+
// Trim whitespace
53+
const trimmed = tag.trim();
54+
55+
// If empty or already starts with #, return as is
56+
if (!trimmed || trimmed.startsWith('#')) {
57+
return trimmed;
58+
}
59+
60+
// Add # prefix
61+
return `#${trimmed}`;
62+
}
63+
4264
/**
4365
* 重写基类中的索引构建方法,为标签创建索引
4466
*/
@@ -55,10 +77,13 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
5577
return;
5678
}
5779

58-
if (!this.allTagsMap.has(tag)) {
59-
this.allTagsMap.set(tag, new Set());
80+
// 规范化标签格式
81+
const normalizedTag = this.normalizeTag(tag);
82+
83+
if (!this.allTagsMap.has(normalizedTag)) {
84+
this.allTagsMap.set(normalizedTag, new Set());
6085
}
61-
this.allTagsMap.get(tag)?.add(task.id);
86+
this.allTagsMap.get(normalizedTag)?.add(task.id);
6287
});
6388
}
6489
});

src/utils/workers/ConfigurableTaskParser.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,16 +1303,42 @@ export class MarkdownTaskParser {
13031303
return metadata;
13041304
}
13051305

1306+
/**
1307+
* Normalize a tag to ensure it has a # prefix
1308+
* @param tag The tag to normalize
1309+
* @returns Normalized tag with # prefix
1310+
*/
1311+
private normalizeTag(tag: string): string {
1312+
if (typeof tag !== 'string') {
1313+
return tag;
1314+
}
1315+
1316+
// Trim whitespace
1317+
const trimmed = tag.trim();
1318+
1319+
// If empty or already starts with #, return as is
1320+
if (!trimmed || trimmed.startsWith('#')) {
1321+
return trimmed;
1322+
}
1323+
1324+
// Add # prefix
1325+
return `#${trimmed}`;
1326+
}
1327+
13061328
/**
13071329
* Merge tags from different sources, removing duplicates
13081330
* @param baseTags Base tags array (from task)
13091331
* @param inheritedTags Tags to inherit (from file metadata)
13101332
* @returns Merged tags array with duplicates removed
13111333
*/
13121334
private mergeTags(baseTags: string[], inheritedTags: string[]): string[] {
1313-
const merged = [...baseTags];
1335+
// Normalize all tags before merging
1336+
const normalizedBaseTags = baseTags.map(tag => this.normalizeTag(tag));
1337+
const normalizedInheritedTags = inheritedTags.map(tag => this.normalizeTag(tag));
1338+
1339+
const merged = [...normalizedBaseTags];
13141340

1315-
for (const tag of inheritedTags) {
1341+
for (const tag of normalizedInheritedTags) {
13161342
if (!merged.includes(tag)) {
13171343
merged.push(tag);
13181344
}
@@ -1461,7 +1487,9 @@ export class MarkdownTaskParser {
14611487
inherited["tags"] === null ||
14621488
inherited["tags"] === "")
14631489
) {
1464-
inherited["tags"] = JSON.stringify(value);
1490+
// Normalize tags before storing
1491+
const normalizedTags = value.map(tag => this.normalizeTag(tag));
1492+
inherited["tags"] = JSON.stringify(normalizedTags);
14651493
}
14661494
} else {
14671495
// Only inherit if:

0 commit comments

Comments
 (0)