Skip to content

Commit

Permalink
feat: add blank lines and comments (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored Oct 31, 2022
1 parent 4069ab8 commit 0cdf4ee
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 30 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,25 @@ export default {
If you need to use function in any rule, you need to create a config file through the `configPath` option

```js
export default {
UserAgent: '*',
Disallow: '/',
export default [
{ UserAgent: '*' },
{ Disallow: '/' },
{ BlankLine: true },
{ Comment: 'Comment here' },

// Be aware that this will NOT work on target: 'static' mode
Sitemap: (req) => `https://${req.headers.host}/sitemap.xml`
}
{ Sitemap: (req) => `https://${req.headers.host}/sitemap.xml` }
]
```

output:

```txt
User-agent: *
Disallow: /
# Comment here
Sitemap: https://robots.nuxtjs.org/sitemap.xml
```

### The keys and values available:
Expand All @@ -92,6 +104,8 @@ export default {
- Host = `Host`
- Sitemap = `Sitemap`
- CleanParam = `Clean-param`
- Comment = `# Comment`
- BlankLine = `Add blank line`

**Note:** Don't worry, keys are parsed with case insensitivity and special characters.

Expand Down
40 changes: 35 additions & 5 deletions src/runtime/server/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
import { defineEventHandler, setHeader } from 'h3'
import { RuleInterface, RuleValue, Rule } from '../../types'
import { defineEventHandler, NodeIncomingMessage, setHeader } from 'h3'
import { RuleValue, Rule } from '../../types'
import robots from '#robots'

export default defineEventHandler(async (event) => {
setHeader(event, 'Content-Type', 'text/plain')
return render(await getRules(robots, event.req))
})

enum Correspondence {
'User-agent',
'Crawl-delay',
'Disallow',
'Allow',
'Host',
'Sitemap',
'Clean-param',
'Comment',
'BlankLine'
}

interface RuleInterface {
key: Correspondence
value: string
}

function render (rules: RuleInterface[]) {
return rules.map(rule => `${rule.key}: ${String(rule.value).trim()}`).join('\n')
return rules.map((rule) => {
const value = String(rule.value).trim()

switch (rule.key.toString()) {
case Correspondence[Correspondence.Comment]:
return `# ${value}`
case Correspondence[Correspondence.BlankLine]:
return ''
default:
return `${rule.key}: ${value}`
}
}).join('\n')
}

async function getRules (options: Rule | Rule[], req = null) {
async function getRules (options: Rule | Rule[], req: NodeIncomingMessage) {
const correspondences = {
useragent: 'User-agent',
crawldelay: 'Crawl-delay',
disallow: 'Disallow',
allow: 'Allow',
host: 'Host',
sitemap: 'Sitemap',
cleanparam: 'Clean-param'
cleanparam: 'Clean-param',
comment: 'Comment',
blankline: 'BlankLine'
}

const rules: RuleInterface[] = []
Expand Down
15 changes: 0 additions & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
export enum Correspondence {
'User-agent',
'Crawl-delay',
'Disallow',
'Allow',
'Host',
'Sitemap',
'Clean-param'
}

export interface RuleInterface {
key: Correspondence
value: string
}

export type RuleValue = string | boolean | Function | (string | boolean | Function)[]

export type Rule = {
Expand Down
2 changes: 1 addition & 1 deletion test/config-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ describe('config file', async () => {

test('render', async () => {
const body = await $fetch('/robots.txt')
expect(body).toBe('User-agent: Googlebot\nUser-agent: Bingbot\nDisallow: /admin')
expect(body).toBe('User-agent: Googlebot\nUser-agent: Bingbot\n# Comment here\n\nDisallow: /admin')
})
})
10 changes: 6 additions & 4 deletions test/fixture/config-file/robots.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
UserAgent: () => ['Googlebot', () => 'Bingbot'],
Disallow: '/admin'
}
export default [
{ UserAgent: () => ['Googlebot', () => 'Bingbot'] },
{ Comment: 'Comment here' },
{ BlankLine: true },
{ Disallow: '/admin' }
]

0 comments on commit 0cdf4ee

Please sign in to comment.