Skip to content

Commit 8c890aa

Browse files
dwertheimerclaude
andcommitted
Priority markers follow NotePlan's rule: leading !, !!, !!! or >> followed by a space
- New helpers/taskPriority.js (no imports): the single definition of what counts as a priority marker, plus get/remove/set/next-level helpers. - paragraph.js: getTaskPriority, removeTaskPriorityIndicators, changePriority and cyclePriorityStateUp/Down use it. changePriority no longer strips every '!' in the line (which turned '!>date' due dates into '>date' scheduled dates and mangled 'Wow!' and images). Cycling down from none now goes to '>>'. - sorting.js: getNumericPriority no longer counts '!!!!', '!x' (no space) or a leading due date ('!>2026-12-05 ...') as a priority; '>>' needs a space too. - HTMLView.js: preview priority styling uses the same rule. - TaskAutomations: drop the '!!!!' (p4) option, which isn't a NotePlan priority; '__p4__' now writes '>>'. Closes #796 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent efea374 commit 8c890aa

8 files changed

Lines changed: 243 additions & 45 deletions

File tree

‎dwertheimer.TaskAutomations/src/NPTaskScanAndProcess.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ export function getSingleTaskOptions(origPara: TParagraph, todayLines: any, cont
156156
{ label: `! Set priority to p1`, value: '__p1__' },
157157
{ label: `!! Set priority to p2`, value: '__p2__' },
158158
{ label: `!!! Set priority to p3`, value: '__p3__' },
159-
{ label: `!!!! Set priority to p4`, value: '__p4__' },
160159
{ label: `x! Remove priority from task (p0)`, value: '__p0__' },
161160
]
162161
if (NotePlan.environment.platform === 'macOS') {
@@ -287,7 +286,7 @@ export function handlePriorityAction(origPara: TParagraph, userChoice: string):
287286
__p1__: '!',
288287
__p2__: '!!',
289288
__p3__: '!!!',
290-
__p4__: '!!!!',
289+
__p4__: '>>', // '!!!!' isn't a NotePlan priority; NotePlan's level 4 is '>>'. Kept as an alias for any saved choice.
291290
'__>>__': '>>',
292291
}
293292
const prioritySymbol = priorityMap[userChoice] || ''

‎helpers/HTMLView.js‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getStoredWindowRect, getWindowFromCustomId, isHTMLWindowOpen, storeWind
1313
import { generateCSSFromTheme, RGBColourConvert } from '@helpers/NPThemeToCSS'
1414
import { isTermInEventLinkHiddenPart, isTermInNotelinkOrURI, isTermInMarkdownPath } from '@helpers/paragraph'
1515
import { RE_EVENT_LINK, RE_SYNC_MARKER } from '@helpers/regex'
16+
import { getPriorityLevel } from '@helpers/taskPriority'
1617
import { getTimeBlockString, isTimeBlockLine } from '@helpers/timeblocks'
1718
import { usersVersionHas } from '@helpers/NPVersions'
1819

@@ -192,17 +193,14 @@ function taskStateFromBox(box: string, marker: string): string {
192193
}
193194

194195
/**
195-
* Leading `!` / `!!` / `!!!` / `>>` on the task text. These are the only supported priority markers.
196+
* Leading `!` / `!!` / `!!!` / `>>` on the task text, followed by a space. These are the only supported priority markers
197+
* (see helpers/taskPriority.js).
196198
* @param {string} text
197199
* @returns {string} `priority1`..`priority4`, or ''
198200
*/
199201
function priorityClassFromTaskText(text: string): string {
200-
const match = text.match(/^(!!!|!!|!|>>)(?=\s|$)/)
201-
if (!match) return ''
202-
if (match[1] === '>>') return 'priority4'
203-
if (match[1] === '!!!') return 'priority3'
204-
if (match[1] === '!!') return 'priority2'
205-
return 'priority1'
202+
const level = getPriorityLevel(text)
203+
return level > 0 ? `priority${String(level)}` : ''
206204
}
207205

208206
/**

‎helpers/__tests__/paragraph.test.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,3 +947,41 @@ describe('paragraph.js', () => {
947947
})
948948
})
949949
})
950+
951+
describe('priority markers (NotePlan: leading !, !!, !!! or >> followed by a space)', () => {
952+
const para = (content: string): any => ({ content, note: null })
953+
describe('getTaskPriority()', () => {
954+
test('reads only a leading marker followed by a space', () => {
955+
expect(p.getTaskPriority('!! x')).toBe(2)
956+
expect(p.getTaskPriority('>> x')).toBe(4)
957+
expect(p.getTaskPriority('x !!')).toBe(0)
958+
expect(p.getTaskPriority('Pay rent !>2026-12-05')).toBe(0)
959+
expect(p.getTaskPriority('!>2026-12-05 Pay rent')).toBe(0)
960+
})
961+
})
962+
describe('changePriority()', () => {
963+
test('keeps due dates, images and other ! in the text', () => {
964+
expect(p.changePriority(para('Pay rent !>2026-12-05'), '!!')).toBe('!! Pay rent !>2026-12-05')
965+
expect(p.changePriority(para('Wow! Great ![img](a.png)'), '!')).toBe('! Wow! Great ![img](a.png)')
966+
})
967+
test('replaces or removes an existing marker', () => {
968+
expect(p.changePriority(para('! x'), '>>')).toBe('>> x')
969+
expect(p.changePriority(para('>> x'), '')).toBe('x')
970+
})
971+
test("treats a marker that isn't NotePlan's (e.g. '!!!!') as no priority", () => {
972+
expect(p.changePriority(para('! x'), '!!!!')).toBe('x')
973+
})
974+
})
975+
describe('cyclePriorityStateUp() / cyclePriorityStateDown()', () => {
976+
test('up keeps a due date', () => {
977+
expect(p.cyclePriorityStateUp(para('Pay rent !>2026-12-05'))).toBe('! Pay rent !>2026-12-05')
978+
})
979+
test('down from none goes to >>', () => {
980+
expect(p.cyclePriorityStateDown(para('x'))).toBe('>> x')
981+
})
982+
test('round trip', () => {
983+
expect(p.cyclePriorityStateUp(para('>> x'))).toBe('x')
984+
expect(p.cyclePriorityStateDown(para('! x'))).toBe('x')
985+
})
986+
})
987+
})

‎helpers/__tests__/sorting.test.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,19 @@ describe('sorting.js', () => {
630630
expect(result).toEqual(defaultPriority)
631631
})
632632

633+
test.each([
634+
['!>2026-12-05 due date at the start', 0],
635+
['!2026-12-05 due date at the start', 0],
636+
['!x no space after the marker', 0],
637+
['!!!! four is not a NotePlan marker', 0],
638+
['>>x no space after the marker', 0],
639+
['!! x', 2],
640+
['! Pay rent !>2026-12-05', 1],
641+
])('NotePlan priority rules: %j -> %i', (content, expected) => {
642+
const paragraph = new Paragraph({ type: 'open', content, filename: 'testFile.md' })
643+
expect(s.getNumericPriority(s.getSortableTask(paragraph))).toEqual(expected)
644+
})
645+
633646
test('should return -1 for unknown priority', () => {
634647
const paragraph = new Paragraph({ type: 'open', content: 'test content ??', filename: 'testFile.md' })
635648
const result = s.getNumericPriority(s.getSortableTask(paragraph))
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* globals describe, expect, test */
2+
import { getPriorityLevel, getPriorityMarker, nextPriorityLevel, removePriorityMarker, setPriorityLevel } from '../taskPriority'
3+
4+
describe('taskPriority', () => {
5+
describe('getPriorityLevel()', () => {
6+
test.each([
7+
['! task', 1],
8+
['!! task', 2],
9+
['!!! task', 3],
10+
['>> task', 4],
11+
[' !! indented task', 2],
12+
['!task (no space)', 0],
13+
['>>task (no space)', 0],
14+
['!!!! four is not a NotePlan marker', 0],
15+
['task !! (not at the start)', 0],
16+
['task >> (not at the start)', 0],
17+
['!2026-12-05 due date first', 0],
18+
['!>2026-12-05 due date first', 0],
19+
['!>2026-W49 due week first', 0],
20+
['Pay rent !>2026-12-05', 0],
21+
['! Pay rent !>2026-12-05', 1],
22+
['![image](a.png) first', 0],
23+
['Wow! Great', 0],
24+
['', 0],
25+
['!', 0],
26+
])('%j -> %i', (content, expected) => {
27+
expect(getPriorityLevel(content)).toBe(expected)
28+
})
29+
})
30+
31+
test('getPriorityMarker() returns the marker itself', () => {
32+
expect(getPriorityMarker('!!! x')).toBe('!!!')
33+
expect(getPriorityMarker('>> x')).toBe('>>')
34+
expect(getPriorityMarker('x')).toBe('')
35+
})
36+
37+
describe('removePriorityMarker()', () => {
38+
test('removes only the leading marker and its space', () => {
39+
expect(removePriorityMarker('!! Pay rent !>2026-12-05')).toBe('Pay rent !>2026-12-05')
40+
expect(removePriorityMarker('>> Wow! Great ![img](a.png)')).toBe('Wow! Great ![img](a.png)')
41+
})
42+
test('leaves text without a NotePlan marker unchanged', () => {
43+
expect(removePriorityMarker('!>2026-12-05 Pay rent')).toBe('!>2026-12-05 Pay rent')
44+
expect(removePriorityMarker('!!!! four')).toBe('!!!! four')
45+
expect(removePriorityMarker('Call Bob !!')).toBe('Call Bob !!')
46+
})
47+
test('keeps leading whitespace', () => {
48+
expect(removePriorityMarker(' ! x')).toBe(' x')
49+
})
50+
})
51+
52+
describe('setPriorityLevel()', () => {
53+
test('adds, replaces and removes the leading marker', () => {
54+
expect(setPriorityLevel('Pay rent', 2)).toBe('!! Pay rent')
55+
expect(setPriorityLevel('! Pay rent', 4)).toBe('>> Pay rent')
56+
expect(setPriorityLevel('>> Pay rent', 0)).toBe('Pay rent')
57+
})
58+
test('leaves due dates, images and other ! alone', () => {
59+
expect(setPriorityLevel('Pay rent !>2026-12-05', 1)).toBe('! Pay rent !>2026-12-05')
60+
expect(setPriorityLevel('!>2026-12-05 Pay rent', 3)).toBe('!!! !>2026-12-05 Pay rent')
61+
expect(setPriorityLevel('Wow! Great ![img](a.png)', 1)).toBe('! Wow! Great ![img](a.png)')
62+
})
63+
test('out-of-range levels mean no priority', () => {
64+
expect(setPriorityLevel('!! x', 7)).toBe('x')
65+
expect(setPriorityLevel('!! x', -1)).toBe('x')
66+
})
67+
})
68+
69+
describe('nextPriorityLevel()', () => {
70+
test('up: none -> ! -> !! -> !!! -> >> -> none', () => {
71+
expect([0, 1, 2, 3, 4].map((l) => nextPriorityLevel(l, 'up'))).toEqual([1, 2, 3, 4, 0])
72+
})
73+
test('down: none -> >> -> !!! -> !! -> ! -> none', () => {
74+
expect([0, 1, 2, 3, 4].map((l) => nextPriorityLevel(l, 'down'))).toEqual([4, 0, 1, 2, 3])
75+
})
76+
})
77+
})

‎helpers/paragraph.js‎

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from '@helpers/regex'
1919
import { getLineMainContentPos } from '@helpers/search'
2020
import { getElementsFromTask } from '@helpers/sorting'
21+
import { getPriorityLevel, nextPriorityLevel, PRIORITY_MARKERS, removePriorityMarker, setPriorityLevel } from '@helpers/taskPriority'
2122
import { stripBlockIDsFromString, stripDateRefsFromString, stripAllTagssFromString, stripLinksFromString } from '@helpers/stringTransforms'
2223
import { parseTeamspaceFilename } from '@helpers/teamspace'
2324

@@ -899,59 +900,47 @@ export function removeDuplicateSyncedLines(paras: $ReadOnlyArray<TParagraph>): $
899900
}
900901

901902
/**
902-
* Get number of consecutive '!' in 'content' that aren't at the start/end of a word, or preceding a '['
903-
* From 3.9.4 there are also `>>` working-on markers at the start of 'content', which are treated as priority 4.
903+
* The task's priority level, as NotePlan reads it: a leading '!', '!!', '!!!' or '>>' followed by a space.
904+
* (Before 2026-12 this counted any run of '!' anywhere in the line, including the '!' of a due date like '!>2026-12-05'.)
904905
* @param {string} content
905-
* @returns {string} number of !, or 4 if line is flagged as 'working-on', or -1
906+
* @returns {number} 1-3 for '!' to '!!!', 4 for '>>' (working on), or 0 for none
906907
*/
907908
export function getTaskPriority(content: string): number {
908-
if (content.match(/\B\!+\B(?!\[)/)) {
909-
// not in middle of word, or starting an image tag
910-
// Note: this second (unanchored) match is guaranteed to succeed if the test above did, but it can find an earlier
911-
// run of '!'s than the one the test matched, so keep it as a separate match rather than re-using the test's result.
912-
const exclamationMatch = content.match(/\B\!+\B/)
913-
if (exclamationMatch) {
914-
return exclamationMatch[0].length
915-
}
916-
}
917-
if (content.match(/^>>/)) {
918-
return 4
919-
}
920-
return 0
909+
return getPriorityLevel(content)
921910
}
922911

923912
/**
924-
* Remove task Priority Indicators (!, !!, !!!, >>) from start of content (was: any where in line except starting an image tag, or at start/end of a word).
913+
* Remove the task's priority marker ('!', '!!', '!!!' or '>>' plus a space, at the start of the content).
925914
* @param {string} content
926-
* @returns {string} content minus any priority indicators
915+
* @returns {string} content minus its priority marker
927916
*/
928917
export function removeTaskPriorityIndicators(content: string): string {
929-
// let output = content.replace(/\B\!+\B(?!\[)/g, '') // not in middle of word, or starting an image tag
930-
let output = content.replace(/^!{1,3}\s/, '') // start of line only
931-
output = output.replace(/^>>\s?/, '') // start of line only
932-
return output
918+
return removePriorityMarker(content)
933919
}
934920

935921
/**
936-
* Change the priority in a task to '!', '!!', '!!!', '>>' (or remove priority)
922+
* Change the priority in a task to '!', '!!', '!!!', '>>' (or remove priority).
923+
* Only the leading marker is replaced: any other '!' in the text (e.g. a due date '!>2026-12-05', an image '![..](..)',
924+
* or 'Wow!') is left alone. (Before 2026-12 every '!' in the line was removed.)
937925
* @author @dwertheimer updated by @jgclark
938926
* @param {TParagraph} input - the task/pagraph to be processed
939-
* @param {string} priorityString - the new priority (!,!!,!!! or '' for none)
927+
* @param {string} priorityString - the new priority ('!', '!!', '!!!', '>>', or '' for none)
940928
* @param {boolean} - commit the change after the change is made (default false)
941929
* @returns {string} the resulting updated paragraph's content
942930
* Note: If the third param is missing or false, THE CHANGE HAS NOT BEEN COMMITTED YET. You should use note.updateParagraph(s) to commit the change you receive back.
943931
* Note: Ideally lives in NPParagraph.js, but putting it here avoids a circular dependency
944932
*/
945933
export function changePriority(inputPara: TParagraph, prioStr: string, commitChange?: boolean = false): string {
946934
const outputPara = inputPara
947-
outputPara.content = outputPara.content.replace(/!\s*/g, '').replace(/\s+!/g, '').replace(/^>>\s/, '')
948-
outputPara.content = `${prioStr ? `${prioStr} ` : ''}${outputPara.content}`.trim()
935+
const level = PRIORITY_MARKERS.indexOf(prioStr ?? '')
936+
if (level === -1) {
937+
logWarn('changePriority', `'${prioStr}' isn't a NotePlan priority marker; removing the priority instead`)
938+
}
939+
outputPara.content = setPriorityLevel(outputPara.content, Math.max(level, 0))
949940
commitChange && outputPara.note ? outputPara.note.updateParagraph(outputPara) : null
950941
return outputPara.content
951942
}
952943

953-
const PRIORITY_LEVELS = ['', '!', '!!', '!!!', '>>']
954-
955944
/**
956945
* Cycle the priority level of a task up: none -> ! -> !! -> !!! -> >> -> none
957946
* Written originally to suit a single UI window in the Dashboard.
@@ -960,22 +949,21 @@ const PRIORITY_LEVELS = ['', '!', '!!', '!!!', '>>']
960949
* @returns {string} the resulting updated paragraph's content
961950
*/
962951
export function cyclePriorityStateUp(input: TParagraph): string {
963-
const currentPriorityLevel = getTaskPriority(input.content)
964-
const newPriorityLevel = (currentPriorityLevel + 1) % 5
965-
return changePriority(input, PRIORITY_LEVELS[newPriorityLevel], true)
952+
const newPriorityLevel = nextPriorityLevel(getPriorityLevel(input.content), 'up')
953+
return changePriority(input, PRIORITY_MARKERS[newPriorityLevel], true)
966954
}
967955

968956
/**
969957
* Cycle the priority level of a task down: none -> >> -> !!! -> !! -> ! -> none
958+
* (Before 2026-12, down from none stayed at none.)
970959
* Written for the Dashboard.
971960
* @author @jgclark
972961
* @param {TParagraph} input - the task/pagraph to be processed
973962
* @returns {string} the resulting updated paragraph's content
974963
*/
975964
export function cyclePriorityStateDown(input: TParagraph): string {
976-
const currentPriorityLevel = getTaskPriority(input.content)
977-
const newPriorityLevel = (currentPriorityLevel - 1) % 5
978-
return changePriority(input, PRIORITY_LEVELS[newPriorityLevel], true)
965+
const newPriorityLevel = nextPriorityLevel(getPriorityLevel(input.content), 'down')
966+
return changePriority(input, PRIORITY_MARKERS[newPriorityLevel], true)
979967
}
980968

981969
export type TagsList = { hashtags: Array<string>, mentions: Array<string> } //include the @ and # characters

‎helpers/sorting.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import get from 'lodash/get'
44
import { isScheduled } from './dateTime'
55
import { clo, logDebug, logError } from './dev'
6+
import { getPriorityMarker } from './taskPriority'
67

78
// Note: an object type, not an `interface`, so it can be spread (`{ ...t, duration: 0 }`).
89
// Kept INEXACT (trailing `...`) because .flowconfig sets exact_by_default=true and callers
@@ -74,7 +75,9 @@ const RE_HASHTAGS: RegExp = /\B(?:#|#)((?![\p{N}\-_\/]+(?:$|[^\p{L}\p{M}\p{N}\
7475
// Mentions follow the same rule. NotePlan reports "@estimate(2)" as the mention "@estimate", so the
7576
// trailing "(...)" is left out here too -- which also keeps @estimate(2) and @estimate(5) in one sort group.
7677
const RE_MENTIONS: RegExp = /\B@((?![\p{N}\-_\/]+(?:$|[^\p{L}\p{M}\p{N}\p{S}\-_\/]))[\p{L}\p{M}\p{N}\p{S}\-_\/]{0,59}[\p{L}\p{M}\p{N}\p{S}\-_])/gu
77-
const RE_LEADING_EXCLAMATIONS: RegExp = /^\s*(!+)/g // at start of content, though allowing for leading whitespace (as NP does)
78+
// A NotePlan priority marker: '!', '!!' or '!!!' at the start of the content (leading whitespace allowed, as NP does), followed
79+
// by a space. So '!!!! x', '!x' and due dates like '!>2026-12-05' at the start are not priorities. See helpers/taskPriority.js.
80+
const RE_LEADING_EXCLAMATIONS: RegExp = /^\s*(!{1,3})\s/g
7881
const RE_LEADING_PARENS_PRIORITY: RegExp = /^\s*\(([a-zA-z])\)\B/g // must be at start of content
7982
export const TASK_TYPES: Array<string> = ['open', 'scheduled', 'done', 'cancelled', 'checklist', 'checklistDone', 'checklistCancelled', 'checklistScheduled']
8083
export const isTask = (para: TParagraph): boolean => TASK_TYPES.indexOf(para.type) >= 0
@@ -191,6 +194,7 @@ export function getElementsFromTask(content: string, reSearch: RegExp): Array<st
191194
* @author @dwertheimer extended by @jgclark
192195
* @param {SortableParagraphSubset} item
193196
* @returns {number} priority from 3, 2, 1, 4 for >>, 0 for not priority markers, or -1 (for error or empty item)
197+
* Note: only NotePlan's markers count: '!', '!!', '!!!' or '>>' at the start of the content, followed by a space.
194198
* Note: -1 is a special value in Dashboard:
195199
* - Sentinel value: indicates "no priority set" or "no items found"
196200
* - Initialization: default for max priority calculations
@@ -207,7 +211,8 @@ export function getNumericPriority(item: SortableParagraphSubset): number {
207211
} else if (item.parensPriority[0]) {
208212
prio = item.parensPriority[0].charCodeAt(0) - 'A'.charCodeAt(0) + 1
209213
if (prio === 23) prio = 4
210-
} else if (item.content.startsWith('>>')) {
214+
} else if (getPriorityMarker(item.content) === '>>') {
215+
// '>>' (working on) only counts at the start and followed by a space, like the '!' markers
211216
prio = 4
212217
}
213218
return prio

0 commit comments

Comments
 (0)