Skip to content

Commit eba6ffd

Browse files
authored
feat: add open graph on the root path (#173)
1 parent d824b18 commit eba6ffd

3 files changed

Lines changed: 58 additions & 36 deletions

File tree

.github/scripts/find-all-pages.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,27 @@ function pageFrom(htmlFile) {
4141

4242
return htmlFile.replace(".html", "");
4343
}
44+
45+
export const LOCALES = ["en", "id"];
46+
export const EXCLUDED_PAGES = [...LOCALES, "404", "_not-found"];
47+
48+
export function pathWithoutLocaleReducer(acc, path) {
49+
for (const locale of LOCALES) {
50+
if (path.startsWith(`${locale}/`)) {
51+
return mergePaths(acc, path.replace(`${locale}/`, ""));
52+
}
53+
}
54+
55+
return mergePaths(acc, path);
56+
}
57+
58+
function mergePaths(paths, path) {
59+
if (!paths.includes(path)) {
60+
return [...paths, path];
61+
}
62+
return paths;
63+
}
64+
65+
export function clean(path) {
66+
return path ? `/${path}` : "";
67+
}

.github/scripts/generate-redirect.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
const { existsSync } = require("fs");
22
const { writeFile, mkdir } = require("fs/promises");
33
const { join } = require("path");
4-
const { findAllPages } = require("./find-all-pages");
4+
const {
5+
clean,
6+
EXCLUDED_PAGES,
7+
findAllPages,
8+
pathWithoutLocaleReducer
9+
} = require("./find-all-pages");
510

611
// Next.js export directory
712
const OUTPUT_DIR = join(process.cwd(), "out");
@@ -14,23 +19,19 @@ await (async function generateSitemap() {
1419
}
1520

1621
const pages = findAllPages(OUTPUT_DIR)
17-
// exclude preserved paths
18-
.filter((page) => !["en", "id", "404"].includes(page))
19-
// remove duplication
20-
.filter((page) => page.startsWith(`${DEFAULT_LOCALE}/`))
21-
// remove locale
22-
.map((page) => page.replace(`${DEFAULT_LOCALE}/`, ""));
23-
const pagesWithRootPage = ["", ...pages];
24-
25-
await Promise.all(
26-
pagesWithRootPage.map(
22+
.filter((page) => !["", ...EXCLUDED_PAGES].includes(page))
23+
.reduce(pathWithoutLocaleReducer, []);
24+
25+
const redirects = await Promise.all(
26+
pages.map(
2727
async (page) =>
28-
await createRedirectPage(
29-
page,
30-
page ? `/${DEFAULT_LOCALE}/${page}` : `/${DEFAULT_LOCALE}`
31-
)
28+
await createRedirectPage(page, `/${DEFAULT_LOCALE}${clean(page)}`)
3229
)
3330
);
31+
for (const { fromPath, toPath } of redirects.sort()) {
32+
console.info(`Generated redirect: /${fromPath} -> ${toPath}`);
33+
}
34+
console.info("✅ Redirects generated");
3435
})();
3536

3637
async function createRedirectPage(fromPath, toPath) {
@@ -51,5 +52,5 @@ async function createRedirectPage(fromPath, toPath) {
5152
`;
5253

5354
await writeFile(join(dir, "index.html"), html.trim(), "utf8");
54-
console.log(`Generated redirect: ${fromPath} -> ${toPath}`);
55+
return { fromPath, toPath };
5556
}

.github/scripts/generate-sitemap.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,31 @@ const { existsSync } = require("fs");
22
const { writeFile } = require("fs/promises");
33
const { join } = require("path");
44
const { format } = require("prettier");
5-
const { findAllPages } = require("./find-all-pages");
5+
const {
6+
clean,
7+
EXCLUDED_PAGES,
8+
findAllPages,
9+
LOCALES,
10+
pathWithoutLocaleReducer
11+
} = require("./find-all-pages");
612

713
const BASE_URL = "https://hyperjump.tech";
814
// Next.js export directory
915
const OUTPUT_DIR = join(process.cwd(), "out");
10-
const LOCALES = ["en", "id"];
11-
const EXCLUDED_PAGES = ["_not-found", "404"];
1216

1317
await (async function generateSitemap() {
1418
if (!existsSync(OUTPUT_DIR)) {
1519
console.error("❌ Output directory 'out' not found.");
1620
return;
1721
}
1822

19-
const sitemapUrls = findAllPages(OUTPUT_DIR)
23+
const paths = findAllPages(OUTPUT_DIR)
2024
.filter((path) => !EXCLUDED_PAGES.includes(path))
21-
.filter(removePathWithLocale)
22-
.sort()
23-
.map((path) => generateSitemapURL(path).join("\n "));
25+
.reduce(pathWithoutLocaleReducer, [])
26+
.sort();
27+
const sitemapUrls = paths.map((path) =>
28+
generateSitemapURL(path).join("\n ")
29+
);
2430

2531
await writeFile(
2632
join(OUTPUT_DIR, "sitemap.xml"),
@@ -29,17 +35,12 @@ await (async function generateSitemap() {
2935
}),
3036
"utf8"
3137
);
32-
console.log("✅ Sitemap generated");
33-
})();
3438

35-
function removePathWithLocale(path) {
36-
for (const locale of LOCALES) {
37-
if (path.startsWith(locale)) {
38-
return false;
39-
}
39+
for (const path of paths) {
40+
console.info(`Generated sitemap: /${path}`);
4041
}
41-
return true;
42-
}
42+
console.info("✅ Sitemap generated");
43+
})();
4344

4445
function generateSitemapURL(path) {
4546
return LOCALES.map(
@@ -52,10 +53,6 @@ function generateSitemapURL(path) {
5253
);
5354
}
5455

55-
function clean(path) {
56-
return path ? `/${path}` : "";
57-
}
58-
5956
function alternateLinks(path) {
6057
return LOCALES.map((locale) =>
6158
`<xhtml:link rel="alternate" hreflang="${locale}" href="${BASE_URL}/${locale}${clean(path)}"/>`.trim()

0 commit comments

Comments
 (0)