Skip to content

Commit 3f05bc4

Browse files
authored
Merge pull request #3340 from obsidian-tasks-group/task.lineNumber
docs: Document `task.lineNumber` for sorting by line number
2 parents ae1524e + db7ca61 commit 3f05bc4

18 files changed

+160
-6
lines changed

docs/Queries/Filters.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,19 @@ For example, this could be used to extract information from `task.originalMarkdo
12791279

12801280
<!-- placeholder to force blank line after included text --><!-- endInclude -->
12811281

1282+
### Line Number
1283+
1284+
There is no built-in instruction to filter by the task's line number.
1285+
1286+
Since Tasks X.Y.Z, **[[Custom Filters|custom filtering]] by the task's line number** is now possible, using `task.lineNumber`.
1287+
1288+
> [!tip]
1289+
> With `task.lineNumber`, the first line in the file is on line number `0` (zero), not `1` (one).
1290+
1291+
<!-- placeholder to force blank line before included text --><!-- include: CustomFilteringExamples.test.other_properties_task.lineNumber_docs.approved.md -->
1292+
1293+
<!-- placeholder to force blank line after included text --><!-- endInclude -->
1294+
12821295
## Filters for File Properties
12831296

12841297
These filters allow searching for tasks in particular files and sections of files.

docs/Queries/Grouping.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,19 @@ group by function task.originalMarkdown.replace(/^[^\[\]]+\[.\] */, '')
828828
829829
<!-- placeholder to force blank line after included text --><!-- endInclude -->
830830
831+
### Line Number
832+
833+
There is no built-in instruction to group by the task's line number.
834+
835+
Since Tasks X.Y.Z, **[[Custom Grouping|custom grouping]] by the task's line number** is now possible, using `task.lineNumber`.
836+
837+
> [!tip]
838+
> With `task.lineNumber`, the first line in the file is on line number `0` (zero), not `1` (one).
839+
840+
<!-- placeholder to force blank line before included text --><!-- include: CustomGroupingExamples.test.other_properties_task.lineNumber_docs.approved.md -->
841+
842+
<!-- placeholder to force blank line after included text --><!-- endInclude -->
843+
831844
## Group by File Properties
832845
833846
### File Path

docs/Queries/Sorting.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ However, any `sort by` instructions in queries take precedence over these defaul
5050
> To sort the results of a query differently from the default, you must add at least one `sort by` line to the query. The sort instructions you supply will take priority over the appended defaults.
5151
>
5252
> Adding `sort by` lines to the [[Global Query]] provides a way override to the default sort order for **all** searches (except those that [[Global Query#Ignoring the global query|ignore the global query]]).
53+
>
54+
> You may also find `sort by function task.lineNumber` to be useful to override the default sort order. See [[#Override the Tasks plugin's default sort order]] below.
5355
5456
## Custom Sorting
5557

@@ -504,6 +506,26 @@ sort by function task.originalMarkdown
504506

505507
<!-- placeholder to force blank line after included text --><!-- endInclude -->
506508

509+
### Line Number
510+
511+
There is no built-in instruction to sort by the task's line number.
512+
513+
Since Tasks X.Y.Z, **[[Custom Sorting|custom sorting]] by the task's line number** is now possible, using `task.lineNumber`.
514+
515+
> [!tip]
516+
> With `task.lineNumber`, the first line in the file is on line number `0` (zero), not `1` (one).
517+
518+
<!-- placeholder to force blank line before included text --><!-- include: CustomSortingExamples.test.other_properties_task.lineNumber_docs.approved.md -->
519+
520+
```javascript
521+
sort by function task.lineNumber
522+
```
523+
524+
- Sort by the line number of the task's original line in the MarkDown file.
525+
- This is useful if you are unhappy with the [[Sorting#default sort order]].
526+
527+
<!-- placeholder to force blank line after included text --><!-- endInclude -->
528+
507529
### Random sorting
508530

509531
> [!released]
@@ -677,21 +699,46 @@ For example, when you `sort by done reverse` and your query results contain task
677699

678700
## Examples
679701

702+
### Sort tasks by due date, from oldest to newest
703+
680704
```tasks
681705
not done
682706
due today
683707
sort by due
684708
```
685709

710+
### Sort tasks by due date, from newest to oldest
711+
686712
```tasks
687713
done
688714
sort by done reverse
689715
```
690716

717+
### Override the Tasks plugin's default sort order
718+
719+
If you are unhappy with the [[#default sort order]], this is one way to override it:
720+
721+
1. sort by the Markdown file's full path,
722+
2. then sort by the task's line number in that file.
723+
724+
```tasks
725+
not done
726+
sort by path
727+
sort by function task.lineNumber
728+
```
729+
730+
Any other sort instructions can be added before these two, such as `sort by priority` or `sort by happens`
731+
732+
### Sort by multiple properties
733+
734+
1. task's status type (Sorted in the order `IN_PROGRESS`, `TODO`, `DONE`, `CANCELLED` then `NON_TASK`),
735+
2. then the task's description, in reverse alphabetical order
736+
3. then by the Markdown file's path.
737+
691738
```tasks
692739
not done
693740
due before next monday
694-
sort by status
741+
sort by status.type
695742
sort by description reverse
696743
sort by path
697744
```

docs/Quick Reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ This table summarizes the filters and other options available inside a `tasks` b
5151
| `description (includes, does not include) <string>`<br>`description (regex matches, regex does not match) /regex/i` | `sort by description` | | | `task.description`<br>`task.descriptionWithoutTags` |
5252
| `has tags`<br>`no tags`<br>`tag (includes, does not include) <tag>`<br>`tags (include, do not include) <tag>`<br>`tag (regex matches, regex does not match) /regex/i`<br>`tags (regex matches, regex does not match) /regex/i` | `sort by tag`<br>`sort by tag <tag_number>` | `group by tags` | `hide tags` | `task.tags` |
5353
| | | | `show tree` | |
54-
| | | | | `task.originalMarkdown` |
54+
| | | | | `task.originalMarkdown`<br>`task.lineNumber` |
5555
| | `sort by random` | | | |
5656
| **[[About Scripting\|Scripting]]** | | | | |
5757
| `filter by function` | `sort by function` | `group by function` | | |

docs/Scripting/Task Properties.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ For more information, including adding your own customised statuses, see [[Statu
148148
| `task.onCompletion` | `string` | `'delete'` | `string` | `''` |
149149
| `task.tags` | `string[]` | `['#todo', '#health']` | `any[]` | `[]` |
150150
| `task.originalMarkdown` | `string` | `' - [ ] Do exercises #todo #health 🆔 abcdef ⛔ 123456,abc123 🔼 🔁 every day when done 🏁 delete ➕ 2023-07-01 🛫 2023-07-02 ⏳ 2023-07-03 📅 2023-07-04 ❌ 2023-07-06 ✅ 2023-07-05 ^dcf64c'` | `string` | `'- [/] minimal task'` |
151+
| `task.lineNumber` | `number` | `17` | `number` | `0` |
151152

152153
<!-- placeholder to force blank line after included text --><!-- endInclude -->
153154

docs/What is New/Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ _In recent [Tasks releases](https://github.com/obsidian-tasks-group/obsidian-tas
1212

1313
## 7.x releases
1414

15+
- X.Y.Z:
16+
- Add `task.lineNumber`.
17+
- This enables `sort by function task.lineNumber` to override the [[Sorting#Default sort order|default sort order]].
18+
- See [[Filters#Line Number|filters]], [[Sorting#Line Number|sorting]] and [[Grouping#Line Number|grouping]].
1519
- 7.15.0:
1620
- Tasks now requires [Obsidian 1.4.0](https://obsidian.md/changelog/2023-08-31-desktop-v1.4.5/) or newer.
1721
- **Translations**:

resources/sample_vaults/Tasks-Demo/Manual Testing/Smoke Testing the Tasks Plugin.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Work through all the tasks below, until zero tasks remain in this query:
5050
> not done
5151
> path includes Smoke Testing the Tasks Plugin
5252
> description includes **check**:
53+
> sort by function task.lineNumber
5354
>
5455
> short display
5556
> ```
@@ -191,14 +192,15 @@ hide postpone button
191192

192193
- Non-task checkbox are tasks without the global filter, treated as `ListItems` by the obsidian-tasks plugin
193194
- [ ] #task Check or uncheck non-task list items with checkbox in the query below this task
194-
- [ ] I will have an `x` status
195-
- [x] I will have a `space` status
195+
- [ ] I will have an `x` status
196+
- [x] I will have a `space` status
196197
- [/] Me too with a `space` status
197198

198-
```tasks
199+
```tasks
199200
filename includes {{query.file.filename}}
200201
heading includes Toggling non-task checkboxes
201-
show tree
202+
description does not include **check**:
203+
show tree
202204
```
203205

204206
- [ ] #task **check**: Checked all above steps for **toggling non-task list items with checkbox in a query** worked
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!-- placeholder to force blank line before included text -->
2+
3+
4+
5+
<!-- placeholder to force blank line after included text -->

tests/Scripting/ScriptingReference/CustomFiltering/CustomFilteringExamples.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,14 @@ describe('other properties', () => {
674674
[],
675675
SampleTasks.withRepresentativeTags(),
676676
],
677+
678+
[
679+
'task.lineNumber',
680+
// comment to force line break
681+
[],
682+
SampleTasks.withRepresentativeLineNumbers(),
683+
],
684+
677685
[
678686
'task.urgency',
679687
[
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!-- placeholder to force blank line before included text -->
2+
3+
4+
5+
<!-- placeholder to force blank line after included text -->

0 commit comments

Comments
 (0)