Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
ci(release): pin release note images to the tag being published
Elian Doran892125bba660
diff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex 24ee581ca5..5b746a7cc6 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -140,6 +140,16 @@ jobs:with:sparse-checkout: |docs/Release Notes+ scripts++ - uses: actions/setup-node@v7+ with:+ node-version: 24++ # The release body cannot be the note as it stands: its images are relative to the+ # folder the note lives in, and GitHub resolves those against the releases URL.+ - name: Compose the release body+ run: node --experimental-strip-types scripts/release-notes-body.mts "${{ github.ref_name }}" "$RUNNER_TEMP/release-body.md"- name: Download all artifactsuses: actions/download-artifact@v8@@ -152,7 +162,7 @@ jobs:uses: softprops/[email protected]with:draft: false- body_path: docs/Release Notes/Release Notes/${{ github.ref_name }}.md+ body_path: ${{ runner.temp }}/release-body.mdfail_on_unmatched_files: truefiles: upload/*.*discussion_category_name: Releasesdiff --git a/package.json b/package.jsonindex f2237ae102..e40c00dec6 100644--- a/package.json+++ b/package.json@@ -33,6 +33,7 @@"electron:start-prod": "pnpm desktop:start-prod","chore:ci-update-nightly-version": "tsx ./scripts/update-nightly-version.ts","chore:generate-openapi": "tsx ./scripts/generate-openapi.ts",+ "chore:release-notes-body": "node --experimental-strip-types ./scripts/release-notes-body.mts","chore:update-build-info": "tsx ./scripts/update-build-info.ts","chore:update-flake-electron": "tsx ./scripts/update-flake-electron.ts","chore:update-version": "tsx ./scripts/update-version.ts",diff --git a/scripts/release-notes-body.mts b/scripts/release-notes-body.mtsnew file mode 100644index 0000000000..a04696decb--- /dev/null+++ b/scripts/release-notes-body.mts@@ -0,0 +1,120 @@+/**+ * Composes the GitHub release body from a release note, by rewriting its relative image+ * references into absolute URLs pinned to the tag being released.+ *+ * The notes under `docs/Release Notes` are exported from Trilium, so their images are bare+ * filenames sitting next to the Markdown:+ *+ * <figure class="image"><img src="v0.105.0_image.png" width="1509" height="1329"></figure>+ *+ * That renders when browsing the repository, but a release body is not a file in the+ * repository — GitHub resolves a relative source against the releases URL, so every image+ * would arrive broken. The publish step in `release.yml` therefore runs the note through+ * this script and hands the result to `body_path` instead of the note itself.+ *+ * Pinning to `refs/tags/<tag>` rather than a branch keeps an already-published body correct+ * when the images are later renamed or moved.+ *+ * The script deliberately has no dependencies, so the publish job can run it straight off a+ * sparse checkout without installing the monorepo. Usage:+ *+ * node --experimental-strip-types ./scripts/release-notes-body.mts v0.105.0 /tmp/body.md+ */++import { existsSync, readFileSync, writeFileSync } from "node:fs";+import { dirname, join, resolve } from "node:path";+import { fileURLToPath } from "node:url";++const SCRIPT_PATH = fileURLToPath(import.meta.url);+const ROOT = resolve(dirname(SCRIPT_PATH), "..");+const NOTES_PATH = [ "docs", "Release Notes", "Release Notes" ];+const DEFAULT_REPOSITORY = "TriliumNext/Trilium";++export function main(tag: string, outputPath: string, repository: string) {+ const notePath = join(ROOT, ...NOTES_PATH, `${tag}.md`);+ if (!existsSync(notePath)) {+ throw new Error(`There is no release note for ${tag}; expected it at ${notePath}.`);+ }++ const { body, images } = rewriteImageSources(readFileSync(notePath, "utf-8"), { repository, tag });++ const missing = images.filter((image) => !existsSync(join(ROOT, ...image.split("/"))));+ if (missing.length > 0) {+ throw new Error(`${tag}.md references images that are not in the repository:\n ${missing.join("\n ")}`);+ }++ writeFileSync(outputPath, body);+ console.log(`Wrote ${outputPath} from ${notePath}.`);+ for (const image of images) {+ console.log(` pinned ${image}`);+ }+}++interface RewriteOptions {+ repository: string;+ tag: string;+}++const HTML_IMAGE = /(<img\b[^>]*?\bsrc\s*=\s*)(["'])(.*?)\2/gi;+const MARKDOWN_IMAGE = /(!\[[^\]]*\]\()([^)\s]+)/g;++/**+ * Returns the note with every relative image source replaced by a tag-pinned URL, alongside+ * the repository-relative path of each image it rewrote, so the caller can check they exist.+ */+export function rewriteImageSources(note: string, { repository, tag }: RewriteOptions) {+ const images: string[] = [];++ function pin(source: string) {+ if (!isRelative(source)) {+ return source;+ }++ const image = resolveAgainstNotes(source);+ images.push(image);+ const path = image.split("/").map(encodeURIComponent).join("/");+ return `https://raw.githubusercontent.com/${repository}/refs/tags/${encodeURIComponent(tag)}/${path}`;+ }++ const body = note+ .replace(HTML_IMAGE, (_match, prefix: string, quote: string, source: string) => `${prefix}${quote}${pin(source)}${quote}`)+ .replace(MARKDOWN_IMAGE, (_match, prefix: string, source: string) => `${prefix}${pin(source)}`);++ return { body, images };+}++function isRelative(source: string) {+ return !/^[a-z][a-z0-9+.-]*:/i.test(source) && !source.startsWith("//") && !source.startsWith("#");+}++function resolveAgainstNotes(source: string) {+ const segments = [ ...NOTES_PATH ];+ for (const segment of source.split("/")) {+ if (segment === "" || segment === ".") {+ continue;+ }++ if (segment === "..") {+ segments.pop();+ } else {+ segments.push(segment);+ }+ }+ return segments.join("/");+}++// Only when run as a script — the pure helpers above are imported by the spec.+if (process.argv[1] === SCRIPT_PATH) {+ const [ tag, outputPath ] = process.argv.slice(2);+ if (!tag || !outputPath) {+ console.error("Usage: release-notes-body.mts <tag> <output-path>");+ process.exit(1);+ }++ try {+ main(tag, outputPath, process.env.GITHUB_REPOSITORY || DEFAULT_REPOSITORY);+ } catch (err) {+ console.error(err instanceof Error ? err.message : err);+ process.exit(1);+ }+}diff --git a/scripts/release-notes-body.spec.ts b/scripts/release-notes-body.spec.tsnew file mode 100644index 0000000000..3c6cac2cc1--- /dev/null+++ b/scripts/release-notes-body.spec.ts@@ -0,0 +1,53 @@+import { describe, expect, it } from "vitest";++import { rewriteImageSources } from "./release-notes-body.mjs";++const OPTIONS = { repository: "TriliumNext/Trilium", tag: "v0.105.0" };+const BASE = "https://raw.githubusercontent.com/TriliumNext/Trilium/refs/tags/v0.105.0/docs/Release%20Notes/Release%20Notes";++describe("rewriteImageSources", () => {+ it("pins relative sources to the tag, in both the exported and the Markdown form", () => {+ const { body, images } = rewriteImageSources([+ `<figure class="image image_resized" style="width:45.59%;"><img style="aspect-ratio:1509/1329;" src="v0.105.0_image.png" width="1509" height="1329"></figure>`,+ ``+ ].join("\n"), OPTIONS);++ expect(body).toBe([+ `<figure class="image image_resized" style="width:45.59%;"><img style="aspect-ratio:1509/1329;" src="${BASE}/v0.105.0_image.png" width="1509" height="1329"></figure>`,+ ``+ ].join("\n"));+ expect(images).toEqual([+ "docs/Release Notes/Release Notes/v0.105.0_image.png",+ "docs/Release Notes/Release Notes/1_v0.105.0_image.png"+ ]);+ });++ it("leaves sources that are already absolute alone", () => {+ const note = [+ `<img src="https://github.com/user-attachments/assets/25f916f8.png">`,+ `<img src="//example.com/hosted.png">`,+ `<img src="data:image/png;base64,AAAA">`,+ ``,+ `[A link](v0.99.5.md) and <a href="v0.99.5.md">another</a>`+ ].join("\n");++ const { body, images } = rewriteImageSources(note, OPTIONS);++ expect(body).toBe(note);+ expect(images).toEqual([]);+ });++ it("resolves a source against the release notes folder", () => {+ const { body, images } = rewriteImageSources(+ `<img src="./same.png"><img src="../shared/other.png">`, OPTIONS);++ expect(body).toBe([+ `<img src="${BASE}/same.png">`,+ `<img src="https://raw.githubusercontent.com/TriliumNext/Trilium/refs/tags/v0.105.0/docs/Release%20Notes/shared/other.png">`+ ].join(""));+ expect(images).toEqual([+ "docs/Release Notes/Release Notes/same.png",+ "docs/Release Notes/shared/other.png"+ ]);+ });+});