Skip to content

Commit b860e08

Browse files
committed
fix: refactor language link tag creation and improve DOM manipulation in HTML rendering
1 parent c8b17f8 commit b860e08

File tree

1 file changed

+19
-45
lines changed

1 file changed

+19
-45
lines changed

src/index.js

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -170,61 +170,35 @@ class CoCreateServerSideRender {
170170
dom = await render(dom, "root");
171171
if (file.langRegion || file.lang) {
172172
dom = await this.translate(dom, file);
173-
// <link rel="alternate" hreflang="x-default" href="https://example.com/en/index.html" language-links>
174-
let isLangLinkTags = dom.querySelector("[language-links]");
175-
if (isLangLinkTags) {
176-
let langLinkTags = this.createLanguageLinkTags(file);
177-
const head = dom.querySelector("head");
178-
if (head && langLinkTags) {
179-
const linksFragment = parse(
180-
`<fragment>${langLinkTags}</fragment>`
181-
);
182-
for (const link of linksFragment.childNodes) {
183-
head.appendChild(link);
184-
}
173+
}
174+
175+
if (file.languages && file.languages.length > 0) {
176+
let langLinkTags = this.createLanguageLinkTags(file);
177+
const head = dom.querySelector("head");
178+
if (head && langLinkTags) {
179+
const linksFragment = parse(langLinkTags);
180+
for (const link of linksFragment.querySelectorAll("link")) {
181+
head.appendChild(link);
182+
}
183+
// Remove the fragment node from the DOM if it exists
184+
if (linksFragment.parentNode) {
185+
linksFragment.remove();
185186
}
186187
}
187188
}
189+
188190
dep = [];
189191
dbCache.clear();
190192
return dom.toString();
191-
}
193+
};
192194

193195
createLanguageLinkTags(file) {
194-
let xDefault = file.path;
196+
let generatedLinksString = `<link rel="alternate" hreflang="x-default" href="https://${file.urlObject.hostname}${file.pathname}">\n`;
195197

196-
if (file.name !== "index.html") {
197-
if (xDefault.endsWith("/")) {
198-
xDefault += file.name;
199-
} else {
200-
xDefault += "/" + file.name;
201-
}
202-
}
203-
let generatedLinksString = `<link rel="alternate" hreflang="x-default" href="${xDefault}">\n`;
204-
205-
// Step 1: Create a lookup object that maps base language to its path.
206-
// This is done once for efficiency.
207-
const paths = {};
208-
for (const p of file.pathname) {
209-
const secondSlashIndex = p.indexOf("/", 1);
210-
const langKey = p.substring(1, secondSlashIndex); // e.g., 'en', 'es', 'pt'
211-
const restOfPath = p.substring(secondSlashIndex);
212-
paths[langKey] = restOfPath;
213-
}
214-
215-
// Step 2: Iterate through all supported languages and build the HTML string.
216198
for (const language of file.languages) {
217-
// Use the base language to find the correct path in our map
218-
const path = paths[language] || paths[language.split("-")[0]];
219-
220-
// If a valid path exists, construct the full link
221-
if (path) {
222-
// Construct the full href URL using the full language code from the array
223-
const hrefUrl = `https://${file.urlObject.hostname}/${language}${path}`;
224-
225-
// Append the HTML string. The hreflang and the URL path are now in sync.
226-
generatedLinksString += `<link rel="alternate" hreflang="${language}" href="${hrefUrl}">\n`;
227-
}
199+
let langPath = `/${language}${file.pathname}`;
200+
const hrefUrl = `https://${file.urlObject.hostname}${langPath}`;
201+
generatedLinksString += `<link rel="alternate" hreflang="${language}" href="${hrefUrl}">\n`;
228202
}
229203
return generatedLinksString;
230204
}

0 commit comments

Comments
 (0)