From 108ab6e5b09075eccea636a1a5658862e1b2fb42 Mon Sep 17 00:00:00 2001 From: Ryan Yin Date: Mon, 16 Jun 2025 22:40:43 +0800 Subject: [PATCH] fix: pdf-exporter - fix page sorting, add margin & header/footer (#228) --- .github/workflows/release-pdf.yml | 74 ++++++++++++------------- docs/.vitepress/vitepress-pdf.config.ts | 38 +++++++++++-- 2 files changed, 69 insertions(+), 43 deletions(-) diff --git a/.github/workflows/release-pdf.yml b/.github/workflows/release-pdf.yml index ef2c655..6ff0bd4 100644 --- a/.github/workflows/release-pdf.yml +++ b/.github/workflows/release-pdf.yml @@ -10,41 +10,41 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v4 - # - name: Install nix - # uses: cachix/install-nix-action@v23 + - name: Install nix + uses: cachix/install-nix-action@v31 + with: + nix_path: "nixpkgs=channel:nixpkgs-unstable" + extra_nix_config: | + experimental-features = nix-command flakes + access-tokens = github.com=${{ secrets.GITHUB_TOKEN }} + - name: Show nixpkgs version + run: nix-instantiate --eval -E '(import {}).lib.version' + - name: Run a command with nix develop + run: | + nix develop .#export-pdf --ignore-environment --command bash -c ' + pnpm install + pnpm export-pdf + ' + # # For debugging, upload the pdfs as artifacts + # - uses: actions/upload-artifact@v3 # with: - # nix_path: "nixpkgs=channel:nixpkgs-unstable" - # extra_nix_config: | - # experimental-features = nix-command flakes - # access-tokens = github.com=${{ secrets.GITHUB_TOKEN }} - # - name: Show nixpkgs version - # run: nix-instantiate --eval -E '(import {}).lib.version' - # - name: Run a command with nix develop - # run: | - # nix develop .#export-pdf --ignore-environment --command bash -c ' - # pnpm install - # pnpm export-pdf - # ' - # # # For debugging, upload the pdfs as artifacts - # # - uses: actions/upload-artifact@v3 - # # with: - # # name: pdf - # # path: '*.pdf' - # - name: Generate changelog - # run: | - # # Get the previous tag - # git fetch --tags - # PREVIOUS_TAG=$(git tag --sort=-creatordate | head -n 2 | tail -n 1) - # # Generate the changelog - # git log --pretty=format:"%h %s" $PREVIOUS_TAG..HEAD > CHANGELOG - # - # echo "Changelog from $PREVIOUS_TAG to HEAD:" - # cat CHANGELOG - # - name: Release PDF - # uses: softprops/action-gh-release@v1 - # if: startsWith(github.ref, 'refs/tags/') - # with: - # generate_release_notes: true - # body_path: CHANGELOG - # files: | - # nixos-and-flakes-book.pdf + # name: pdf + # path: '*.pdf' + - name: Generate changelog + run: | + # Get the previous tag + git fetch --tags + PREVIOUS_TAG=$(git tag --sort=-creatordate | head -n 2 | tail -n 1) + # Generate the changelog + git log --pretty=format:"%h %s" $PREVIOUS_TAG..HEAD > CHANGELOG + + echo "Changelog from $PREVIOUS_TAG to HEAD:" + cat CHANGELOG + - name: Release PDF + uses: softprops/action-gh-release@v2 + if: startsWith(github.ref, 'refs/tags/') + with: + generate_release_notes: true + body_path: CHANGELOG + files: | + nixos-and-flakes-book.pdf diff --git a/docs/.vitepress/vitepress-pdf.config.ts b/docs/.vitepress/vitepress-pdf.config.ts index 45aa362..2c0a60a 100644 --- a/docs/.vitepress/vitepress-pdf.config.ts +++ b/docs/.vitepress/vitepress-pdf.config.ts @@ -17,14 +17,40 @@ if (sidebar) { // print routeOrder in terminal console.log("routeOrder: ", routeOrder) +const headerTemplate = `
+ +
` + +const footerTemplate = `
+ +
` + export default defineUserConfig({ + urlOrigin: "https://nixos-and-flakes.thiscute.world/", + pdfOptions: { + format: "A4", + printBackground: true, + displayHeaderFooter: true, + headerTemplate, + footerTemplate, + margin: { + bottom: 60, + left: 25, + right: 25, + top: 60, + }, + }, + routePatterns: ["!/zh/**"], // exclude zh-CN pages sorter: (pageA, pageB) => { - const aIndex = routeOrder.findIndex((route) => route === pageA.path) - const bIndex = routeOrder.findIndex((route) => route === pageB.path) - const index = aIndex - bIndex - // console.log(`sorter: ${pageA.path} vs ${pageB.path} = ${index}`); - return index + // console.log("pageA: ", pageA.path, " pageB: ", pageB.path) + + // remove the locale prefix + const pathA = pageA.path.replace("/en/", "/") + const pathB = pageB.path.replace("/en/", "/") + // compare + const aIndex = routeOrder.findIndex((route) => route === pathA) + const bIndex = routeOrder.findIndex((route) => route === pathB) + return aIndex - bIndex }, - urlOrigin: "https://nixos-and-flakes.thiscute.world/", })