|
| 1 | +import type { ManagerContract } from './manager' |
| 2 | + |
| 3 | +export class Replacer |
| 4 | +{ |
| 5 | + private manager: ManagerContract |
| 6 | + private block: string = ':' |
| 7 | + |
| 8 | + constructor(manager: ManagerContract) |
| 9 | + { |
| 10 | + this.manager = manager |
| 11 | + } |
| 12 | + |
| 13 | + static create(manager: ManagerContract): Replacer |
| 14 | + { |
| 15 | + return new Replacer(manager) |
| 16 | + } |
| 17 | + |
| 18 | + compact(): string |
| 19 | + { |
| 20 | + this.regex(this.getPatterns(), item => this.replace(item, this.template(item))) |
| 21 | + |
| 22 | + return this.manager.text |
| 23 | + } |
| 24 | + |
| 25 | + expand(): string |
| 26 | + { |
| 27 | + this.regex(this.getExpandPattern(), item => this.replace(item, this.url(item))) |
| 28 | + |
| 29 | + return this.manager.text |
| 30 | + } |
| 31 | + |
| 32 | + private regex(patterns: Array<RegExp>, callback): void |
| 33 | + { |
| 34 | + Array.from(patterns, (pattern: RegExp) => { |
| 35 | + const matches = this.manager.text.matchAll(pattern) |
| 36 | + |
| 37 | + Array.from(matches).reverse().forEach( |
| 38 | + item => this.manager.text = callback(item) |
| 39 | + ) |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + private template(match: Array<string>): string |
| 44 | + { |
| 45 | + let args = match[3] === undefined |
| 46 | + ? [this.getRepository(), match[1], match[2]] |
| 47 | + : [`${ match[1] }/${ match[2] }`, match[3], match[4]] |
| 48 | + |
| 49 | + return this.getBlock() + [this.getKey()].concat( |
| 50 | + args.filter(val => !! val) |
| 51 | + ).join(this.getBlock()) + this.getBlock() |
| 52 | + } |
| 53 | + |
| 54 | + private url(match: Array<string>): string |
| 55 | + { |
| 56 | + let link: string = this.getFormatLink() |
| 57 | + let value: string = this.getFormatValue() |
| 58 | + |
| 59 | + const isSameRepository = match[1].includes(this.getRepository()) |
| 60 | + |
| 61 | + const replaces = this.getExpandReplaces() |
| 62 | + |
| 63 | + const codePrefix: string = this.asCode() && isSameRepository ? '<code>' : '' |
| 64 | + const codeSuffix: string = this.asCode() && isSameRepository ? '</code>' : '' |
| 65 | + |
| 66 | + for (let i = 1; i <= 4; i++) { |
| 67 | + if (match[i] === undefined) { |
| 68 | + break |
| 69 | + } |
| 70 | + |
| 71 | + link = link.replace('$' + i, match[i]) |
| 72 | + |
| 73 | + value = replaces !== undefined && replaces[i] !== undefined |
| 74 | + ? value.replace('$' + i, replaces[i](match[i])) |
| 75 | + : value.replace('$' + i, match[i]) |
| 76 | + } |
| 77 | + |
| 78 | + link = link.replace('https://github.com/', '').replace('$key', this.getKey()) |
| 79 | + |
| 80 | + value = isSameRepository |
| 81 | + ? value.replace(this.getRepository(), '').replace('/$key/', this.hasForceSplitter() ? this.getSplitter() : '') |
| 82 | + : value.replace('/$key/', this.getSplitter()) |
| 83 | + |
| 84 | + return `<a href="https://github.com/${ link }" target="_blank" rel="noopener noreferrer">${ codePrefix }${ value }${ codeSuffix }<ExternalLinkIcon /></a>` |
| 85 | + } |
| 86 | + |
| 87 | + private replace(match: Array<string>, to: string): string |
| 88 | + { |
| 89 | + // return this.manager.text.replace(match[0], to) |
| 90 | + const index: number = match['index'] |
| 91 | + const from: string = match[0] |
| 92 | + |
| 93 | + return this.manager.text.slice(0, index) + to + this.manager.text.slice(index + from.length) |
| 94 | + } |
| 95 | + |
| 96 | + private getRepository(): string |
| 97 | + { |
| 98 | + return this.manager.repository |
| 99 | + } |
| 100 | + |
| 101 | + private getBlock(): string |
| 102 | + { |
| 103 | + return this.block.repeat(2) |
| 104 | + } |
| 105 | + |
| 106 | + private getKey(): string |
| 107 | + { |
| 108 | + return this.manager.key |
| 109 | + } |
| 110 | + |
| 111 | + private getPatterns(): Array<RegExp> |
| 112 | + { |
| 113 | + return this.manager.patterns |
| 114 | + } |
| 115 | + |
| 116 | + private getFormatValue(): string |
| 117 | + { |
| 118 | + return this.manager.formatValue |
| 119 | + } |
| 120 | + |
| 121 | + private getFormatLink(): string |
| 122 | + { |
| 123 | + return this.manager.formatLink |
| 124 | + } |
| 125 | + |
| 126 | + private getSplitter(): string |
| 127 | + { |
| 128 | + return this.manager.splitter |
| 129 | + } |
| 130 | + |
| 131 | + private hasForceSplitter(): boolean |
| 132 | + { |
| 133 | + return this.manager.forceSplitter |
| 134 | + } |
| 135 | + |
| 136 | + private getExpandReplaces(): object | undefined |
| 137 | + { |
| 138 | + return this.manager.formatReplaces |
| 139 | + } |
| 140 | + |
| 141 | + private getExpandPattern(): Array<RegExp> |
| 142 | + { |
| 143 | + return [new RegExp( |
| 144 | + `${ this.block }{2}${ this.getKey() }${ this.block }{2}` + |
| 145 | + `([\\w\\d\\/.\\-_]+)${ this.block }{2}` + |
| 146 | + `([\\w\\d\\/.\\-_]+)${ this.block }{2}` + |
| 147 | + `([\\w\\d\\/.\\-_]+)?${ this.block }{0,2}`, |
| 148 | + 'g' |
| 149 | + )] |
| 150 | + } |
| 151 | + |
| 152 | + private asCode(): boolean |
| 153 | + { |
| 154 | + return this.manager.asCode |
| 155 | + } |
| 156 | +} |
0 commit comments