Skip to content

Instantly share code, notes, and snippets.

View marcoroth's full-sized avatar
🚀
Shipping

Marco Roth marcoroth

🚀
Shipping
View GitHub Profile
@KonnorRogers
KonnorRogers / dynamic-replace.js
Last active September 18, 2023 15:13
Adding "$" before code blocks that isn't selectable by a user
var code = document.querySelector(":is(.language-bash, .language-shell, .language-zsh, .language-sh, .language-console).highlighter-rouge pre.highlight > code")
code.innerHTML = code.innerHTML.split("\n").map((str) => {
return str.replace(/^(\w)/, "<span class='highlight-command-line-start'>$</span>$1")
}).join("\n")
@mattiaz9
mattiaz9 / blurhashDataURL.ts
Last active July 19, 2024 05:10
Convert blurhash to a base64 DataURL string (no canvas or node-canvas)
import { decode } from "blurhash"
export function blurHashToDataURL(hash: string | undefined): string | undefined {
if (!hash) return undefined
const pixels = decode(hash, 32, 32)
const dataURL = parsePixels(pixels, 32, 32)
return dataURL
}
import { Controller } from "stimulus";
import { get } from "@rails/request.js";
import { PageSnapshot } from "@hotwired/turbo";
export default class extends Controller {
static values = { hoverTime: Number };
connect() {
this.element.addEventListener("mouseover", this.prefetch.bind(this));
this.element.addEventListener("touchstart", this.prefetch.bind(this));
}
URI.open(url) do |uri|
base_uri = uri.base_uri
listable_type = case base_uri.to_s
when /soundcloud.com\/(?<artist>.+)\/tracks/
raise UnparsableTrackUrlError
when /soundcloud.com\/(?<artist>.+)\/sets\/(?<title>.+)/
SoundCloudPlaylist
when /soundcloud.com\/(?<artist>.+)\/(?<title>.+)/
SoundCloudTrack
@jaredcwhite
jaredcwhite / cable_car_example.html
Last active June 9, 2021 17:31
CableCar + web component
<cable-car href="/cc/cable_car" csrf-token="94IkWdvagUHWPGVkD+VeBFVjGoM0zXAf6tgm6BPU+kEpOj2BTtI6bXJQzEDXGAF+R6Dc1ek7oNouatTMrbUy">
<button
onclick="this.closest('cable-car').post(this, {extra: 123})"
data-x="1"
output="#show-the-things"
>
Do a thing!
</button>
<section id="show-the-things"></section>
@KonnorRogers
KonnorRogers / struct.rb
Last active June 17, 2021 14:53
Using hashes for structs
# using a predefined hash
hash = { field1: "foo", field2: "bar" }
HashStruct = Struct.new(*hash.keys, keyword_init: true)
hash_struct = HashStruct.new(hash)
hash_struct.field1 # => "foo"
hash_struct.field2 # => "bar"
hash_struct.field3 # => ERROR!
@leastbad
leastbad / README.md
Last active November 11, 2022 17:27
stimulus-youtube preview

My goal with this was to wrap the terrible YouTube Embed API in a Stimulus controller that would allow me to access the underlying API while providing some convenience methods. One key outcome is that the controller emits youtube events which contain the current position in the video. This means that other code can now respond to the position you are at in the video.

<div data-controller="youtube" data-youtube-code-value="Lo_1pyQ7xvc">
  <button data-action="youtube#play">Play</button>
  <button data-action="youtube#pause">Pause</button>
  <button data-action="youtube#stop">Stop</button>
  <br>
  <div data-youtube-target="frame"></div>
@leastbad
leastbad / application_controller.rb
Created March 25, 2021 16:05
Notifications are fun (Devise, Bootstrap 5, Notyf, FontAwesome)
class ApplicationController < ActionController::Base
include CableReady::Broadcaster
include Toastable
end
@KonnorRogers
KonnorRogers / geocode_job.rb
Last active April 16, 2022 10:51
async geocoding with Ruby-Geocoder
# app/jobs/geocode_job.rb
class GeocodeJob < ApplicationJob
def perform(model)
model.geocode
end
end