|
| 1 | +const SITE_URL = 'https://apisix.apache.org'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Docusaurus plugin that injects hreflang <link> tags and canonical URLs |
| 5 | + * into every HTML page during the post-build phase. |
| 6 | + * |
| 7 | + * For each page, it generates: |
| 8 | + * <link rel="alternate" hreflang="en" href="https://apisix.apache.org/..." /> |
| 9 | + * <link rel="alternate" hreflang="zh" href="https://apisix.apache.org/zh/..." /> |
| 10 | + * <link rel="alternate" hreflang="x-default" href="https://apisix.apache.org/..." /> |
| 11 | + * <link rel="canonical" href="..." /> (if not already present) |
| 12 | + */ |
| 13 | +module.exports = function hreflangPlugin() { |
| 14 | + return { |
| 15 | + name: 'hreflang', |
| 16 | + |
| 17 | + async postBuild({ outDir }) { |
| 18 | + const fs = require('fs'); |
| 19 | + const path = require('path'); |
| 20 | + |
| 21 | + function findHtmlFiles(dir) { |
| 22 | + const results = []; |
| 23 | + const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 24 | + for (const entry of entries) { |
| 25 | + const fullPath = path.join(dir, entry.name); |
| 26 | + if (entry.isDirectory()) { |
| 27 | + results.push(...findHtmlFiles(fullPath)); |
| 28 | + } else if (entry.name.endsWith('.html')) { |
| 29 | + results.push(fullPath); |
| 30 | + } |
| 31 | + } |
| 32 | + return results; |
| 33 | + } |
| 34 | + |
| 35 | + const htmlFiles = findHtmlFiles(outDir); |
| 36 | + |
| 37 | + for (const filePath of htmlFiles) { |
| 38 | + let html = fs.readFileSync(filePath, 'utf-8'); |
| 39 | + |
| 40 | + // Determine relative path from outDir |
| 41 | + const relativePath = path.relative(outDir, filePath); |
| 42 | + |
| 43 | + // Determine the current locale based on file path |
| 44 | + const isZh = relativePath.startsWith('zh' + path.sep); |
| 45 | + |
| 46 | + // Compute the path without locale prefix |
| 47 | + const pathWithoutLocale = isZh |
| 48 | + ? relativePath.slice(3) // remove "zh/" |
| 49 | + : relativePath; |
| 50 | + |
| 51 | + // Normalize path separators for URL |
| 52 | + const urlPath = pathWithoutLocale.split(path.sep).join('/'); |
| 53 | + |
| 54 | + // Build URLs |
| 55 | + const enUrl = `${SITE_URL}/${urlPath}`.replace(/\/index\.html$/, '/'); |
| 56 | + const zhUrl = `${SITE_URL}/zh/${urlPath}`.replace(/\/index\.html$/, '/'); |
| 57 | + const currentUrl = isZh ? zhUrl : enUrl; |
| 58 | + |
| 59 | + // Build hreflang tags |
| 60 | + const hreflangTags = [ |
| 61 | + `<link rel="alternate" hreflang="en" href="${enUrl}" />`, |
| 62 | + `<link rel="alternate" hreflang="zh" href="${zhUrl}" />`, |
| 63 | + `<link rel="alternate" hreflang="x-default" href="${enUrl}" />`, |
| 64 | + ].join('\n '); |
| 65 | + |
| 66 | + // Add canonical tag if not already present |
| 67 | + const hasCanonical = html.includes('rel="canonical"'); |
| 68 | + const canonicalTag = hasCanonical |
| 69 | + ? '' |
| 70 | + : `<link rel="canonical" href="${currentUrl}" />`; |
| 71 | + |
| 72 | + // Inject before </head> |
| 73 | + const injection = [hreflangTags, canonicalTag] |
| 74 | + .filter(Boolean) |
| 75 | + .join('\n '); |
| 76 | + html = html.replace('</head>', ` ${injection}\n </head>`); |
| 77 | + |
| 78 | + fs.writeFileSync(filePath, html, 'utf-8'); |
| 79 | + } |
| 80 | + }, |
| 81 | + }; |
| 82 | +}; |
0 commit comments