-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate_note.sh
More file actions
executable file
·72 lines (60 loc) · 2.3 KB
/
create_note.sh
File metadata and controls
executable file
·72 lines (60 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
# Usage:
# create_note.sh create a new note named by DATE_UUID in uncategorized/
# create_note.sh "a post title" create a new note with a slugified, date-prefixed filename in uncategorized/
# create_note.sh -h | --help show this help
set -euo pipefail
# set strict mode
# -e means exit when error, do not proceed
# -u means referring to unset (undefined) variables will error out
# -o pipefail means use the exit code of the failure inside the pipeline instead of the last command
# Figure out script directory
# going through the trouble so that directory path is not hardcoded
# because the location of this repo changes on different devices
NOTES_DIR="$(cd "$(dirname "$(realpath "$0")")" && pwd)"
# "$0" - the path the script was invoked as
# realpath "$0" - resolves $0 o a canonical absolute path (follows symlinks)
# dirname "$(realpath "$0")" - strips the filename, leaving just the directory
# cd "$(dirname ...)" && pwd — cd into that directory, then pwd prints the current working directory
# Show usage
if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
sed -n '3,6p' "$0" | sed 's/^# \{0,1\}//'
exit 0
fi
# Default target for new notes
TARGET_DIR="${NOTES_DIR}/uncategorized"
mkdir -p "$TARGET_DIR"
DATE_UUID=$(date '+%Y-%m-%d-%H%M%S') # 2026-04-14-171053
DATE_STRING=$(date '+%Y-%m-%dT%H:%M:%S%:z') # 2023-11-19T11:55:40+02:00
TITLE="$*" # $* treats all args as one string "$1 $2 $3"
SLUG=$(echo "$TITLE" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | tr ' ' '-' | tr '[:upper:]' '[:lower:]')
# Slug explained:
# echo ' this is a post ' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | tr " " "-" # this-is-a-post
# use sed to trim space from beginning and end
# use tr to replace spaces with -
# use tr to make the slug lowercase
if [ -n "$SLUG" ]; then
# if filename is provided, use that prefixed with date
FILENAME="${DATE_UUID}-${SLUG}.md"
else
# if no filename provided, use the UUID as the filename
FILENAME="${DATE_UUID}.md"
fi
FILEPATH="${TARGET_DIR}/${FILENAME}"
if [ -e "$FILEPATH" ]; then
echo "File already exists: $FILEPATH" >&2
exit 1
fi
cat > "$FILEPATH" <<EOF
---
title: ${TITLE}
date: ${DATE_STRING}
uuid: ${DATE_UUID}
slug: ${SLUG}
draft: true
description:
tags:
---
EOF
# echo "$FILEPATH"
code "$FILEPATH"