Skip to content

Instantly share code, notes, and snippets.

@axayjha
Created May 16, 2025 14:57
Show Gist options
  • Save axayjha/6b27e9be96e8f0ee7664372c2a0c42f6 to your computer and use it in GitHub Desktop.
Save axayjha/6b27e9be96e8f0ee7664372c2a0c42f6 to your computer and use it in GitHub Desktop.
q25_new
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