Skip to content

Commit

Permalink
fix(cms): add category link resolver (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanilowicz authored Oct 23, 2023
1 parent 2e80139 commit f3be470
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tidy-kiwis-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware-pwa/cms-base": patch
---

Add category link resolver
2 changes: 2 additions & 0 deletions packages/cms-base/helpers/buildUrlPrefix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default function buildUrlPrefix(url: string | any, prefix: string) {
url = url[0] !== "/" ? `/${url}` : url;

if (typeof url === "string") {
return prefix ? `/${prefix}${url}` : url;
}
Expand Down
8 changes: 7 additions & 1 deletion packages/cms-base/helpers/html-to-vue/getOptionsFromNode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import resolveUrl from "../resolveUrl";

export type NodeObject = {
type: string;
name: string;
Expand Down Expand Up @@ -34,9 +36,13 @@ export function getOptionsFromNode(node: any): Options {
align = node.attrs.align;
delete node.attrs.align;
}

const attrs = Object.keys(node.attrs).length === 0 ? null : { ...node.attrs };

// Resolve URL if exist
if (attrs?.href) {
attrs.href = `${resolveUrl(attrs.href)}`;
}

return {
align: align,
attrs: attrs,
Expand Down
16 changes: 16 additions & 0 deletions packages/cms-base/helpers/resolveUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import buildUrlPrefix from "./buildUrlPrefix";
import getUrlPrefix from "./getUrlPrefix";

export default function resolveUrl(url: string) {
const navigationPattern = /[a-zA-z0-9]+\/navigation\/[a-zA-z0-9]+/;
const urlPrefix = getUrlPrefix();

switch (true) {
case navigationPattern.test(url):
// Remove first lang segment of the URL
const newUrl = url.split("/").slice(1);
return newUrl ? `${buildUrlPrefix(newUrl.join("/"), urlPrefix)}` : url;
default:
return url;
}
}

2 comments on commit f3be470

@vercel
Copy link

@vercel vercel bot commented on f3be470 Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

frontends-demo – ./templates/vue-demo-store

frontends-demo-git-main-shopware-frontends.vercel.app
frontends-demo.vercel.app
frontends-demo-shopware-frontends.vercel.app

@vercel
Copy link

@vercel vercel bot commented on f3be470 Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.