diff --git a/src/runtime/util.ts b/src/runtime/util.ts index f868f2cb..134bae04 100644 --- a/src/runtime/util.ts +++ b/src/runtime/util.ts @@ -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) diff --git a/test/fixtures/comments.txt b/test/fixtures/comments.txt new file mode 100644 index 00000000..b98a1aa3 --- /dev/null +++ b/test/fixtures/comments.txt @@ -0,0 +1,4 @@ +# line comment + +User-agent: * # 1st inline comment +Allow: / # 2nd inline comment \ No newline at end of file diff --git a/test/unit/robotsTxtParser.test.ts b/test/unit/robotsTxtParser.test.ts index e57047ab..6c1b2b00 100644 --- a/test/unit/robotsTxtParser.test.ts +++ b/test/unit/robotsTxtParser.test.ts @@ -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": [], + } + `) + }) })