Skip to content

Commit 49a0e9e

Browse files
feat: Add task.links, to access links on task line
Co-Authored-By: rhhub <7444613+rhhub@users.noreply.github.com>
1 parent c99d241 commit 49a0e9e

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/Task/ListItem.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ export class ListItem {
203203
return this.file.cachedMetadata?.links ?? [];
204204
}
205205

206+
/**
207+
* Return a list of links in the task or list item's line.
208+
*
209+
* The data contest is documented here:
210+
* https://docs.obsidian.md/Reference/TypeScript+API/LinkCache
211+
*/
212+
public get links(): LinkCache[] {
213+
return this.linksInFileBody.filter((link) => link.position.start.line === this.lineNumber);
214+
}
215+
206216
/**
207217
* Return the name of the file containing this object, with the .md extension removed.
208218
*/

tests/Task/ListItem.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ describe('links', () => {
212212

213213
expect(tasks[0].linksInFileBody.length).toEqual(3);
214214
});
215+
216+
it('should return all links in the task line', () => {
217+
const tasks = readTasksFromSimulatedFile(links_everywhere);
218+
219+
expect(tasks[0].links.length).toEqual(1);
220+
expect(tasks[0].links[0].original).toEqual('[[link_in_task_wikilink]]');
221+
});
215222
});
216223

217224
describe('identicalTo', () => {

0 commit comments

Comments
 (0)