Skip to content

Commit ba9a4d7

Browse files
chore: add docs assets download script (#44)
Co-authored-by: Y.D.X. <[email protected]>
1 parent 230699f commit ba9a4d7

File tree

5 files changed

+208
-93
lines changed

5 files changed

+208
-93
lines changed

.github/workflows/ci.yaml

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ jobs:
3636
- name: ${{ matrix.name }}
3737
run: ${{ matrix.command }}
3838

39+
ci-shell:
40+
runs-on: ubuntu-24.04
41+
strategy:
42+
fail-fast: false
43+
matrix:
44+
include:
45+
- task: shfmt
46+
name: "Run shfmt"
47+
command: "shfmt -d ."
48+
- task: shellcheck
49+
name: "Run shellcheck"
50+
command: "shellcheck **/*.sh"
51+
name: Run ${{ matrix.task }}
52+
steps:
53+
- name: Checkout the repository
54+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
55+
- name: Setup tools
56+
uses: jdx/mise-action@13abe502c30c1559a5c37dff303831bab82c9402 # v2.2.3
57+
- name: ${{ matrix.name }}
58+
run: ${{ matrix.command }}
59+
3960
build:
4061
runs-on: ubuntu-24.04
4162
strategy:
@@ -64,7 +85,6 @@ jobs:
6485
uses: jdx/mise-action@13abe502c30c1559a5c37dff303831bab82c9402 # v2.2.3
6586
- name: Install dependencies
6687
run: bun install --frozen-lockfile
67-
6888
- name: Fetch docs assets
6989
if: ${{ matrix.kind == 'ja-JP' }}
7090
run: bun run fetch-docs-ja-jp
@@ -73,45 +93,7 @@ jobs:
7393
shell: bash
7494
run: |
7595
set -euxo pipefail
76-
77-
# Prepare docs.json
78-
curl -L https://github.com/typst-community/dev-builds/releases/download/docs-${{ matrix.tag }}/docs.json \
79-
-o public/docs.json
80-
sd '/DOCS-BASE/' '${{ matrix.base }}' public/docs.json
81-
82-
# Prepare docs assets
83-
curl -LO https://github.com/typst-community/dev-builds/releases/download/docs-${{ matrix.tag }}/docs-assets.zip
84-
unzip docs-assets.zip
85-
mv assets public/assets
86-
87-
# Prepare favicon
88-
curl -L https://github.com/typst-community/org/raw/main/design/typst-community.icon.png \
89-
-o public/favicon.png
90-
91-
# Configure metadata
92-
TAG=${{ matrix.tag }}
93-
# Strip the leading ‘v’ or convert to a valid format
94-
${{ startsWith(matrix.tag, 'v') && 'VERSION=${TAG#v}' || 'VERSION="0.dev.${TAG}"' }}
95-
cat << EOF > public/metadata.json
96-
{
97-
"\$schema": "../metadata.schema.json",
98-
"language": "en-US",
99-
"version": "$VERSION",
100-
"typstOfficialUrl": "https://typst.app",
101-
"typstOfficialDocsUrl": "https://typst.app/docs/",
102-
"githubOrganizationUrl": "https://github.com/typst-community",
103-
"socialLinks": [
104-
{ "url": "https://github.com/typst-community/typst-docs-web" },
105-
{
106-
"title": "Discord (Typst)",
107-
"url": "https://discord.gg/2uDybryKPe"
108-
}
109-
],
110-
"originUrl": "https://example.com/",
111-
"basePath": "${{ matrix.base }}",
112-
"displayTranslationStatus": false
113-
}
114-
EOF
96+
bash scripts/fetch-docs-assets.sh --tag "${{ matrix.tag }}" --base "${{ matrix.base }}" --dest public
11597
11698
- name: Run Vite build
11799
run: bun run build

mise.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ node = "22.11.0"
55
bun = "1.2.21"
66
pinact = "3.1.1"
77
sd = "1.0.0"
8+
shfmt = "3.12.0"
9+
shellcheck = "0.11.0"

netlify.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[build]
2-
command = "bash netlify-build.sh"
2+
command = "bash scripts/netlify-build.sh"
33
publish = "_site"

scripts/fetch-docs-assets.sh

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/usr/bin/env bash
2+
# Usage: fetch-docs-assets.sh [--tag TAG] [--base BASE] [--dest DEST]
3+
# This script downloads docs.json and assets from the typst-community/dev-builds GitHub release
4+
# and writes `public/docs.json`, `public/assets`, `public/favicon.png`, and `public/metadata.json`.
5+
6+
set -euo pipefail
7+
8+
print_usage() {
9+
cat <<'EOF'
10+
Usage: fetch-docs-assets.sh [--tag TAG] [--base BASE] [--dest DEST] [--origin ORIGIN]
11+
12+
Environment/args:
13+
--tag TAG (required) Release tag to download, e.g. v0.14.0 or latest
14+
--base BASE (required) Base path used by docs, e.g. / or /docs/
15+
--dest DEST (optional) Destination directory to write files (default: public)
16+
--origin ORIGIN (optional) Origin URL for the deployed site, without base path (default: https://example.com/)
17+
18+
This script performs the following:
19+
- Downloads docs.json from the release tag
20+
- Replaces '/DOCS-BASE/' placeholder in docs.json with the provided base
21+
- Downloads docs-assets.zip and extracts it to destination/assets
22+
- Downloads favicon and writes to destination/favicon.png
23+
- Generates destination/metadata.json with basic deployment metadata
24+
EOF
25+
}
26+
27+
# Defaults
28+
ORG="typst-community"
29+
DEST="public"
30+
ORIGIN="https://example.com/"
31+
TAG=""
32+
BASE=""
33+
34+
# Parse args
35+
while [[ $# -gt 0 ]]; do
36+
case "$1" in
37+
--tag)
38+
TAG="$2"
39+
shift 2
40+
;;
41+
--base)
42+
BASE="$2"
43+
shift 2
44+
;;
45+
--dest)
46+
DEST="$2"
47+
shift 2
48+
;;
49+
--origin)
50+
ORIGIN="$2"
51+
shift 2
52+
;;
53+
--help | -h)
54+
print_usage
55+
exit 0
56+
;;
57+
*)
58+
echo "Unknown option: $1" >&2
59+
print_usage
60+
exit 2
61+
;;
62+
esac
63+
done
64+
65+
if [[ -z "$TAG" || -z "$BASE" ]]; then
66+
echo "--tag and --base are required" >&2
67+
print_usage
68+
exit 2
69+
fi
70+
71+
mkdir -p "$DEST"
72+
73+
# Compute VERSION like the previous CI did
74+
if [[ "$TAG" == v* ]]; then
75+
VERSION="${TAG#v}"
76+
else
77+
VERSION="0.dev.${TAG}"
78+
fi
79+
80+
RELEASE_BASE_URL="https://github.com/${ORG}/dev-builds/releases/download/docs-${TAG}"
81+
82+
# Download docs.json
83+
DOCS_URL="${RELEASE_BASE_URL}/docs.json"
84+
echo "Fetching docs from ${DOCS_URL}"
85+
if ! curl -sSfL "$DOCS_URL" -o "${DEST}/docs.json"; then
86+
echo "Failed to download docs.json from ${DOCS_URL}" >&2
87+
exit 3
88+
fi
89+
90+
# Replace placeholder '/DOCS-BASE/' with provided base in docs.json
91+
# Use `sd` if available, else fallback to sed
92+
if command -v sd >/dev/null 2>&1; then
93+
sd '/DOCS-BASE/' "$BASE" "${DEST}/docs.json"
94+
else
95+
# Use portable sed: escape slashes
96+
ESCAPED_BASE=$(printf '%s' "$BASE" | sed 's|/|\\/|g')
97+
sed -i "s/\/DOCS-BASE\//${ESCAPED_BASE}/g" "${DEST}/docs.json"
98+
fi
99+
100+
# Download assets and extract
101+
ASSETS_URL="${RELEASE_BASE_URL}/docs-assets.zip"
102+
ASSETS_ZIP="docs-assets.zip"
103+
if curl -sSfL "$ASSETS_URL" -o "$ASSETS_ZIP"; then
104+
echo "Extracting ${ASSETS_ZIP} to ${DEST}/assets"
105+
# Clean up existing assets if present
106+
rm -rf "${DEST}/assets"
107+
unzip -q "$ASSETS_ZIP"
108+
if [[ -d assets ]]; then
109+
mv assets "${DEST}/assets"
110+
else
111+
echo "Downloaded zip did not contain assets/ folder" >&2
112+
# keep build going; not necessarily fatal
113+
fi
114+
rm -f "$ASSETS_ZIP"
115+
else
116+
echo "No assets ZIP was found at ${ASSETS_URL} (continuing without error)" >&2
117+
fi
118+
119+
# Download favicon
120+
FAVICON_URL="https://github.com/${ORG}/org/raw/main/design/typst-community.icon.png"
121+
if curl -sSfL "$FAVICON_URL" -o "${DEST}/favicon.png"; then
122+
echo "Wrote favicon to ${DEST}/favicon.png"
123+
else
124+
echo "Failed to download favicon from ${FAVICON_URL} (continuing without error)" >&2
125+
fi
126+
127+
# Write metadata.json
128+
cat >"${DEST}/metadata.json" <<EOF
129+
{
130+
"\$schema": "../metadata.schema.json",
131+
"language": "en-US",
132+
"version": "${VERSION}",
133+
"typstOfficialUrl": "https://typst.app",
134+
"typstOfficialDocsUrl": "https://typst.app/docs/",
135+
"githubOrganizationUrl": "https://github.com/${ORG}",
136+
"socialLinks": [
137+
{ "url": "https://github.com/${ORG}/typst-docs-web" },
138+
{
139+
"title": "Discord (Typst)",
140+
"url": "https://discord.gg/2uDybryKPe"
141+
}
142+
],
143+
"originUrl": "${ORIGIN}",
144+
"basePath": "${BASE}",
145+
"displayTranslationStatus": false
146+
}
147+
EOF
148+
149+
# Done
150+
printf '\nFetch docs assets script done.\n'
151+
152+
exit 0

netlify-build.sh renamed to scripts/netlify-build.sh

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ mkdir _site
1414

1515
# Prepare favicon
1616
curl -L https://github.com/typst-community/org/raw/main/design/typst-community.icon.png \
17-
-o _site/favicon.png
17+
-o _site/favicon.png
1818
cp _site/favicon.png public/favicon.png
1919

2020
# Prepare the index page
2121
REF=$(git rev-parse --short HEAD)
2222
DATE=$(git log --max-count=1 --pretty='%cd' --date=iso)
23-
cat << EOF > _site/index.html
23+
cat <<EOF >_site/index.html
2424
<!DOCTYPE html>
2525
<html lang="en">
2626
<head>
@@ -54,6 +54,7 @@ cat << EOF > _site/index.html
5454
<p>Build a website from the documentation JSON file generated by <a href="https://github.com/typst/typst/blob/main/docs/Cargo.toml#L2">typst-docs</a>.</p>
5555
<p>This website is for developing typst-docs-web. Its contents might be changed at anytime.</p>
5656
<ul>
57+
<li><a href="./en-US-v0.14.1/">en-US, v0.14.1</a></li>
5758
<li><a href="./en-US-v0.14.0/">en-US, v0.14.0</a></li>
5859
<li><a href="./en-US-v0.13.1/">en-US, v0.13.1</a></li>
5960
<li><a href="./ja-JP/">ja-JP, v0.13.1</a></li>
@@ -71,67 +72,45 @@ EOF
7172
# 3.1. Build en-US
7273

7374
build_en_US() {
74-
local VERSION="$1"
75-
76-
BASE="en-US-$VERSION"
77-
78-
# Prepare docs.json
79-
curl -L https://github.com/typst-community/dev-builds/releases/download/docs-"$VERSION"/docs.json \
80-
-o public/docs.json
81-
sd '/DOCS-BASE/' "/$BASE/" public/docs.json
82-
83-
# Prepare docs assets
84-
curl -LO https://github.com/typst-community/dev-builds/releases/download/docs-"$VERSION"/docs-assets.zip
85-
unzip docs-assets.zip && rm docs-assets.zip
86-
mv assets public/assets
87-
88-
# Configure metadata
89-
cat << EOF > public/metadata.json
90-
{
91-
"\$schema": "../metadata.schema.json",
92-
"language": "en-US",
93-
"version": "${VERSION#v}",
94-
"typstOfficialUrl": "https://typst.app",
95-
"typstOfficialDocsUrl": "https://typst.app/docs/",
96-
"githubOrganizationUrl": "https://github.com/typst-community",
97-
"githubRepositoryUrl": "https://github.com/typst-community/typst-docs-web",
98-
"discordServerUrl": "https://discord.gg/2uDybryKPe",
99-
"originUrl": "${DEPLOY_URL:-https://example.com}/",
100-
"basePath": "/$BASE/",
101-
"displayTranslationStatus": false
102-
}
103-
EOF
104-
# $DEPLOY_URL will be set by netlify. Fallback to example.com for local testing.
105-
# https://docs.netlify.com/build/configure-builds/environment-variables/#deploy-urls-and-metadata
106-
107-
# Build
108-
mise exec -- bun run build
109-
mv dist _site/"$BASE"
110-
111-
# Clean
112-
rm -r public/{docs.json,assets,metadata.json}
75+
local VERSION="$1"
76+
local BASE="en-US-${VERSION}"
77+
78+
bash scripts/fetch-docs-assets.sh \
79+
--tag "${VERSION}" \
80+
--base "/${BASE}/" \
81+
--dest public \
82+
--origin "${DEPLOY_URL}"
83+
# $DEPLOY_URL will be set by netlify.
84+
# https://docs.netlify.com/build/configure-builds/environment-variables/#deploy-urls-and-metadata
85+
86+
# Build
87+
mise exec -- bun run build
88+
mv dist _site/"$BASE"
89+
90+
# Clean
91+
rm -r public/{docs.json,assets,metadata.json}
11392
}
11493

94+
build_en_US v0.14.1
11595
build_en_US v0.14.0
11696
build_en_US v0.13.1
11797

11898
# 3.2. Build ja-JP
11999

120-
# Prepare JSON files
121-
mise exec -- bun run fetch-docs-ja-jp
122-
sd '"/docs/' '"/ja-JP/' public/docs.json
123-
sd --fixed-strings \
124-
'"basePath": "/docs/",' \
125-
'"basePath": "/ja-JP/",' \
126-
public/metadata.json
127-
128100
# Prepare docs assets
129101
# At present, typst-jp do not translate comments within example code.
130102
# And there is no simple way to download assets from GitHub Actions or the gh-pages branch.
131103
# Therefore, we reuse the assets from the official version.
132-
curl -LO https://github.com/typst-community/dev-builds/releases/download/docs-v0.13.1/docs-assets.zip
133-
unzip docs-assets.zip && rm docs-assets.zip
134-
mv assets public/assets
104+
bash scripts/fetch-docs-assets.sh --tag "v0.13.1" --base "/irrelevant/"
105+
rm public/{docs,metadata}.json
106+
107+
# Prepare JSON files
108+
mise exec -- bun run fetch-docs-ja-jp
109+
sd '"/docs/' '"/ja-JP/' public/docs.json
110+
sd --fixed-strings \
111+
'"basePath": "/docs/",' \
112+
'"basePath": "/ja-JP/",' \
113+
public/metadata.json
135114

136115
# Build
137116
mise exec -- bun run build

0 commit comments

Comments
 (0)