diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 08c8e0e..1fb1149 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -36,6 +36,27 @@ jobs: - name: ${{ matrix.name }} run: ${{ matrix.command }} + ci-shell: + runs-on: ubuntu-24.04 + strategy: + fail-fast: false + matrix: + include: + - task: shfmt + name: "Run shfmt" + command: "shfmt -d ." + - task: shellcheck + name: "Run shellcheck" + command: "shellcheck **/*.sh" + name: Run ${{ matrix.task }} + steps: + - name: Checkout the repository + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + - name: Setup tools + uses: jdx/mise-action@13abe502c30c1559a5c37dff303831bab82c9402 # v2.2.3 + - name: ${{ matrix.name }} + run: ${{ matrix.command }} + build: runs-on: ubuntu-24.04 strategy: @@ -64,7 +85,6 @@ jobs: uses: jdx/mise-action@13abe502c30c1559a5c37dff303831bab82c9402 # v2.2.3 - name: Install dependencies run: bun install --frozen-lockfile - - name: Fetch docs assets if: ${{ matrix.kind == 'ja-JP' }} run: bun run fetch-docs-ja-jp @@ -73,45 +93,7 @@ jobs: shell: bash run: | set -euxo pipefail - - # Prepare docs.json - curl -L https://github.com/typst-community/dev-builds/releases/download/docs-${{ matrix.tag }}/docs.json \ - -o public/docs.json - sd '/DOCS-BASE/' '${{ matrix.base }}' public/docs.json - - # Prepare docs assets - curl -LO https://github.com/typst-community/dev-builds/releases/download/docs-${{ matrix.tag }}/docs-assets.zip - unzip docs-assets.zip - mv assets public/assets - - # Prepare favicon - curl -L https://github.com/typst-community/org/raw/main/design/typst-community.icon.png \ - -o public/favicon.png - - # Configure metadata - TAG=${{ matrix.tag }} - # Strip the leading ā€˜v’ or convert to a valid format - ${{ startsWith(matrix.tag, 'v') && 'VERSION=${TAG#v}' || 'VERSION="0.dev.${TAG}"' }} - cat << EOF > public/metadata.json - { - "\$schema": "../metadata.schema.json", - "language": "en-US", - "version": "$VERSION", - "typstOfficialUrl": "https://typst.app", - "typstOfficialDocsUrl": "https://typst.app/docs/", - "githubOrganizationUrl": "https://github.com/typst-community", - "socialLinks": [ - { "url": "https://github.com/typst-community/typst-docs-web" }, - { - "title": "Discord (Typst)", - "url": "https://discord.gg/2uDybryKPe" - } - ], - "originUrl": "https://example.com/", - "basePath": "${{ matrix.base }}", - "displayTranslationStatus": false - } - EOF + bash scripts/fetch-docs-assets.sh --tag "${{ matrix.tag }}" --base "${{ matrix.base }}" --dest public - name: Run Vite build run: bun run build diff --git a/mise.toml b/mise.toml index 13a2449..3076066 100644 --- a/mise.toml +++ b/mise.toml @@ -5,3 +5,5 @@ node = "22.11.0" bun = "1.2.21" pinact = "3.1.1" sd = "1.0.0" +shfmt = "3.12.0" +shellcheck = "0.11.0" diff --git a/netlify.toml b/netlify.toml index 5816a23..d74d894 100644 --- a/netlify.toml +++ b/netlify.toml @@ -1,3 +1,3 @@ [build] -command = "bash netlify-build.sh" +command = "bash scripts/netlify-build.sh" publish = "_site" diff --git a/scripts/fetch-docs-assets.sh b/scripts/fetch-docs-assets.sh new file mode 100644 index 0000000..d466b63 --- /dev/null +++ b/scripts/fetch-docs-assets.sh @@ -0,0 +1,152 @@ +#!/usr/bin/env bash +# Usage: fetch-docs-assets.sh [--tag TAG] [--base BASE] [--dest DEST] +# This script downloads docs.json and assets from the typst-community/dev-builds GitHub release +# and writes `public/docs.json`, `public/assets`, `public/favicon.png`, and `public/metadata.json`. + +set -euo pipefail + +print_usage() { + cat <<'EOF' +Usage: fetch-docs-assets.sh [--tag TAG] [--base BASE] [--dest DEST] [--origin ORIGIN] + +Environment/args: + --tag TAG (required) Release tag to download, e.g. v0.14.0 or latest + --base BASE (required) Base path used by docs, e.g. / or /docs/ + --dest DEST (optional) Destination directory to write files (default: public) + --origin ORIGIN (optional) Origin URL for the deployed site, without base path (default: https://example.com/) + +This script performs the following: + - Downloads docs.json from the release tag + - Replaces '/DOCS-BASE/' placeholder in docs.json with the provided base + - Downloads docs-assets.zip and extracts it to destination/assets + - Downloads favicon and writes to destination/favicon.png + - Generates destination/metadata.json with basic deployment metadata +EOF +} + +# Defaults +ORG="typst-community" +DEST="public" +ORIGIN="https://example.com/" +TAG="" +BASE="" + +# Parse args +while [[ $# -gt 0 ]]; do + case "$1" in + --tag) + TAG="$2" + shift 2 + ;; + --base) + BASE="$2" + shift 2 + ;; + --dest) + DEST="$2" + shift 2 + ;; + --origin) + ORIGIN="$2" + shift 2 + ;; + --help | -h) + print_usage + exit 0 + ;; + *) + echo "Unknown option: $1" >&2 + print_usage + exit 2 + ;; + esac +done + +if [[ -z "$TAG" || -z "$BASE" ]]; then + echo "--tag and --base are required" >&2 + print_usage + exit 2 +fi + +mkdir -p "$DEST" + +# Compute VERSION like the previous CI did +if [[ "$TAG" == v* ]]; then + VERSION="${TAG#v}" +else + VERSION="0.dev.${TAG}" +fi + +RELEASE_BASE_URL="https://github.com/${ORG}/dev-builds/releases/download/docs-${TAG}" + +# Download docs.json +DOCS_URL="${RELEASE_BASE_URL}/docs.json" +echo "Fetching docs from ${DOCS_URL}" +if ! curl -sSfL "$DOCS_URL" -o "${DEST}/docs.json"; then + echo "Failed to download docs.json from ${DOCS_URL}" >&2 + exit 3 +fi + +# Replace placeholder '/DOCS-BASE/' with provided base in docs.json +# Use `sd` if available, else fallback to sed +if command -v sd >/dev/null 2>&1; then + sd '/DOCS-BASE/' "$BASE" "${DEST}/docs.json" +else + # Use portable sed: escape slashes + ESCAPED_BASE=$(printf '%s' "$BASE" | sed 's|/|\\/|g') + sed -i "s/\/DOCS-BASE\//${ESCAPED_BASE}/g" "${DEST}/docs.json" +fi + +# Download assets and extract +ASSETS_URL="${RELEASE_BASE_URL}/docs-assets.zip" +ASSETS_ZIP="docs-assets.zip" +if curl -sSfL "$ASSETS_URL" -o "$ASSETS_ZIP"; then + echo "Extracting ${ASSETS_ZIP} to ${DEST}/assets" + # Clean up existing assets if present + rm -rf "${DEST}/assets" + unzip -q "$ASSETS_ZIP" + if [[ -d assets ]]; then + mv assets "${DEST}/assets" + else + echo "Downloaded zip did not contain assets/ folder" >&2 + # keep build going; not necessarily fatal + fi + rm -f "$ASSETS_ZIP" +else + echo "No assets ZIP was found at ${ASSETS_URL} (continuing without error)" >&2 +fi + +# Download favicon +FAVICON_URL="https://github.com/${ORG}/org/raw/main/design/typst-community.icon.png" +if curl -sSfL "$FAVICON_URL" -o "${DEST}/favicon.png"; then + echo "Wrote favicon to ${DEST}/favicon.png" +else + echo "Failed to download favicon from ${FAVICON_URL} (continuing without error)" >&2 +fi + +# Write metadata.json +cat >"${DEST}/metadata.json" < _site/index.html +cat <_site/index.html @@ -54,6 +54,7 @@ cat << EOF > _site/index.html

Build a website from the documentation JSON file generated by typst-docs.

This website is for developing typst-docs-web. Its contents might be changed at anytime.