aboutsummaryrefslogtreecommitdiff
path: root/content/snippets
diff options
context:
space:
mode:
Diffstat (limited to 'content/snippets')
-rw-r--r--content/snippets/11ty-fetch-put-request.md50
-rw-r--r--content/snippets/algolia-copy-between-apps.md27
-rw-r--r--content/snippets/batch-compress-images-magick.md13
-rw-r--r--content/snippets/batch-rename-files.md11
-rw-r--r--content/snippets/browse-update-algolia-index.md53
-rw-r--r--content/snippets/jq-convert-jsonl-json.md15
-rw-r--r--content/snippets/jq-count-json-items.md9
-rw-r--r--content/snippets/loop-files-rename.md13
-rw-r--r--content/snippets/rip-edit-audio.md33
-rw-r--r--content/snippets/tailwind-descendent-has.md19
10 files changed, 243 insertions, 0 deletions
diff --git a/content/snippets/11ty-fetch-put-request.md b/content/snippets/11ty-fetch-put-request.md
new file mode 100644
index 0000000..ac0a9e8
--- /dev/null
+++ b/content/snippets/11ty-fetch-put-request.md
@@ -0,0 +1,50 @@
+---
+title: Make a PUT request with @11ty/eleventy-fetch
+description: Make a PUT request with @11ty/eleventy-fetch
+tags: ["11ty"]
+---
+
+[11ty](https://www.11ty.dev/) fetch lets you cache resources for any remote asset at build time. It scored it in a cache in your project.
+
+The basic request format supports `GET` only:
+
+```js
+const EleventyFetch = require("@11ty/eleventy-fetch");
+
+module.exports = async function () {
+ let url = "https://api.github.com/repos/11ty/eleventy";
+
+ /* This returns a promise */
+ return EleventyFetch(url, {
+ duration: "1d", // save for 1 day
+ type: "json", // we’ll parse JSON for you
+ });
+};
+```
+
+You can update it to change it to a `PUT` request (there were no examples I could find of this anywhere). Here i'm making a PUT request to retrieve my username from [Hardcover](https://hardcover.app/) using a GraphQL query.
+
+```js
+const url = "https://api.hardcover.app/v1/graphql";
+
+return EleventyFetch(url, {
+ duration: "1d",
+ type: "json",
+ fetchOptions: {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ authorization: "<BEARER_TOKEN>",
+ },
+ body: JSON.stringify({
+ query: `{
+ me {
+ username
+ }
+ } `,
+ }),
+ },
+});
+```
+
+Running this at build time in `_data/username.json` will store the asset in a global `username` object.
diff --git a/content/snippets/algolia-copy-between-apps.md b/content/snippets/algolia-copy-between-apps.md
new file mode 100644
index 0000000..8bd80d8
--- /dev/null
+++ b/content/snippets/algolia-copy-between-apps.md
@@ -0,0 +1,27 @@
+---
+title: Copy indices between Algolia apps w/ CLI
+description: Copy indices between Algolia apps w/ CLI
+tags: ["algolia"]
+---
+
+Ensure the [Algolia CLI](https://www.algolia.com/doc/tools/cli/get-started/overview/#install-the-algolia-cli) is installed
+
+```console
+algolia profile add
+```
+
+Add both your profiles, e.g `account1`, `account`
+
+```console
+algolia objects browse INDEX_NAME > records.json --profile account1
+```
+
+Pull the records locally.
+
+Then upload to second app and index (ensure you change the `--profile` flag ).
+
+The `-F` (short for `--file`) flag in Algolia CLI lets you read from a JSON file.
+
+```console
+algolia objects import INDEX_2_NAME -F records.json --profile account2
+```
diff --git a/content/snippets/batch-compress-images-magick.md b/content/snippets/batch-compress-images-magick.md
new file mode 100644
index 0000000..e484390
--- /dev/null
+++ b/content/snippets/batch-compress-images-magick.md
@@ -0,0 +1,13 @@
+---
+title: Batch compress image using ImageMagick
+description: Batch compress image using ImageMagick
+tags: ["commandline"]
+---
+
+This command loops a directory (`cd` into it, i'm assuming you're in it) and for every `.jpg` it aims to compress it to 200kb (not guaranteed)
+
+```console
+for file in *.jpg; do magick "$file" -define jpeg:extent=200kb "compressed/$file" done;
+```
+
+Make a `compressed` directory first, of course.
diff --git a/content/snippets/batch-rename-files.md b/content/snippets/batch-rename-files.md
new file mode 100644
index 0000000..1413d11
--- /dev/null
+++ b/content/snippets/batch-rename-files.md
@@ -0,0 +1,11 @@
+---
+title: Batch rename files using rename
+description: Batch rename files using rename
+tags: ["commandline"]
+---
+
+This appends .json to each file that doesn't already have a .json extension.
+
+```console
+rename 's/$/.json/' *
+```
diff --git a/content/snippets/browse-update-algolia-index.md b/content/snippets/browse-update-algolia-index.md
new file mode 100644
index 0000000..6dbe91e
--- /dev/null
+++ b/content/snippets/browse-update-algolia-index.md
@@ -0,0 +1,53 @@
+---
+title: Browse Algolia index and update objects (JS)
+description: Browse Algolia index and update objects (JS)
+tags: ["algolia"]
+---
+
+```js
+const algolia = require("algoliasearch");
+const client = algolia("<YOUR-APP-ID>", "<YOUR-WRITE-API-KEY>");
+
+const index = client.initIndex("<YOUR-INDEX-NAME>");
+
+function transformFunction(hit) {
+ return {
+ ...hit,
+ foo: "bar",
+ };
+}
+
+async function transformObjects(batch, isDryRun) {
+ const newArr = batch.map((el) => {
+ return transformFunction(el);
+ });
+
+ // Dry run
+ if (isDryRun) {
+ console.log(newArr, "no objects were updated, this is a dry run");
+ }
+
+ // Real run
+ else {
+ try {
+ const data = await index.saveObjects(newArr);
+ const { objectIDs } = data;
+ console.log(
+ "The following objectIDs were successfully updated:",
+ objectIDs
+ );
+ } catch (err) {
+ console.log("an error occurred updating the records", err);
+ }
+ }
+}
+
+index
+ .browseObjects({
+ query: "",
+ batch: (batch) => {
+ transformObjects(batch, false);
+ },
+ })
+ .then(() => console.log("code completed"));
+```
diff --git a/content/snippets/jq-convert-jsonl-json.md b/content/snippets/jq-convert-jsonl-json.md
new file mode 100644
index 0000000..b58a021
--- /dev/null
+++ b/content/snippets/jq-convert-jsonl-json.md
@@ -0,0 +1,15 @@
+---
+title: Convert JSONL to JSON using JQ
+description: Convert JSONL to JSON using JQ
+tags: ["jq", "commandline"]
+---
+
+```console
+jq -s '.' input.jsonl > output.json
+```
+
+And the other way:
+
+```console
+jq -c '.[]' input.json > output.jsonl
+```
diff --git a/content/snippets/jq-count-json-items.md b/content/snippets/jq-count-json-items.md
new file mode 100644
index 0000000..1093bfb
--- /dev/null
+++ b/content/snippets/jq-count-json-items.md
@@ -0,0 +1,9 @@
+---
+title: Count items in JSON file using jq
+description: Count items in JSON file using jq
+tags: ["jq", "commandline"]
+---
+
+```console
+jq -s '. | length' sample_data.json
+```
diff --git a/content/snippets/loop-files-rename.md b/content/snippets/loop-files-rename.md
new file mode 100644
index 0000000..27a3134
--- /dev/null
+++ b/content/snippets/loop-files-rename.md
@@ -0,0 +1,13 @@
+---
+title: Batch rename files using file index
+description: Batch rename files using file index
+tags: ["commandline"]
+---
+
+This terminal command renames all .jpg files in a directory to `{name}{index}.jpg`:
+
+```console
+i=1; for f in *.jpg; do mv -- "$f" "name-$i.jpg"; i=$((i+1)); done
+```
+
+`name` is just a placeholder, replace with whatever
diff --git a/content/snippets/rip-edit-audio.md b/content/snippets/rip-edit-audio.md
new file mode 100644
index 0000000..0cbb35b
--- /dev/null
+++ b/content/snippets/rip-edit-audio.md
@@ -0,0 +1,33 @@
+---
+title: Ripping and cutting music sets
+description: Ripping and cutting music sets
+tags: ["commandline"]
+---
+
+Here's a simple process to rip audio, add metadata and cut the audio from the command line:
+
+```console
+yt-dlp --extract--audio --audio-quality 0 "https://youtu.be/vbkyazLGovM?si=SQXs6PHIcBrsr5to"
+```
+
+This rips a 3 hour set locally. Use `mv` if you want to rename the file. Let's call the file `set.m4a`.
+
+Adding metadata using `exiftool`:
+
+```console
+exiftool -Title="Big set" -Artist="Wicked skengman" set.m4a
+```
+
+It's a three hour set and we only want from 2:30:00 until the end, so only the last 30 minutes, for this we can use `ffmpeg`.
+
+```console
+ffmpeg -ss 2:30:00 -i set.m4a -c copy output.m4a
+```
+
+`copy` copies the file without re-encoding.
+
+If using `cmus` then run the following to update your library with the new file:
+
+```console
+:add ~/Music/
+```
diff --git a/content/snippets/tailwind-descendent-has.md b/content/snippets/tailwind-descendent-has.md
new file mode 100644
index 0000000..5134ac9
--- /dev/null
+++ b/content/snippets/tailwind-descendent-has.md
@@ -0,0 +1,19 @@
+---
+title: Apply a tailwind class based on descendent state
+description: Apply a tailwind class based on descendent state
+tags: ["tailwind"]
+---
+
+You can apply css classes to parent elements if any of the parent's descendents meet a certain condition using the `has()` selector.
+
+Extending this to Tailwind is easy. A use case I came against recently was this: we have a header with a class that's passed dynamically using React state. The class is `scroll-locked`, used to indicate that the mobile menu has been toggled and that whole app should be scroll locked, i.e `overflow: hidden`.
+
+Instead of prop drilling and passing state from the `<header>` to the `<body>`, which is where we want to scroll lock, we can use the `has()` selector on the `<body>` to apply a class only when the `<header className="scroll-locked">`.
+
+The css for this is:
+
+```jsx
+<body className="has-[header.scroll-locked]:truncate">
+```
+
+`has-*` is a Tailwind modifier and what we pass in the [square brackets] is just css selector. `:` is used as a separator between different parts of a utility class name.