Created
May 16, 2025 14:57
-
-
Save axayjha/6b27e9be96e8f0ee7664372c2a0c42f6 to your computer and use it in GitHub Desktop.
q25_new
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseRobotFile(robotLines) { | |
const rulesByAgent = { DoeBot: [], "*": [] }; | |
let currentRelevantAgentKey = null; | |
const UA_PREFIX = "User-agent:"; | |
const DIS_PREFIX = "Disallow:"; | |
for (const rawLine of robotLines) { | |
const line = rawLine.trim(); | |
if (line.startsWith(UA_PREFIX)) { | |
const agentName = line.substring(UA_PREFIX.length).trim(); | |
if (agentName === "DoeBot" || agentName === "*") { | |
currentRelevantAgentKey = agentName; | |
} else { | |
currentRelevantAgentKey = null; | |
} | |
} else if (line.startsWith(DIS_PREFIX) && currentRelevantAgentKey) { | |
let path = line.substring(DIS_PREFIX.length).trim(); | |
const commentIndex = path.indexOf('#'); | |
if (commentIndex !== -1) { | |
path = path.substring(0, commentIndex).trim(); | |
} | |
if (path && !path.includes(' ')) { | |
rulesByAgent[currentRelevantAgentKey].push(path); | |
} | |
} | |
} | |
return [...new Set([...rulesByAgent.DoeBot, ...rulesByAgent["*"]])].sort(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment