mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-06-25 04:59:11 +00:00
Add bake preview_link task and bin/preview-link for ad-hoc link previews
Builds a link post via Pressa::LinkPost, builds the site for http://mudge:8000 (blog-server.service serves www/ straight off disk), prints the preview URL, then deletes the local post file again. No git pull/commit/push at all -- used by the mudge dashboard's new 'Publish Beta' button to preview a post without committing anything.
This commit is contained in:
parent
281272aa32
commit
1b86eda064
2 changed files with 82 additions and 0 deletions
43
bake.rb
43
bake.rb
|
|
@ -101,6 +101,49 @@ def new_link
|
|||
puts post.target_path
|
||||
end
|
||||
|
||||
# Build a link post and preview it on the mudge blog server without touching
|
||||
# git: writes posts/YYYY/MM/slug.md, builds for http://mudge:8000 (the same
|
||||
# target blog-server.service serves straight out of www/), prints the
|
||||
# preview URL, then deletes the local file. Drives bin/preview-link.
|
||||
def preview_link
|
||||
payload =
|
||||
begin
|
||||
JSON.parse($stdin.read)
|
||||
rescue JSON::ParserError => e
|
||||
abort "Error: invalid JSON payload on stdin: #{e.message}"
|
||||
end
|
||||
|
||||
author = payload["author"] || Pressa::Config::SimpleToml.load_file("site.toml")["author"]
|
||||
post =
|
||||
begin
|
||||
Pressa::LinkPost.build(
|
||||
title: payload["title"],
|
||||
link: payload["link"],
|
||||
body: payload["body"],
|
||||
tags: payload["tags"],
|
||||
author:
|
||||
)
|
||||
rescue Pressa::LinkPost::Error => e
|
||||
abort "Error: #{e.message}"
|
||||
end
|
||||
|
||||
abort "Error: post already exists at #{post.target_path}" if File.exist?(post.target_path)
|
||||
|
||||
FileUtils.mkdir_p(File.dirname(post.target_path))
|
||||
File.write(post.target_path, post.content)
|
||||
|
||||
year_month = post.target_path[%r{^posts/(\d{4}/\d{2})/}, 1]
|
||||
slug = File.basename(post.filename, ".md")
|
||||
|
||||
begin
|
||||
mudge
|
||||
ensure
|
||||
FileUtils.rm_f(post.target_path)
|
||||
end
|
||||
|
||||
puts "http://mudge:8000/posts/#{year_month}/#{slug}/"
|
||||
end
|
||||
|
||||
# Create a new draft in public/drafts/.
|
||||
# @parameter title_parts [Array] Optional title words; defaults to Untitled.
|
||||
def new_draft(*title_parts)
|
||||
|
|
|
|||
39
bin/preview-link
Executable file
39
bin/preview-link
Executable file
|
|
@ -0,0 +1,39 @@
|
|||
#!/usr/bin/env bash
|
||||
#
|
||||
# Build a link post from a JSON payload on stdin and preview it on the mudge
|
||||
# blog server (http://mudge:8000, served by blog-server.service straight out
|
||||
# of www/) without touching git at all: no pull, no commit, no push. The post
|
||||
# file is deleted from posts/ again right after the preview build, so this
|
||||
# never leaves a trace in the repo or in git history.
|
||||
#
|
||||
# ssh mudge '$HOME/samhuri.net/bin/preview-link' <<'JSON'
|
||||
# {"title": "Magical Wristband", "link": "https://example.net/x",
|
||||
# "body": "Optional commentary.", "tags": "gear, tech"}
|
||||
# JSON
|
||||
#
|
||||
# Prints the preview URL on stdout, progress on stderr. Used by the
|
||||
# dashboard's "Publish Beta" button.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Non-interactive SSH sessions get a bare PATH; make sure rbenv is reachable.
|
||||
export PATH="$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH"
|
||||
if command -v rbenv >/dev/null 2>&1; then
|
||||
eval "$(rbenv init - bash)"
|
||||
fi
|
||||
|
||||
# Run from the repo root regardless of where the script lives.
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
REPO="${SAMHURI_REPO:-$(cd "$SCRIPT_DIR/.." && pwd)}"
|
||||
cd "$REPO"
|
||||
|
||||
payload="$(cat)"
|
||||
if [ -z "${payload//[[:space:]]/}" ]; then
|
||||
echo "Error: empty payload on stdin" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "==> Building preview" >&2
|
||||
# `bake mudge` logs its own build progress to stdout (not stderr), so only the
|
||||
# final line -- the preview URL `preview_link` puts last -- is what we want.
|
||||
printf '%s' "$payload" | bundle exec bake preview_link | tee /dev/stderr | tail -n 1
|
||||
Loading…
Reference in a new issue