Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: Resolve published asset paths in structured content (#6096)
Oleg Isonen43a9f824adb5
diff --git a/docs/university/foundations/assets.md b/docs/university/foundations/assets.mdindex 145c1c62c0..2e5ca94429 100644--- a/docs/university/foundations/assets.md+++ b/docs/university/foundations/assets.md@@ -83,10 +83,16 @@ background image, social image, download link, or another URL property. Thesame resolution works for nested properties and values in arrays. JSON filescan use relative paths in the same way.+Imported content can also use published asset paths such as+`./assets/hero.png` or `/assets/hero.png`. Webstudio matches the final file name+across project Asset folders, so the document and referenced asset do not need+to share the same folder hierarchy. The file name must identify exactly one+project asset.+Query strings and fragments are preserved, so values such as`./images/hero.png?width=1200#cover` remain usable. Webstudio resolves a value-only when it uniquely matches a project asset. Absolute URLs, root-relative-URLs, missing paths, and ambiguous paths remain unchanged.+only when it uniquely matches a project asset. External URLs, other+root-relative URLs, missing paths, and ambiguous paths remain unchanged.## Organizing assets with foldersdiff --git a/packages/content-engine/src/asset-path-resolution.ts b/packages/content-engine/src/asset-path-resolution.tsnew file mode 100644index 0000000000..11c4f03baf--- /dev/null+++ b/packages/content-engine/src/asset-path-resolution.ts@@ -0,0 +1,116 @@+import { encodeAssetPathSegment } from "./asset-path";++export const createUniqueAssetIdsByPath = (+ assets: Iterable<{ id: string; path: string }>+) => {+ const ambiguousPaths = new Set<string>();+ const assetIdsByPath = new Map<string, string>();+ for (const { id, path } of assets) {+ if (assetIdsByPath.has(path)) {+ ambiguousPaths.add(path);+ assetIdsByPath.delete(path);+ continue;+ }+ if (ambiguousPaths.has(path) === false) {+ assetIdsByPath.set(path, id);+ }+ }+ return assetIdsByPath;+};++const getRelativeAssetPath = ({+ sourcePath,+ url,+ allowRootRelative = false,+}: {+ sourcePath: string;+ url: string;+ allowRootRelative?: boolean;+}) => {+ if (+ url.length === 0 ||+ url.startsWith("#") ||+ url.startsWith("?") ||+ (allowRootRelative === false && url.startsWith("/"))+ ) {+ return;+ }+ const origin = "https://content.webstudio.invalid";+ let tokenPrefix = "__webstudio_asset_source_";+ while (sourcePath.includes(tokenPrefix) || url.includes(tokenPrefix)) {+ tokenPrefix = `_${tokenPrefix}`;+ }+ const sourceSegments = sourcePath.split("/");+ const sourceSegmentByToken = new Map(+ sourceSegments.map((segment, index) => [+ `${tokenPrefix}${index}__`,+ segment,+ ])+ );+ const sourceUrl = Array.from(sourceSegmentByToken.keys()).join("/");+ let parsed: URL;+ try {+ parsed = new URL(url, new URL(sourceUrl, `${origin}/`));+ } catch {+ return;+ }+ if (parsed.origin !== origin) {+ return;+ }+ const segments: string[] = [];+ for (const encodedSegment of parsed.pathname.slice(1).split("/")) {+ let segment: string;+ try {+ segment = decodeURIComponent(encodedSegment);+ } catch {+ return;+ }+ if (segment === "") {+ continue;+ }+ const sourceSegment = sourceSegmentByToken.get(segment);+ if (sourceSegment !== undefined) {+ segments.push(sourceSegment);+ continue;+ }+ if (segment.includes("/")) {+ return;+ }+ segments.push(encodeAssetPathSegment(segment));+ }+ return segments.join("/");+};++const getPublishedAssetName = (url: string) => {+ const path = getRelativeAssetPath({+ sourcePath: "document",+ url,+ allowRootRelative: true,+ });+ const segments = path?.split("/");+ if (segments?.length !== 2 || segments[0] !== "assets") {+ return;+ }+ return segments[1];+};++export const createAssetIdResolver = (+ assetIdsByPath: ReadonlyMap<string, string>,+ sourcePath: string+) => {+ const assetIds = new Set(assetIdsByPath.values());+ const assetIdsByName = createUniqueAssetIdsByPath(+ Array.from(assetIdsByPath, ([path, id]) => ({+ id,+ path: path.slice(path.lastIndexOf("/") + 1),+ }))+ );+ return (url: string) => {+ const path = getRelativeAssetPath({ sourcePath, url });+ return (+ (path === undefined ? undefined : assetIdsByPath.get(path)) ??+ assetIdsByName.get(getPublishedAssetName(url) ?? "") ??+ (assetIds.has(url) ? url : undefined)+ );+ };+};diff --git a/packages/content-engine/src/asset-value-references.test.ts b/packages/content-engine/src/asset-value-references.test.tsindex d9c9acd14e..8495e9f4b3 100644--- a/packages/content-engine/src/asset-value-references.test.ts+++ b/packages/content-engine/src/asset-value-references.test.ts@@ -63,6 +63,50 @@ describe("structured asset value references", () => {]);});+ test("resolves published Asset paths from sibling folders", () => {+ const properties = {+ relative: "./assets/inception_TDxBrdPwfoJJe_A-k5jhB.png",+ rootRelative: "/assets/inception_TDxBrdPwfoJJe_A-k5jhB.png",+ };+ const references = discoverAssetValueReferences({+ properties,+ sourcePath: "content/data/inception.json",+ assetIdsByPath: new Map([+ [+ "content/images/inception_TDxBrdPwfoJJe_A-k5jhB.png",+ "inception-image",+ ],+ ]),+ });++ expect(references).toEqual([+ {+ path: ["properties", "relative"],+ assetId: "inception-image",+ },+ {+ path: ["properties", "rootRelative"],+ assetId: "inception-image",+ },+ ]);+ expect(+ resolveAssetValueReferences({+ value: { properties },+ references,+ assetUrls: {+ "inception-image":+ "/cgi/image/inception_TDxBrdPwfoJJe_A-k5jhB.png?format=raw",+ },+ })+ ).toEqual({+ properties: {+ relative: "/cgi/image/inception_TDxBrdPwfoJJe_A-k5jhB.png?format=raw",+ rootRelative:+ "/cgi/image/inception_TDxBrdPwfoJJe_A-k5jhB.png?format=raw",+ },+ });+ });+test("resolves without mutating source values and merges URL suffixes", () => {const value = {properties: {diff --git a/packages/content-engine/src/asset-value-references.ts b/packages/content-engine/src/asset-value-references.tsindex 1a77eec20a..d4fcc4d49a 100644--- a/packages/content-engine/src/asset-value-references.ts+++ b/packages/content-engine/src/asset-value-references.ts@@ -1,4 +1,4 @@-import { createAssetIdResolver } from "./markdown-assets";+import { createAssetIdResolver } from "./asset-path-resolution";export type AssetValueReference = {path: Array<string | number>;diff --git a/packages/content-engine/src/content-source.test.ts b/packages/content-engine/src/content-source.test.tsindex b81c9970e4..df5c782d5e 100644--- a/packages/content-engine/src/content-source.test.ts+++ b/packages/content-engine/src/content-source.test.ts@@ -522,7 +522,7 @@ describe("content source snapshots", () => {});const portrait = createFile({id: "portrait",- path: "team/assets/portrait.png",+ path: "team/images/portrait.png",contentType: "image/png",});const source: ContentSource = {diff --git a/packages/content-engine/src/content-source.ts b/packages/content-engine/src/content-source.tsindex d1ca4a4388..f1223c9eb7 100644--- a/packages/content-engine/src/content-source.ts+++ b/packages/content-engine/src/content-source.ts@@ -10,10 +10,8 @@ import {type ContentCompilerInput,} from "./asset-index";import type { ContentArtifactV1 } from "./schema";-import {- createUniqueAssetIdsByPath,- discoverMarkdownAssetReferenceRanges,-} from "./markdown-assets";+import { discoverMarkdownAssetReferenceRanges } from "./markdown-assets";+import { createUniqueAssetIdsByPath } from "./asset-path-resolution";import type { MarkdownAssetReferences } from "./markdown-references";import {discoverAssetValueReferences,diff --git a/packages/content-engine/src/markdown-assets.test.ts b/packages/content-engine/src/markdown-assets.test.tsindex 2c5cb9879a..9437bd2c3a 100644--- a/packages/content-engine/src/markdown-assets.test.ts+++ b/packages/content-engine/src/markdown-assets.test.ts@@ -100,6 +100,40 @@ describe("Markdown asset references", () => {});});+ test("resolves published Asset paths by unique asset name", () => {+ expect(+ discoverReferences({+ sourcePath: "blog/posts/inception.md",+ markdown: `+++ `,+ assetIdsByPath: new Map([+ [+ "blog/images/inception_TDxBrdPwfoJJe_A-k5jhB.png",+ "inception-image",+ ],+ ]),+ })+ ).toEqual({+ "./assets/inception_TDxBrdPwfoJJe_A-k5jhB.png": "inception-image",+ "/assets/inception_TDxBrdPwfoJJe_A-k5jhB.png": "inception-image",+ });+ });++ test("leaves published Asset paths with ambiguous names unresolved", () => {+ expect(+ discoverReferences({+ sourcePath: "blog/posts/article.md",+ markdown: "",+ assetIdsByPath: new Map([+ ["blog/images/hero.png", "blog-hero"],+ ["archive/images/hero.png", "archive-hero"],+ ]),+ })+ ).toEqual({});+ });+test("preserves encoded dot segments in canonical source folders", () => {expect(discoverReferences({diff --git a/packages/content-engine/src/markdown-assets.ts b/packages/content-engine/src/markdown-assets.tsindex 30924be195..a469a3c3a8 100644--- a/packages/content-engine/src/markdown-assets.ts+++ b/packages/content-engine/src/markdown-assets.ts@@ -1,26 +1,12 @@import { parse, postprocess, preprocess } from "micromark";import { decodeString } from "micromark-util-decode-string";-import { createCanonicalAssetPath, encodeAssetPathSegment } from "./asset-path";+import { createCanonicalAssetPath } from "./asset-path";+import {+ createAssetIdResolver,+ createUniqueAssetIdsByPath,+} from "./asset-path-resolution";import type { MarkdownAssetReference } from "./markdown-references";-export const createUniqueAssetIdsByPath = (- assets: Iterable<{ id: string; path: string }>-) => {- const ambiguousPaths = new Set<string>();- const assetIdsByPath = new Map<string, string>();- for (const { id, path } of assets) {- if (assetIdsByPath.has(path)) {- ambiguousPaths.add(path);- assetIdsByPath.delete(path);- continue;- }- if (ambiguousPaths.has(path) === false) {- assetIdsByPath.set(path, id);- }- }- return assetIdsByPath;-};-const getMarkdownUrlTokens = (markdown: string) => {const events = postprocess(parse()@@ -46,81 +32,6 @@ const getMarkdownUrlTokens = (markdown: string) => {return tokens;};-const getRelativeAssetPath = ({- sourcePath,- url,-}: {- sourcePath: string;- url: string;-}) => {- if (- url.length === 0 ||- url.startsWith("#") ||- url.startsWith("?") ||- url.startsWith("/")- ) {- return;- }- const origin = "https://content.webstudio.invalid";- let tokenPrefix = "__webstudio_asset_source_";- while (sourcePath.includes(tokenPrefix) || url.includes(tokenPrefix)) {- tokenPrefix = `_${tokenPrefix}`;- }- const sourceSegments = sourcePath.split("/");- const sourceSegmentByToken = new Map(- sourceSegments.map((segment, index) => [- `${tokenPrefix}${index}__`,- segment,- ])- );- const sourceUrl = Array.from(sourceSegmentByToken.keys()).join("/");- let parsed: URL;- try {- parsed = new URL(url, new URL(sourceUrl, `${origin}/`));- } catch {- return;- }- if (parsed.origin !== origin) {- return;- }- const segments: string[] = [];- for (const encodedSegment of parsed.pathname.slice(1).split("/")) {- let segment: string;- try {- segment = decodeURIComponent(encodedSegment);- } catch {- return;- }- if (segment === "") {- continue;- }- const sourceSegment = sourceSegmentByToken.get(segment);- if (sourceSegment !== undefined) {- segments.push(sourceSegment);- continue;- }- if (segment.includes("/")) {- return;- }- segments.push(encodeAssetPathSegment(segment));- }- return segments.join("/");-};--export const createAssetIdResolver = (- assetIdsByPath: ReadonlyMap<string, string>,- sourcePath: string-) => {- const assetIds = new Set(assetIdsByPath.values());- return (url: string) => {- const path = getRelativeAssetPath({ sourcePath, url });- return (- (path === undefined ? undefined : assetIdsByPath.get(path)) ??- (assetIds.has(url) ? url : undefined)- );- };-};-export const discoverMarkdownAssetReferenceRanges = ({markdown,sourcePath,