@enonic/nextjs-adapter 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/UrlProcessor.js CHANGED
@@ -59,7 +59,7 @@ export class UrlProcessor {
59
59
  // normalise localhost-127.0.0.1 if present in urls
60
60
  const normalUrl = url.replace(this.localhostPattern, '127.0.0.1');
61
61
  const normalApiUrl = apiUrl.replace(this.localhostPattern, '127.0.0.1');
62
- const common = commonChars(normalUrl, normalApiUrl);
62
+ const common = commonChars(normalUrl, normalApiUrl, '/');
63
63
  const remaining = common.length > 0 ? normalUrl.substring(common.length) : normalUrl;
64
64
  return (remaining.length > 0 && remaining.charAt(0) === '/') ? remaining.substring(1) : remaining;
65
65
  }
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "@enonic/nextjs-adapter",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "license": "Apache-2.0",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
9
9
  "scripts": {
10
- "copy": "npx --yes cpy-cli package.json README.md dist",
10
+ "copy": "npx --yes cpy-cli package.json README.md setversion.js dist",
11
11
  "build": "tsc --build src/tsconfig.json && tsc --build server/tsconfig.json",
12
12
  "lint": "eslint --quiet server && eslint --quiet src",
13
- "lint:fix": "eslint --fix server && eslint --fix src"
13
+ "lint:fix": "eslint --fix server && eslint --fix src",
14
+ "prepublishOnly": "node setversion.js"
14
15
  },
15
16
  "dependencies": {
16
17
  "html-react-parser": "^4.2.2",
package/setversion.js ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+
3
+ "use strict";
4
+
5
+ const { execSync } = require("child_process");
6
+ const version = execSync("git describe --tags --abbrev=0").toString().slice(1).trim();
7
+
8
+ execSync(`awk -v ver="${version}" '{gsub(/"version": ".*"/,"\\"version\\": \\""ver"\\"")}1' package.json > tmp && mv tmp package.json`);
package/utils.d.ts CHANGED
@@ -101,7 +101,7 @@ export declare function getContentApiUrl(context?: MinimalContext): string;
101
101
  export declare function addJsessionHeaders(headers?: Object, context?: MinimalContext): void;
102
102
  export declare function addLocaleHeaders(headers?: Object, context?: Context): void;
103
103
  export declare const getXpBaseUrl: (context?: MinimalContext) => string;
104
- export declare const commonChars: (s1?: string, s2?: string) => string;
104
+ export declare const commonChars: (s1?: string, s2?: string, wordSeparators?: string) => string;
105
105
  export declare const sanitizeGraphqlName: (text: string) => string;
106
106
  declare const adapterConstants: {
107
107
  IS_DEV_MODE: boolean;
package/utils.js CHANGED
@@ -195,20 +195,31 @@ export const getXpBaseUrl = (context) => {
195
195
  // TODO: workaround for XP pattern controller mapping not picked up in edit mode
196
196
  return (header || '/').replace(/\/edit\//, '/inline/');
197
197
  };
198
- export const commonChars = (s1, s2) => {
198
+ export const commonChars = (s1, s2, wordSeparators) => {
199
199
  let result = '';
200
200
  if (!s1 || s1.length === 0 || !s2 || s2.length === 0) {
201
201
  return result;
202
202
  }
203
+ let lastSeparatorIndex = -1;
204
+ let lastCommonIndex = -1;
203
205
  for (let i = 0; i < s1.length; i++) {
204
- const s1Element = s1[i];
205
- if (s2[i] === s1Element) {
206
- result += s1Element;
206
+ const s1El = s1[i];
207
+ if ((wordSeparators === null || wordSeparators === void 0 ? void 0 : wordSeparators.indexOf(s1El)) >= 0) {
208
+ lastSeparatorIndex = i;
209
+ }
210
+ if (s2[i] === s1El) {
211
+ lastCommonIndex = i;
207
212
  }
208
213
  else {
209
214
  break;
210
215
  }
211
216
  }
217
+ if (lastSeparatorIndex > 0 || lastCommonIndex === lastSeparatorIndex) {
218
+ result = s1.substring(0, lastSeparatorIndex);
219
+ }
220
+ else if (lastCommonIndex > -1) {
221
+ result = s1.substring(0, lastCommonIndex + 1);
222
+ }
212
223
  return result;
213
224
  };
214
225
  // sanitizes text according to graphql naming spec http://spec.graphql.org/October2021/#sec-Names