Skip to content

Commit

Permalink
fix: comments being treated as errors (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
hacknug authored Oct 17, 2024
1 parent c895897 commit ee973f1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/runtime/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export function parseRobotsTxt(s: string): ParsedRobotsTxt {
}
let ln = -1
// read the contents
for (const line of s.split('\n')) {
for (const _line of s.split('\n')) {
ln++
const [line, comment] = _line.split('#').map(s => s.trim())
const sepIndex = line.indexOf(':')
// may not exist for comments
if (sepIndex === -1)
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/comments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# line comment

User-agent: * # 1st inline comment
Allow: / # 2nd inline comment
22 changes: 22 additions & 0 deletions test/unit/robotsTxtParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,26 @@ Unknown: /bar
]
`)
})

it('comments do not error', async () => {
const robotsTxt = await fsp.readFile('./test/fixtures/comments.txt', { encoding: 'utf-8' })
expect(parseRobotsTxt(robotsTxt)).toMatchInlineSnapshot(`
{
"errors": [],
"groups": [
{
"allow": [
"/",
],
"comment": [],
"disallow": [],
"userAgent": [
"*",
],
},
],
"sitemaps": [],
}
`)
})
})

0 comments on commit ee973f1

Please sign in to comment.