Skip to content
Open
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ Create a file named "on-modify.obsidian-sync" in folder "~/.task/hooks" with con
#!/bin/bash
read -r OLD
read -r NEW
~/folder-you-cloned/obsidian-taskwarrior-sync/mtt_taskwarrior_to_md.sh --task "$NEW"
# escape double quotes
NEW_ESCAPED=$(echo "$NEW" | sed 's/"/\\"/g')
~/folder-you-cloned/obsidian-taskwarrior-sync/mtt_md_import.sh --task $NEW_ESCAPED
echo "$NEW"
```

Expand Down
6 changes: 3 additions & 3 deletions mtt_md_add_uuids.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ done
# First pass: build mapping of short IDs to UUIDs
echo "First pass: Building mapping of short IDs to UUIDs..."
echo "Searching for short IDs with pattern: \\[id:: [a-z0-9]{6}\\]"
debug_echo "Running: rg --no-heading --line-number --with-filename \"\\[id:: [a-z0-9]{6}\\]\" $file_pattern"
rg --no-heading --line-number --with-filename "\\[id:: [a-z0-9]{6}\\]" $file_pattern | while IFS=: read -r file line_number line; do
debug_echo "Running: rg --no-heading --line-number --with-filename \"\\[id:: [a-z0-9]{6}\\]\" --glob $file_pattern"
rg --no-heading --line-number --with-filename "\\[id:: [a-z0-9]{6}\\]" --glob $file_pattern | while IFS=: read -r file line_number line; do
echo "Found line with short ID: $line"
# Extract the short ID using regex
if [[ $line =~ \[id::\ ([a-z0-9]{6})\] ]]; then
Expand Down Expand Up @@ -128,7 +128,7 @@ fi
rm -f /tmp/id_mappings.tmp

# Fourth pass: Add UUIDs to tasks without any ID
rg --no-heading --line-number --with-filename "^- \\[ \\] " $file_pattern | while IFS=: read -r file line_number line; do
rg --no-heading --line-number --with-filename "^- \\[ \\] " --glob $file_pattern | while IFS=: read -r file line_number line; do
echo "......................................"
echo "scanning file $file"
echo "scanning line $line"
Expand Down
2 changes: 1 addition & 1 deletion mtt_sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fi

# Build command arguments for mtt_md_add_uuids.sh (only needs mask)
uuid_args=()
[ -n "$file_pattern" ] && uuid_args+=(--mask "$file_pattern")
[ -n "$file_pattern" ] && uuid_args+=(--mask "\"$file_pattern\"")

# Build command arguments for mtt_md_to_taskwarrior.sh (needs both mask and project)
export_args=()
Expand Down
12 changes: 7 additions & 5 deletions mtt_taskwarrior_to_md.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ show_help() {
echo " --debug Display debug information including sed commands"
echo
echo "Sample call:"
echo "mtt_taskwarrior_to_md --task \"{\"id\":0,\"description\":\"feed the cat\",\"end\":\"20250328T213759Z\",\"entry\"::\"20250328T102249Z\",\"modified\":\"20250328T213759Z\",\"project\":\"paymetrics\",\"status\":\"completed\",\"uuid\":\"eb48e204-e8be-416b-857d-8154edbbd7ad\",\"annotations\":[{\"entry\":\"20250328T213742Z\",\"description\":\"Source: \/Users\/nbossard\/PilotageDistri\/business-server\/documentation\/Agenda\/2025-03-28.md\"}],\"tags\":[\"Nicolas\"],\"urgency\":4.4}\""
echo "mtt_md_import --task \"{\\\"id\":0,\\\"description\\\":\\\"feed the cat\\\",\\\"end\\\":\\\"20250328T213759Z\\\",\\\"entry\\\"::\\\"20250328T102249Z\\\",\\\"modified\\\":\\\"20250328T213759Z\\\",\\\"project\\\":\\\"paymetrics\\\",\\\"status\\\":\\\"completed\\\",\\\"uuid\\\":\\\"eb48e204-e8be-416b-857d-8154edbbd7ad\\\",\\\"annotations\\\":[{\\\"entry\\\":\\\"20250328T213742Z\\\",\\\"description\\\":\\\"Source: \\\/Users\\\/nbossard\\\/PilotageDistri\\\/business-server\\\/documentation\\\/Agenda\\\/2025-03-28.md\\\"}],\\\"tags\\\":[\\\"Nicolas\\\"],\\\"urgency\\\":4.4}\""
exit 0
}

Expand Down Expand Up @@ -66,7 +66,7 @@ convert_priority() {


# Parse command line arguments
task_json=""
param_task_json=""
debug=false
while [[ "$#" -gt 0 ]]; do
case $1 in
Expand All @@ -75,7 +75,7 @@ while [[ "$#" -gt 0 ]]; do
--task)
shift
if [[ -n "$1" ]]; then
task_json="$1"
param_task_json="$1"
else
echo "mtt - Error: --task requires JSON data"
show_help
Expand All @@ -86,12 +86,14 @@ while [[ "$#" -gt 0 ]]; do
shift
done

if [ -z "$task_json" ]; then
if [ -z "$param_task_json" ]; then
echo "mtt - Error: --task parameter is required"
show_help
exit $EXIT_MISSING_ARGS
fi

# unescape double quotes
task_json=$(echo "${param_task_json}" | sed 's/\\"/"/g')
echo "mtt - task to be imported is : $task_json"
echo "mtt - removing escaping"
task_json=$(echo "$task_json" | sed 's/\\"/\"/g')
Expand Down Expand Up @@ -166,7 +168,7 @@ updated_task_line+="$formatted_tags"
escaped_task_line=$(printf '%s\n' "$updated_task_line" | sed 's/[|&]/\\&/g')

# Use a different delimiter for sed (| instead of /) to avoid issues with slashes
sed_command="sed -i.bak -E 's|^- \[ \].*\[id:: $uuid\].*$|${escaped_task_line}|' \"$source_file\""
sed_command="sed -i.bak -E \"s|^- \[ \].*\[id:: $uuid\].*$|${escaped_task_line}|\" \"$source_file\""
if [ "$debug" = true ]; then
echo "mtt - Debug: Executing sed command:"
echo "mtt - $sed_command"
Expand Down
3 changes: 3 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ test:
$(MAKE) -C test_taskwarrior_to_md_3_with_dependency $@
$(MAKE) -C test_taskwarrior_to_md_4_deleted $@
$(MAKE) -C test_taskwarrior_to_md_5_with_priority $@
$(MAKE) -C test_taskwarrior_to_md_6_desc_with_quote $@
$(MAKE) -C test_add_uuids_1 $@
$(MAKE) -C test_add_uuids_2_multiple_tasks $@
$(MAKE) -C test_add_uuids_3_multiple_files $@
Expand All @@ -28,3 +29,5 @@ clean:
$(MAKE) -C test_taskwarrior_to_md_3_with_dependency $@
$(MAKE) -C test_taskwarrior_to_md_4_deleted $@
$(MAKE) -C test_taskwarrior_to_md_5_with_priority $@
$(MAKE) -C test_taskwarrior_to_md_6_desc_with_quote $@

20 changes: 20 additions & 0 deletions tests/test5_import_desc_with_quote/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
modified_markdown_file.md: existing_markdown_file.md \
Makefile \
task_to_be_imported.json
cp ./existing_markdown_file.md modified_markdown_file.md
../../mtt_md_import.sh --task '$(shell cat task_to_be_imported.json)'

clean:
rm -f modified_markdown_file.md

test: modified_markdown_file.md
$(call .cecho,"Comparing files with cmp and diff")
@if cmp -s expected_markdown_file.md modified_markdown_file.md; then \
echo "✅ TEST PASSED : Files are identical"; \
else \
echo "❌ Files are different:"; \
DIFF_OUTPUT=$$(diff -u expected_markdown_file.md modified_markdown_file.md); \
echo "$$DIFF_OUTPUT"; \
exit 1; \
fi

5 changes: 5 additions & 0 deletions tests/test5_import_desc_with_quote/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# README

This is test of a task modification in taskwarrior being synced back to original file.

**But the description contains a quote, which should be supported.**
5 changes: 5 additions & 0 deletions tests/test5_import_desc_with_quote/existing_markdown_file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# bla bla

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

- [ ] feed the cat l'obèse [id:: 16a1d4bb-fec6-416b-be70-6df438a6f44e]
5 changes: 5 additions & 0 deletions tests/test5_import_desc_with_quote/expected_markdown_file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# bla bla

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

- [x] feed the cat l 'obèse [id:: 16a1d4bb-fec6-416b-be70-6df438a6f44e]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"description":"feed the cat l'obèse","entry":"20250406T090430Z","modified":"20250406T091641Z","status":"deleted","uuid":"16a1d4bb-fec6-416b-be70-6df438a6f44e","annotations":[{"entry":"20250406T090430Z","description":"Source: ./modified_markdown_file.md"}]}