Skip to content

Commit 1d67dd0

Browse files
committed
use same logic for creating subdomain
1 parent 40ee558 commit 1d67dd0

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

bin/post-preview-url-comment/index.node.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from "vitest";
22
import { DOCS_BASE_URL, PREVIEW_URL_REGEX } from "./constants";
3-
import { filenameToPath } from "./util";
3+
import { filenameToPath, branchToSubdomain } from "./util";
44

55
test("PREVIEW_URL_REGEX", () => {
66
const comment =
@@ -13,6 +13,20 @@ test("PREVIEW_URL_REGEX", () => {
1313
);
1414
});
1515

16+
describe("branchToSubdomain", () => {
17+
test("slash", () => {
18+
expect(branchToSubdomain("kian/pcx-15803")).toEqual("kian-pcx-15803");
19+
});
20+
21+
test("normal", () => {
22+
expect(branchToSubdomain("pcx-15803")).toEqual("pcx-15803");
23+
});
24+
25+
test("capitalisation", () => {
26+
expect(branchToSubdomain("PCX-15803")).toEqual("pcx-15803");
27+
});
28+
});
29+
1630
describe("filenameToPath", () => {
1731
test("index", () => {
1832
expect(filenameToPath("src/content/docs/workers/index.mdx")).toEqual(

bin/post-preview-url-comment/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
PREVIEW_URL_REGEX,
99
} from "./constants";
1010

11-
import { filenameToPath, slug } from "./util";
11+
import { filenameToPath, branchToSubdomain } from "./util";
1212

1313
async function run(): Promise<void> {
1414
try {
@@ -51,7 +51,7 @@ async function run(): Promise<void> {
5151
);
5252

5353
const previewUrl = {
54-
branch: `https://${slug(process.env.GITHUB_REF_NAME)}.preview.developers.cloudflare.com`,
54+
branch: `https://${branchToSubdomain(process.env.GITHUB_REF_NAME)}.preview.developers.cloudflare.com`,
5555
commit: `https://${ctx.sha.slice(0, 8)}.preview.developers.cloudflare.com`,
5656
};
5757

bin/post-preview-url-comment/util.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { slug } from "github-slugger";
2-
2+
import { execSync } from "node:child_process";
33
import { CONTENT_BASE_PATH } from "./constants";
44

55
export const filenameToPath = (filename: string) => {
@@ -30,4 +30,9 @@ export const filenameToPath = (filename: string) => {
3030
.concat("/");
3131
};
3232

33-
export { slug };
33+
export const branchToSubdomain = (branch: string) => {
34+
return execSync(
35+
`echo "${branch}" | iconv -c -t ascii//TRANSLIT | sed -E 's/[~^]+//g' | sed -E 's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-+|-+$//g' | tr A-Z a-z`,
36+
{ encoding: "utf-8" },
37+
).trim();
38+
};

0 commit comments

Comments
 (0)