Skip to content

Commit 384139b

Browse files
committed
canonicalizeurl refactoring to avoid source domain
1 parent 00c1c21 commit 384139b

File tree

6 files changed

+48
-5
lines changed

6 files changed

+48
-5
lines changed

astro.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import prefetch from "@astrojs/prefetch";
33
import getRedirects from "./src/lib/getRedirects";
44

55
import sitemap from "@astrojs/sitemap";
6+
import "dotenv/config";
67

78
// https://astro.build/config
89
export default defineConfig({

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@fontsource-variable/lora": "^5.0.9",
3232
"@fontsource/source-sans-pro": "^5.0.8",
3333
"astro": "^3.1.0",
34+
"dotenv": "^16.3.1",
3435
"feather-icons": "^4.29.1",
3536
"findup-sync": "^5.0.0",
3637
"gray-matter": "^4.0.3",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/canonicalizeUrl.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,26 @@ import canonicalizeUrl from "./canonicalizeUrl";
33

44
describe("canonicalizeUrl", () => {
55
it("adds schema if missing", async () => {
6-
const result = canonicalizeUrl("padm.us/psychpricing");
6+
const result = canonicalizeUrl("doc.bmndr.co/psychpricing");
77

88
expect(result).toContain("https://");
99
});
10+
11+
it("replaces domain", async () => {
12+
const result = canonicalizeUrl("doc.bmndr.co/psychpricing");
13+
14+
expect(result).not.toContain("doc.bmndr.co");
15+
});
16+
17+
it("uses source domain", async () => {
18+
const result = canonicalizeUrl("doc.bmndr.co/psychpricing");
19+
20+
expect(result).not.toContain("undefined");
21+
});
22+
23+
it("returns the correct url", async () => {
24+
const result = canonicalizeUrl("doc.bmndr.co/psychpricing");
25+
26+
expect(result).toEqual("https://the_source_domain/psychpricing/export/txt");
27+
});
1028
});

src/lib/canonicalizeUrl.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import env from "./env";
2+
13
export default function canonicalizeUrl(url: string): string {
4+
const domain = env("SOURCE_DOMAIN");
25
// Start with basic cleanup canonicalization applicable to all URLs...
36
url = url.trim(); // trim whitespace
47
if (!url.match(/^https?:\/\//i)) url = "https://" + url; // add protocol
@@ -12,14 +15,21 @@ export default function canonicalizeUrl(url: string): string {
1215
/^(https?:\/\/doc\.bmndr\.com)\/([^/]+)$/,
1316
/^(https?:\/\/doc\.beeminder\.com)\/([^/]+)$/,
1417
];
18+
1519
for (const regex of sugarlist) {
16-
url = url.replace(regex, "https://" + "padm" + "." + "us" + "/" + "$2");
20+
url = url.replace(regex, `https://${domain}/$2`);
1721
}
1822

19-
// Following is a special case for the raw padm.us URL just in case:
20-
url = url.replace(/^(https?:\/\/padm\.us)$/, "$1/public/export/txt");
23+
// Following is a special case for the raw source URL just in case:
24+
url = url.replace(
25+
new RegExp(`^(https?://${domain})$`),
26+
"$1/public/export/txt",
27+
);
2128
// And finally the key transformation: append /export/txt for etherpad:
22-
url = url.replace(/^(https?:\/\/padm\.us)\/([^/]+)$/, "$1/$2/export/txt");
29+
url = url.replace(
30+
new RegExp(`^(https?://${domain})/([^/]+)$`),
31+
"$1/$2/export/txt",
32+
);
2333

2434
return url;
2535
}

vitest.setup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,10 @@ beforeEach(() => {
6262
timeEnd: vi.fn(),
6363
});
6464

65+
vi.stubGlobal("process", {
66+
...process,
67+
env: { ...process.env, SOURCE_DOMAIN: "the_source_domain" },
68+
});
69+
6570
__reset();
6671
});

0 commit comments

Comments
 (0)