Skip to content
Draft
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ a text editor. The short domain must use Cloudflare as its authoritative DNS pro

Follow the [documentation](https://vanityurls.link/en/docs/quickstart) to spin your vanityURLs' instance.

## Remote link submission (optional)

GitLab-hosted instances can add short links from a browser at `/add`, without a local checkout: submitting a URL there
picks a collision-free random slug (or a self-chosen one), returns it immediately, and triggers a GitLab pipeline that
runs `v8s-lnk` to append the link, commit, and push. It's opt-in (`links.remote_submission.enabled` in site config, off
by default) and the page should be placed behind its own Cloudflare Access Application, since it grants write access to
the repository. See `defaults/gitlab/.gitlab-ci.yml` for the required secrets and CI/CD variables.

## Contributions

[Contributions](.github/CONTRIBUTING.md) are welcome! We recognize
Expand Down
37 changes: 37 additions & 0 deletions defaults/gitlab/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# vanityURLs remote link submission (/add)
#
# This job is only ever started by the Worker's /add/submit handler via the GitLab
# Pipeline Trigger API, which passes SUBMIT_URL and SUBMIT_SLUG as trigger variables.
# The workflow rule below stops a pipeline from being created at all for an ordinary
# push — including the push this job itself makes — since without it GitLab still spins
# up an empty pipeline for that commit (a job-level `rules:` only skips the job, not the
# pipeline shell).
#
# Setup:
# - A Pipeline Trigger Token, stored as the Cloudflare Worker secret GITLAB_TRIGGER_TOKEN.
# - A Project Access Token with `write_repository` scope, allowed to push to the default
# branch, stored as a masked CI/CD variable named GIT_PUSH_TOKEN. Only mark it "protected"
# too if the default branch is itself a protected branch — otherwise a protected variable
# is withheld from pipelines running on it and the push step fails with an empty token.
# - Optionally, a V8S_LINKS_OWNER CI/CD variable to attribute these commits to your usual
# owner tag (defaults to "bhd" otherwise, same as running v8s-lnk locally without it set).
workflow:
rules:
- if: "$SUBMIT_URL && $SUBMIT_SLUG"

add-link:
image: node:24
rules:
- if: "$SUBMIT_URL && $SUBMIT_SLUG"
script:
- npm ci
- git config user.email "vanityurls-bot@${CI_SERVER_HOST}"
- git config user.name "vanityURLs bot"
- git remote set-url origin "https://oauth2:${GIT_PUSH_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"
# GitLab Runner checks out a detached commit, not a branch. v8s-lnk's own `git push` (run
# below) expects a normal tracked branch, so set that up first: `push.default current` lets
# a plain `git push` push the current branch to a same-named remote branch with no upstream
# tracking metadata required.
- git checkout -B "$CI_COMMIT_REF_NAME"
- git config push.default current
- node scripts/v8s-lnk "$SUBMIT_URL" "$SUBMIT_SLUG"
67 changes: 67 additions & 0 deletions defaults/public/add/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Add a short link | VanityURLs</title>
<meta name="description" content="Submit a URL to create a new VanityURLs short link" />
<meta name="robots" content="noindex,nofollow" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
<link rel="stylesheet" href="/v8s-style.css?v=20260601" />
<link rel="stylesheet" href="/v8s-submit.css?v=20260601" />
</head>
<body>
<main class="page-shell">
<section class="search-home" aria-labelledby="brand-title">
<a class="wordmark" href="/" aria-label="VanityURLs">
<h1 id="brand-title"><span>Vanity</span><span>URLs</span></h1>
</a>

<form class="redirect-form" id="submitForm">
<label class="sr-only" for="submitUrl">URL to shorten</label>
<div class="search-box">
<svg class="search-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
<path d="M10.6 13.4a3.5 3.5 0 0 0 5 0l3.3-3.3a3.5 3.5 0 0 0-5-5l-1.6 1.6" />
<path d="M13.4 10.6a3.5 3.5 0 0 0-5 0l-3.3 3.3a3.5 3.5 0 0 0 5 5l1.6-1.6" />
</svg>
<input
id="submitUrl"
name="url"
type="url"
autocomplete="off"
autocapitalize="none"
spellcheck="false"
inputmode="url"
placeholder="Paste a long URL to shorten"
required
/>
<button type="submit" aria-label="Create short link">
<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
<path d="M5 12h12M13 6l6 6-6 6" />
</svg>
</button>
</div>

<label class="sr-only" for="submitSlug">Custom short code (optional)</label>
<div class="search-box search-box--slug">
<span class="search-box-prefix" aria-hidden="true">/</span>
<input
id="submitSlug"
name="slug"
type="text"
autocomplete="off"
autocapitalize="none"
spellcheck="false"
maxlength="80"
placeholder="custom short code (optional)"
/>
</div>
</form>

<div class="submit-result" id="submitResult" aria-live="polite"></div>
</section>
</main>
<script src="/v8s-submit.js?v=20260601" defer></script>
</body>
</html>
94 changes: 94 additions & 0 deletions defaults/public/v8s-submit.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
.search-box--slug {
min-height: 46px;
margin-top: 12px;
padding-left: 18px;
}

.search-box-prefix {
flex: 0 0 auto;
color: var(--subtle);
font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.95rem;
}

.search-box--slug input {
font-size: 0.95rem;
}

.submit-result {
display: none;
width: min(100%, 620px);
border: 1px solid var(--border);
border-radius: 8px;
background: var(--panel);
padding: 18px 20px;
box-shadow: var(--shadow);
text-align: left;
}

.submit-result.is-visible {
display: block;
}

.submit-result.is-error {
border-color: var(--danger, #c0392b);
}

.submit-label {
margin: 0 0 8px;
color: var(--muted);
font-size: 0.82rem;
font-weight: 700;
letter-spacing: 0;
text-transform: uppercase;
}

.submit-shortcode {
display: flex;
align-items: center;
gap: 10px;
margin: 0;
}

.submit-shortcode a {
overflow-wrap: anywhere;
color: var(--brand);
font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 1.1rem;
font-weight: 650;
text-decoration: underline;
text-decoration-thickness: 1px;
text-underline-offset: 3px;
}

.submit-copy {
border: 1px solid var(--border);
border-radius: 6px;
background: transparent;
color: var(--text);
font-size: 0.78rem;
padding: 4px 10px;
cursor: pointer;
}

.submit-copy:hover {
background: var(--panel);
}

.submit-meta {
margin: 12px 0 0;
color: var(--muted);
font-size: 0.9rem;
line-height: 1.55;
}

.submit-meta a {
color: var(--brand);
}

.submit-message {
margin: 0;
color: var(--text);
font-size: 0.95rem;
line-height: 1.55;
}
165 changes: 165 additions & 0 deletions defaults/public/v8s-submit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
(function submitBrowserCode() {
// Only en/fr are translated so far, unlike v8s-lookup.js's full en/fr/es/it/de set — es/it/de
// instances fall back to English below. Translation contributions welcome.
const SUBMIT_LABELS = {
en: {
empty: "Paste a URL to shorten",
invalid: "Enter a valid http(s) URL",
invalidSlug: "Use only letters, numbers, and - . _ ~",
error: "Unable to create the short link",
pending: "Creating your short link…",
shortcode: "Your new short link",
track: "Track the pipeline",
copy: "Copy",
copied: "Copied!",
eta: (minutes) => `Typically live within ~${minutes} minute${minutes === 1 ? "" : "s"}.`
},
fr: {
empty: "Collez une URL à raccourcir",
invalid: "Entrez une URL http(s) valide",
invalidSlug: "Utilisez seulement des lettres, chiffres et - . _ ~",
error: "Impossible de créer le lien court",
pending: "Création du lien court…",
shortcode: "Votre nouveau lien court",
track: "Suivre le pipeline",
copy: "Copier",
copied: "Copié!",
eta: (minutes) => `Généralement en ligne d'ici ~${minutes} minute${minutes === 1 ? "" : "s"}.`
}
};

const submitLanguage = String(document.documentElement.lang || "en")
.toLowerCase()
.split("-")[0];
const LABELS = SUBMIT_LABELS[submitLanguage] || SUBMIT_LABELS.en;

document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById("submitForm");
const input = document.getElementById("submitUrl");
const slugInput = document.getElementById("submitSlug");
const result = document.getElementById("submitResult");

if (!form || !input || !result) return;

form.addEventListener("submit", async (event) => {
event.preventDefault();
await submitUrl(form, input, slugInput, result);
});
});

async function submitUrl(form, input, slugInput, result) {
const url = input.value.trim();

if (!url) {
renderMessage(result, LABELS.empty, true);
return;
}

if (!isHttpUrl(url)) {
renderMessage(result, LABELS.invalid, true);
return;
}

const slug = normalizeSlugInput(slugInput?.value);

if (slug && !isValidSlugFormat(slug)) {
renderMessage(result, LABELS.invalidSlug, true);
return;
}

const submitButton = form.querySelector("button[type=submit]");
submitButton?.setAttribute("disabled", "disabled");
renderMessage(result, LABELS.pending, false);

try {
const response = await fetch("/add/submit", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(slug ? { url, slug } : { url }),
cache: "no-store"
});

const body = await response.json().catch(() => ({}));

if (!response.ok || !body.slug) {
renderMessage(result, body.error || LABELS.error, true);
return;
}

renderShortcode(result, body);
form.reset();
} catch {
renderMessage(result, LABELS.error, true);
} finally {
submitButton?.removeAttribute("disabled");
}
}

function isHttpUrl(value) {
try {
const parsed = new URL(value);
return parsed.protocol === "http:" || parsed.protocol === "https:";
} catch {
return false;
}
}

function normalizeSlugInput(value) {
return String(value || "")
.trim()
.replace(/^\/+/, "")
.replace(/\/+$/, "");
}

function isValidSlugFormat(slug) {
if (slug.includes("//") || slug.includes("?") || slug.includes("#") || slug.includes("|")) return false;
return slug.split("/").every((part) => /^[A-Za-z0-9][A-Za-z0-9._~-]*$/.test(part));
}

function renderShortcode(result, { slug, pipelineUrl, etaMinutes }) {
const shortLink = `${window.location.origin}/${slug}`;

result.classList.remove("is-error");
result.classList.add("is-visible");
result.innerHTML = `
<p class="submit-label">${escapeHtml(LABELS.shortcode)}</p>
<p class="submit-shortcode">
<a href="${escapeAttr(shortLink)}" target="_blank" rel="noreferrer">${escapeHtml(shortLink)}</a>
<button type="button" class="submit-copy" data-copy="${escapeAttr(shortLink)}">${escapeHtml(LABELS.copy)}</button>
</p>
<p class="submit-meta">
${escapeHtml(etaMinutes ? LABELS.eta(etaMinutes) : "")}
${pipelineUrl ? `<a href="${escapeAttr(pipelineUrl)}" target="_blank" rel="noreferrer">${escapeHtml(LABELS.track)}</a>` : ""}
</p>
`;

const copyButton = result.querySelector(".submit-copy");
copyButton?.addEventListener("click", () => {
navigator.clipboard?.writeText(copyButton.dataset.copy || "").then(() => {
copyButton.textContent = LABELS.copied;
setTimeout(() => {
copyButton.textContent = LABELS.copy;
}, 2000);
});
});
}

function renderMessage(result, message, isError) {
result.classList.toggle("is-error", Boolean(isError));
result.classList.add("is-visible");
result.innerHTML = `<p class="submit-message">${escapeHtml(message)}</p>`;
}

function escapeHtml(value) {
return String(value ?? "")
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll('"', "&quot;")
.replaceAll("'", "&#039;");
}

function escapeAttr(value) {
return escapeHtml(value);
}
})();
7 changes: 7 additions & 0 deletions defaults/v8s-site-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
"tag_random_slug_lengths": {
"training": 4,
"debug": 2
},
"remote_submission": {
"enabled": false,
"gitlab_project": "",
"gitlab_ref": "main",
"gitlab_api_base": "https://gitlab.com/api/v4",
"eta_minutes": 2
}
},
"targets": {
Expand Down
Loading
Loading