(text: string, options: SitesFixesParserOptions<T>)
| 22 | export type SiteFixesIndex = URLTrie<[number, number]>; |
| 23 | |
| 24 | export function parseSitesFixesConfig<T extends SiteProps>(text: string, options: SitesFixesParserOptions<T>): T[] { |
| 25 | const sites: T[] = []; |
| 26 | |
| 27 | const blocks = text.replace(/\r/g, '').split(/^\s*={2,}\s*$/gm); |
| 28 | blocks.forEach((block) => { |
| 29 | const lines = block.split('\n'); |
| 30 | const commandIndices: number[] = []; |
| 31 | lines.forEach((ln, i) => { |
| 32 | if (ln.match(/^[A-Z]+(\s[A-Z]+){0,2}$/)) { |
| 33 | commandIndices.push(i); |
| 34 | } |
| 35 | }); |
| 36 | |
| 37 | if (commandIndices.length === 0) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | const siteFix = { |
| 42 | url: parseArray(lines.slice(0, commandIndices[0]).join('\n')) as readonly string[], |
| 43 | } as T; |
| 44 | |
| 45 | commandIndices.forEach((commandIndex, i) => { |
| 46 | const command = lines[commandIndex].trim(); |
| 47 | const valueText = lines.slice(commandIndex + 1, i === commandIndices.length - 1 ? lines.length : commandIndices[i + 1]).join('\n'); |
| 48 | const prop = options.getCommandPropName(command); |
| 49 | if (!prop) { |
| 50 | return; |
| 51 | } |
| 52 | const value = options.parseCommandValue(command, valueText); |
| 53 | siteFix[prop] = value; |
| 54 | }); |
| 55 | |
| 56 | sites.push(siteFix); |
| 57 | }); |
| 58 | |
| 59 | return sites; |
| 60 | } |
| 61 | |
| 62 | // URL patterns are guaranteed to not have protocol and leading '/' |
| 63 | export function getDomain(url: string): string { |
no test coverage detected