Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve access to tags #2949

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ async function convertMarkdownFileToTestFunction(filePath, tp) {
const fileContents = await app.vault.read(tFile);
const cachedMetadata = app.metadataCache.getFileCache(tFile);
const obsidianApiVersion = tp.obsidian.apiVersion;
const data = { filePath, fileContents, cachedMetadata, obsidianApiVersion };
const getAllTags = tp.obsidian.getAllTags(cachedMetadata);
const parseFrontMatterTags = tp.obsidian.parseFrontMatterTags(cachedMetadata.frontmatter);
const data = { filePath, fileContents, cachedMetadata, obsidianApiVersion, getAllTags, parseFrontMatterTags };

const filename = filePath.split('/')[1].replace('.md', '');
if (filename.includes(' ')) {
Expand Down
12 changes: 10 additions & 2 deletions src/Scripting/TasksFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ import { type CachedMetadata, type FrontMatterCache, getAllTags, parseFrontMatte
export class TasksFile {
private readonly _path: string;
private readonly _cachedMetadata: CachedMetadata;
private readonly _frontmatter: FrontMatterCache = {} as FrontMatterCache;

constructor(path: string, cachedMetadata: CachedMetadata = {}) {
this._path = path;
this._cachedMetadata = cachedMetadata;

const rawFrontmatter = cachedMetadata.frontmatter;
if (rawFrontmatter !== undefined) {
this._frontmatter = JSON.parse(JSON.stringify(rawFrontmatter));
this._frontmatter.tags = parseFrontMatterTags(rawFrontmatter);
}
}

/**
Expand All @@ -29,7 +36,8 @@ export class TasksFile {
* @todo Review presence of global filter tag in the results.
*/
get tags(): string[] {
return getAllTags(this.cachedMetadata) ?? [];
const tags = getAllTags(this.cachedMetadata) ?? [];
return [...new Set(tags)];
}

/**
Expand Down Expand Up @@ -71,7 +79,7 @@ export class TasksFile {
* or if the markdown file has no frontmatter or empty frontmatter.
*/
public get frontmatter(): FrontMatterCache {
return this._cachedMetadata.frontmatter ?? ({} as FrontMatterCache);
return this._frontmatter;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Obsidian/__test_data__/blockquote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,6 @@ export const blockquote = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task'],
parseFrontMatterTags: null,
};
2 changes: 2 additions & 0 deletions tests/Obsidian/__test_data__/callout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,6 @@ export const callout = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task'],
parseFrontMatterTags: null,
};
2 changes: 2 additions & 0 deletions tests/Obsidian/__test_data__/callout_custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,6 @@ export const callout_custom = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task'],
parseFrontMatterTags: null,
};
2 changes: 2 additions & 0 deletions tests/Obsidian/__test_data__/callout_labelled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,6 @@ export const callout_labelled = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task'],
parseFrontMatterTags: null,
};
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,6 @@ export const callouts_nested_issue_2890_labelled = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task', '#task', '#task'],
parseFrontMatterTags: null,
};
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,6 @@ export const callouts_nested_issue_2890_unlabelled = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task', '#task', '#task'],
parseFrontMatterTags: null,
};
2 changes: 2 additions & 0 deletions tests/Obsidian/__test_data__/comments_html_style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,6 @@ export const comments_html_style = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task'],
parseFrontMatterTags: null,
};
2 changes: 2 additions & 0 deletions tests/Obsidian/__test_data__/comments_markdown_style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,6 @@ export const comments_markdown_style = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task', '#i-am-parsed-by-obsidian', '#task'],
parseFrontMatterTags: null,
};
12 changes: 7 additions & 5 deletions tests/Obsidian/__test_data__/empty_yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export const empty_yaml = {
cachedMetadata: {
tags: [
{
tag: '#task',
position: {
start: {
line: 5,
Expand All @@ -17,12 +16,11 @@ export const empty_yaml = {
offset: 34,
},
},
tag: '#task',
},
],
headings: [
{
heading: 'empty_yaml',
level: 1,
position: {
start: {
line: 3,
Expand All @@ -35,6 +33,8 @@ export const empty_yaml = {
offset: 21,
},
},
heading: 'empty_yaml',
level: 1,
},
],
sections: [
Expand Down Expand Up @@ -86,8 +86,6 @@ export const empty_yaml = {
],
listItems: [
{
parent: -5,
task: ' ',
position: {
start: {
line: 5,
Expand All @@ -100,8 +98,12 @@ export const empty_yaml = {
offset: 55,
},
},
parent: -5,
task: ' ',
},
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task'],
parseFrontMatterTags: null,
};
14 changes: 8 additions & 6 deletions tests/Obsidian/__test_data__/example_kanban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const example_kanban = {
cachedMetadata: {
tags: [
{
tag: '#task',
position: {
start: {
line: 8,
Expand All @@ -33,12 +32,11 @@ export const example_kanban = {
offset: 55,
},
},
tag: '#task',
},
],
headings: [
{
heading: 'Backlog',
level: 2,
position: {
start: {
line: 6,
Expand All @@ -51,6 +49,8 @@ export const example_kanban = {
offset: 42,
},
},
heading: 'Backlog',
level: 2,
},
],
sections: [
Expand Down Expand Up @@ -117,8 +117,6 @@ export const example_kanban = {
],
listItems: [
{
parent: -8,
task: ' ',
position: {
start: {
line: 8,
Expand All @@ -131,12 +129,13 @@ export const example_kanban = {
offset: 80,
},
},
parent: -8,
task: ' ',
},
],
frontmatter: {
'kanban-plugin': 'basic',
},
frontmatterLinks: [],
frontmatterPosition: {
start: {
line: 0,
Expand All @@ -149,6 +148,9 @@ export const example_kanban = {
offset: 30,
},
},
frontmatterLinks: [],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task'],
parseFrontMatterTags: null,
};
2 changes: 2 additions & 0 deletions tests/Obsidian/__test_data__/inheritance_1parent1child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,6 @@ export const inheritance_1parent1child = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task'],
parseFrontMatterTags: null,
};
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,6 @@ export const inheritance_1parent1child1newroot_after_header = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task', '#task'],
parseFrontMatterTags: null,
};
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,6 @@ export const inheritance_1parent1child1sibling_emptystring = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task', '#task'],
parseFrontMatterTags: null,
};
2 changes: 2 additions & 0 deletions tests/Obsidian/__test_data__/inheritance_1parent2children.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,6 @@ export const inheritance_1parent2children = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task', '#task'],
parseFrontMatterTags: null,
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const inheritance_1parent2children1grandchild = {
cachedMetadata: {
tags: [
{
tag: '#task',
position: {
start: {
line: 0,
Expand All @@ -21,9 +20,9 @@ export const inheritance_1parent2children1grandchild = {
offset: 11,
},
},
tag: '#task',
},
{
tag: '#task',
position: {
start: {
line: 1,
Expand All @@ -36,9 +35,9 @@ export const inheritance_1parent2children1grandchild = {
offset: 39,
},
},
tag: '#task',
},
{
tag: '#task',
position: {
start: {
line: 2,
Expand All @@ -51,9 +50,9 @@ export const inheritance_1parent2children1grandchild = {
offset: 68,
},
},
tag: '#task',
},
{
tag: '#task',
position: {
start: {
line: 3,
Expand All @@ -66,6 +65,7 @@ export const inheritance_1parent2children1grandchild = {
offset: 101,
},
},
tag: '#task',
},
],
sections: [
Expand All @@ -87,8 +87,6 @@ export const inheritance_1parent2children1grandchild = {
],
listItems: [
{
parent: -1,
task: ' ',
position: {
start: {
line: 0,
Expand All @@ -101,10 +99,10 @@ export const inheritance_1parent2children1grandchild = {
offset: 23,
},
},
parent: -1,
task: ' ',
},
{
parent: 0,
task: ' ',
position: {
start: {
line: 1,
Expand All @@ -117,10 +115,10 @@ export const inheritance_1parent2children1grandchild = {
offset: 52,
},
},
},
{
parent: 0,
task: ' ',
},
{
position: {
start: {
line: 2,
Expand All @@ -133,10 +131,10 @@ export const inheritance_1parent2children1grandchild = {
offset: 81,
},
},
parent: 0,
task: ' ',
},
{
parent: 2,
task: ' ',
position: {
start: {
line: 3,
Expand All @@ -149,8 +147,12 @@ export const inheritance_1parent2children1grandchild = {
offset: 114,
},
},
parent: 2,
task: ' ',
},
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task', '#task', '#task'],
parseFrontMatterTags: null,
};
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,6 @@ export const inheritance_1parent2children1sibling = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task', '#task', '#task'],
parseFrontMatterTags: null,
};
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@ export const inheritance_1parent2children2grandchildren = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task', '#task', '#task', '#task'],
parseFrontMatterTags: null,
};
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,6 @@ export const inheritance_1parent2children2grandchildren1sibling = {
],
},
obsidianApiVersion: '1.6.5',
getAllTags: ['#task', '#task', '#task', '#task', '#task', '#task'],
parseFrontMatterTags: null,
};
Loading