Skip to content

Commit ee973f1

Browse files
authoredOct 17, 2024··
fix: comments being treated as errors (#152)
1 parent c895897 commit ee973f1

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed
 

Diff for: ‎src/runtime/util.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ export function parseRobotsTxt(s: string): ParsedRobotsTxt {
3131
}
3232
let ln = -1
3333
// read the contents
34-
for (const line of s.split('\n')) {
34+
for (const _line of s.split('\n')) {
3535
ln++
36+
const [line, comment] = _line.split('#').map(s => s.trim())
3637
const sepIndex = line.indexOf(':')
3738
// may not exist for comments
3839
if (sepIndex === -1)

Diff for: ‎test/fixtures/comments.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# line comment
2+
3+
User-agent: * # 1st inline comment
4+
Allow: / # 2nd inline comment

Diff for: ‎test/unit/robotsTxtParser.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,26 @@ Unknown: /bar
355355
]
356356
`)
357357
})
358+
359+
it('comments do not error', async () => {
360+
const robotsTxt = await fsp.readFile('./test/fixtures/comments.txt', { encoding: 'utf-8' })
361+
expect(parseRobotsTxt(robotsTxt)).toMatchInlineSnapshot(`
362+
{
363+
"errors": [],
364+
"groups": [
365+
{
366+
"allow": [
367+
"/",
368+
],
369+
"comment": [],
370+
"disallow": [],
371+
"userAgent": [
372+
"*",
373+
],
374+
},
375+
],
376+
"sitemaps": [],
377+
}
378+
`)
379+
})
358380
})

0 commit comments

Comments
 (0)
Please sign in to comment.