-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from TheDragonCode/1.x
Upgrade project structure
- Loading branch information
Showing
17 changed files
with
389 additions
and
227 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { Replacer } from './replacer' | ||
|
||
export interface ManagerContract | ||
{ | ||
key: string | ||
repository: string | ||
text: string | ||
|
||
splitter: string | ||
forceSplitter: boolean | ||
|
||
patterns: Array<RegExp> | ||
|
||
formatValue: string | ||
formatLink: string | ||
formatReplaces?: object | ||
|
||
asCode: boolean | ||
|
||
setRepository(name: string): Manager | ||
|
||
setKey(key: string): Manager | ||
|
||
setSplitter(value: string, force: boolean): Manager | ||
|
||
setText(text: string): Manager | ||
|
||
setCompactPatterns(patterns: RegExp | Array<RegExp>): Manager | ||
|
||
setExpandFormat(value: string, link?: string): Manager | ||
|
||
setExpandValueReplaces(values: object): Manager | ||
|
||
setAsCode(): Manager | ||
|
||
compact(): string | ||
|
||
expand(): string | ||
} | ||
|
||
export class Manager implements ManagerContract | ||
{ | ||
public key: string = 'key' | ||
public repository: string = '' | ||
public text: string = '' | ||
|
||
public splitter: string = '#' | ||
public forceSplitter: boolean = false | ||
|
||
public patterns: Array<RegExp> = [] | ||
|
||
public formatValue: string = '$1/$key/$2' | ||
public formatLink: string = '$1/$key/$2' | ||
public formatReplaces?: object | ||
|
||
public asCode: boolean = false | ||
|
||
static create(): Manager | ||
{ | ||
return new Manager() | ||
} | ||
|
||
setRepository(name: string): Manager | ||
{ | ||
this.repository = name.replace('https://github.com/', '') | ||
|
||
return this | ||
} | ||
|
||
setKey(key: string): Manager | ||
{ | ||
this.key = key | ||
|
||
return this | ||
} | ||
|
||
setSplitter(value: string, force: boolean = false): Manager | ||
{ | ||
this.splitter = value | ||
this.forceSplitter = force | ||
|
||
return this | ||
} | ||
|
||
setText(text: string): Manager | ||
{ | ||
this.text = text | ||
|
||
return this | ||
} | ||
|
||
setCompactPatterns(patterns: RegExp | Array<RegExp>): Manager | ||
{ | ||
this.patterns = Array.isArray(patterns) ? patterns : [patterns] | ||
|
||
return this | ||
} | ||
|
||
setExpandFormat(value: string, link?: string): Manager | ||
{ | ||
this.formatValue = value | ||
this.formatLink = link || value | ||
|
||
return this | ||
} | ||
|
||
setExpandValueReplaces(values: object): Manager | ||
{ | ||
this.formatReplaces = values | ||
|
||
return this | ||
} | ||
|
||
setAsCode(): Manager | ||
{ | ||
this.asCode = true | ||
|
||
return this | ||
} | ||
|
||
compact(): string | ||
{ | ||
return this.resolveReplacer().compact() | ||
} | ||
|
||
expand(): string | ||
{ | ||
return this.resolveReplacer().expand() | ||
} | ||
|
||
private resolveReplacer(): Replacer | ||
{ | ||
return Replacer.create(this) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import type { ManagerContract } from './manager' | ||
|
||
export class Replacer | ||
{ | ||
private manager: ManagerContract | ||
private block: string = ':' | ||
|
||
constructor(manager: ManagerContract) | ||
{ | ||
this.manager = manager | ||
} | ||
|
||
static create(manager: ManagerContract): Replacer | ||
{ | ||
return new Replacer(manager) | ||
} | ||
|
||
compact(): string | ||
{ | ||
this.regex(this.getPatterns(), item => this.replace(item, this.template(item))) | ||
|
||
return this.manager.text | ||
} | ||
|
||
expand(): string | ||
{ | ||
this.regex(this.getExpandPattern(), item => this.replace(item, this.url(item))) | ||
|
||
return this.manager.text | ||
} | ||
|
||
private regex(patterns: Array<RegExp>, callback): void | ||
{ | ||
Array.from(patterns, (pattern: RegExp) => { | ||
const matches = this.manager.text.matchAll(pattern) | ||
|
||
Array.from(matches).reverse().forEach( | ||
item => this.manager.text = callback(item) | ||
) | ||
}) | ||
} | ||
|
||
private template(match: Array<string>): string | ||
{ | ||
let args = match[3] === undefined | ||
? [this.getRepository(), match[1], match[2]] | ||
: [`${ match[1] }/${ match[2] }`, match[3], match[4]] | ||
|
||
return this.getBlock() + [this.getKey()].concat( | ||
args.filter(val => !! val) | ||
).join(this.getBlock()) + this.getBlock() | ||
} | ||
|
||
private url(match: Array<string>): string | ||
{ | ||
let link: string = this.getFormatLink() | ||
let value: string = this.getFormatValue() | ||
|
||
const isSameRepository = match[1].includes(this.getRepository()) | ||
|
||
const replaces = this.getExpandReplaces() | ||
|
||
const codePrefix: string = this.asCode() && isSameRepository ? '<code>' : '' | ||
const codeSuffix: string = this.asCode() && isSameRepository ? '</code>' : '' | ||
|
||
for (let i = 1; i <= 4; i++) { | ||
if (match[i] === undefined) { | ||
break | ||
} | ||
|
||
link = link.replace('$' + i, match[i]) | ||
|
||
value = replaces !== undefined && replaces[i] !== undefined | ||
? value.replace('$' + i, replaces[i](match[i])) | ||
: value.replace('$' + i, match[i]) | ||
} | ||
|
||
link = link.replace('https://github.com/', '').replace('$key', this.getKey()) | ||
|
||
value = isSameRepository | ||
? value.replace(this.getRepository(), '').replace('/$key/', this.hasForceSplitter() ? this.getSplitter() : '') | ||
: value.replace('/$key/', this.getSplitter()) | ||
|
||
return `<a href="https://github.com/${ link }" target="_blank" rel="noopener noreferrer">${ codePrefix }${ value }${ codeSuffix }<ExternalLinkIcon /></a>` | ||
} | ||
|
||
private replace(match: Array<string>, to: string): string | ||
{ | ||
// return this.manager.text.replace(match[0], to) | ||
const index: number = match['index'] | ||
const from: string = match[0] | ||
|
||
return this.manager.text.slice(0, index) + to + this.manager.text.slice(index + from.length) | ||
} | ||
|
||
private getRepository(): string | ||
{ | ||
return this.manager.repository | ||
} | ||
|
||
private getBlock(): string | ||
{ | ||
return this.block.repeat(2) | ||
} | ||
|
||
private getKey(): string | ||
{ | ||
return this.manager.key | ||
} | ||
|
||
private getPatterns(): Array<RegExp> | ||
{ | ||
return this.manager.patterns | ||
} | ||
|
||
private getFormatValue(): string | ||
{ | ||
return this.manager.formatValue | ||
} | ||
|
||
private getFormatLink(): string | ||
{ | ||
return this.manager.formatLink | ||
} | ||
|
||
private getSplitter(): string | ||
{ | ||
return this.manager.splitter | ||
} | ||
|
||
private hasForceSplitter(): boolean | ||
{ | ||
return this.manager.forceSplitter | ||
} | ||
|
||
private getExpandReplaces(): object | undefined | ||
{ | ||
return this.manager.formatReplaces | ||
} | ||
|
||
private getExpandPattern(): Array<RegExp> | ||
{ | ||
return [new RegExp( | ||
`${ this.block }{2}${ this.getKey() }${ this.block }{2}` + | ||
`([\\w\\d\\/.\\-_]+)${ this.block }{2}` + | ||
`([\\w\\d\\/.\\-_]+)${ this.block }{2}` + | ||
`([\\w\\d\\/.\\-_]+)?${ this.block }{0,2}`, | ||
'g' | ||
)] | ||
} | ||
|
||
private asCode(): boolean | ||
{ | ||
return this.manager.asCode | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,8 @@ | ||
import type { GitHubLinkifyTransformer } from '../../types/transformer.js' | ||
import { replace } from '../helpers.js' | ||
import { template } from '../template.js' | ||
import { regex } from '../regex' | ||
import { url } from '../url' | ||
|
||
export const blobsCompact: GitHubLinkifyTransformer = (text: string, repo: string) => { | ||
const replacer = (value, item) => replace(value, item, template('blob', `${ item[1] }/${ item[2] }`, item[3])) | ||
|
||
text = regex(text, /\[[\w\d\s`]+]\(https:\/\/github\.com\/([\w\d\-_]+)\/([\w\d\-_]+)\/blob\/([\w\d\/\.\-_]+)\)/g, replacer) | ||
text = regex(text, /https:\/\/github\.com\/([\w\d\-_]+)\/([\w\d\-_]+)\/blob\/([\w\d\/\.\-_]+)/g, replacer) | ||
|
||
return text | ||
} | ||
|
||
export const blobsExpand: GitHubLinkifyTransformer = (text: string, repo: string) => { | ||
const replacer = (value, item) => value.replace(item[0], url(repo, `${ item[1].includes(repo) ? '' : item[1] + '#' }${ item[2] }`, `${ item[1] }/blob/${ item[2] }`)) | ||
|
||
text = regex(text, /::blob::([\w\d\-_\/]+)::([\w\d\/\.\-_]+)::/g, replacer) | ||
|
||
return text | ||
} | ||
import { Manager } from '../manager' | ||
|
||
export const blobsTransformer = Manager.create() | ||
.setKey('blob') | ||
.setCompactPatterns([ | ||
/\[[\w\d\s`]+]\(https:\/\/github\.com\/([\w\d\-_]+)\/([\w\d\-_]+)\/blob\/([\w\d\/.\-_]+)\)/g, | ||
/https:\/\/github\.com\/([\w\d\-_]+)\/([\w\d\-_]+)\/blob\/([\w\d\/.\-_]+)/g | ||
]) |
Oops, something went wrong.