|
| 1 | +/* |
| 2 | + * Copyright 2023 Exactpro (Exactpro Systems Limited) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +const keysToTemplates = { |
| 18 | + UserAgent: (input: string) => `User-agent: ${input}`, |
| 19 | + CrawlDelay: (input: string) => `Crawl-delay: ${input}`, |
| 20 | + Disallow: (input: string) => `Disallow: ${input}`, |
| 21 | + Allow: (input: string) => `Allow: ${input}`, |
| 22 | + Host: (input: string) => `Host: ${input}`, |
| 23 | + Sitemap: (input: string) => `Sitemap: ${input}`, |
| 24 | + CleanParam: (input: string) => `Clean-param: ${input}`, |
| 25 | + Comment: (input: string) => `# ${input}` |
| 26 | +} |
| 27 | + |
| 28 | +export type RobotsTxtOptions = { |
| 29 | + [key in keyof typeof keysToTemplates]?: string |
| 30 | +} & { |
| 31 | + BlankLine?: boolean |
| 32 | +} |
| 33 | + |
| 34 | +export default defineEventHandler((event) => { |
| 35 | + setHeader(event, 'Content-Type', 'text/plain') |
| 36 | + const lines: string[] = [] |
| 37 | + const config = useAppConfig() |
| 38 | + for (const robotOptionsItem of config.exactproDocs?.seo?.robots ?? []) { |
| 39 | + for (const [key, value] of Object.entries(robotOptionsItem)) { |
| 40 | + if (typeof value === 'string') { |
| 41 | + lines.push(keysToTemplates[key as keyof typeof keysToTemplates](value)) |
| 42 | + } |
| 43 | + // If BlankLine is true, add blank line |
| 44 | + if (typeof value === 'boolean' && value) { |
| 45 | + lines.push('') |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + if (config.exactproDocs?.seo?.sitemap?.baseUrl) { |
| 50 | + lines.push('') |
| 51 | + lines.push( |
| 52 | + `Sitemap: ${config.exactproDocs.seo.sitemap.baseUrl}/sitemap.xml` |
| 53 | + ) |
| 54 | + } |
| 55 | + return lines.join('\n') |
| 56 | +}) |
0 commit comments