Skip to content

Commit 36985d4

Browse files
committed
Release v1.2.0
1 parent f54766b commit 36985d4

31 files changed

+324
-194
lines changed

lib/node/plugins/helpers.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/node/plugins/helpers.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/node/plugins/manager.d.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export interface ManagerContract {
2+
key: string;
3+
repository: string;
4+
text: string;
5+
splitter: string;
6+
forceSplitter: boolean;
7+
patterns: Array<RegExp>;
8+
formatValue: string;
9+
formatLink: string;
10+
formatReplaces?: object;
11+
asCode: boolean;
12+
setRepository(name: string): Manager;
13+
setKey(key: string): Manager;
14+
setSplitter(value: string, force: boolean): Manager;
15+
setText(text: string): Manager;
16+
setCompactPatterns(patterns: RegExp | Array<RegExp>): Manager;
17+
setExpandFormat(value: string, link?: string): Manager;
18+
setExpandValueReplaces(values: object): Manager;
19+
setAsCode(): Manager;
20+
compact(): string;
21+
expand(): string;
22+
}
23+
export declare class Manager implements ManagerContract {
24+
key: string;
25+
repository: string;
26+
text: string;
27+
splitter: string;
28+
forceSplitter: boolean;
29+
patterns: Array<RegExp>;
30+
formatValue: string;
31+
formatLink: string;
32+
formatReplaces?: object;
33+
asCode: boolean;
34+
static create(): Manager;
35+
setRepository(name: string): Manager;
36+
setKey(key: string): Manager;
37+
setSplitter(value: string, force?: boolean): Manager;
38+
setText(text: string): Manager;
39+
setCompactPatterns(patterns: RegExp | Array<RegExp>): Manager;
40+
setExpandFormat(value: string, link?: string): Manager;
41+
setExpandValueReplaces(values: object): Manager;
42+
setAsCode(): Manager;
43+
compact(): string;
44+
expand(): string;
45+
private resolveReplacer;
46+
}

lib/node/plugins/manager.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { Replacer } from './replacer';
2+
export class Manager {
3+
constructor() {
4+
this.key = 'key';
5+
this.repository = '';
6+
this.text = '';
7+
this.splitter = '#';
8+
this.forceSplitter = false;
9+
this.patterns = [];
10+
this.formatValue = '$1/$key/$2';
11+
this.formatLink = '$1/$key/$2';
12+
this.asCode = false;
13+
}
14+
static create() {
15+
return new Manager();
16+
}
17+
setRepository(name) {
18+
this.repository = name.replace('https://github.com/', '');
19+
return this;
20+
}
21+
setKey(key) {
22+
this.key = key;
23+
return this;
24+
}
25+
setSplitter(value, force = false) {
26+
this.splitter = value;
27+
this.forceSplitter = force;
28+
return this;
29+
}
30+
setText(text) {
31+
this.text = text;
32+
return this;
33+
}
34+
setCompactPatterns(patterns) {
35+
this.patterns = Array.isArray(patterns) ? patterns : [patterns];
36+
return this;
37+
}
38+
setExpandFormat(value, link) {
39+
this.formatValue = value;
40+
this.formatLink = link || value;
41+
return this;
42+
}
43+
setExpandValueReplaces(values) {
44+
this.formatReplaces = values;
45+
return this;
46+
}
47+
setAsCode() {
48+
this.asCode = true;
49+
return this;
50+
}
51+
compact() {
52+
return this.resolveReplacer().compact();
53+
}
54+
expand() {
55+
return this.resolveReplacer().expand();
56+
}
57+
resolveReplacer() {
58+
return Replacer.create(this);
59+
}
60+
}

lib/node/plugins/regex.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/node/plugins/regex.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

lib/node/plugins/replacer.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { ManagerContract } from './manager';
2+
export declare class Replacer {
3+
private manager;
4+
private block;
5+
constructor(manager: ManagerContract);
6+
static create(manager: ManagerContract): Replacer;
7+
compact(): string;
8+
expand(): string;
9+
private regex;
10+
private template;
11+
private url;
12+
private replace;
13+
private getRepository;
14+
private getBlock;
15+
private getKey;
16+
private getPatterns;
17+
private getFormatValue;
18+
private getFormatLink;
19+
private getSplitter;
20+
private hasForceSplitter;
21+
private getExpandReplaces;
22+
private getExpandPattern;
23+
private asCode;
24+
}

lib/node/plugins/replacer.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
export class Replacer {
2+
constructor(manager) {
3+
this.block = ':';
4+
this.manager = manager;
5+
}
6+
static create(manager) {
7+
return new Replacer(manager);
8+
}
9+
compact() {
10+
this.regex(this.getPatterns(), item => this.replace(item, this.template(item)));
11+
return this.manager.text;
12+
}
13+
expand() {
14+
this.regex(this.getExpandPattern(), item => this.replace(item, this.url(item)));
15+
return this.manager.text;
16+
}
17+
regex(patterns, callback) {
18+
Array.from(patterns, (pattern) => {
19+
const matches = this.manager.text.matchAll(pattern);
20+
Array.from(matches).reverse().forEach(item => this.manager.text = callback(item));
21+
});
22+
}
23+
template(match) {
24+
let args = match[3] === undefined
25+
? [this.getRepository(), match[1], match[2]]
26+
: [`${match[1]}/${match[2]}`, match[3], match[4]];
27+
return this.getBlock() + [this.getKey()].concat(args.filter(val => !!val)).join(this.getBlock()) + this.getBlock();
28+
}
29+
url(match) {
30+
let link = this.getFormatLink();
31+
let value = this.getFormatValue();
32+
const isSameRepository = match[1].includes(this.getRepository());
33+
const replaces = this.getExpandReplaces();
34+
const codePrefix = this.asCode() && isSameRepository ? '<code>' : '';
35+
const codeSuffix = this.asCode() && isSameRepository ? '</code>' : '';
36+
for (let i = 1; i <= 4; i++) {
37+
if (match[i] === undefined) {
38+
break;
39+
}
40+
link = link.replace('$' + i, match[i]);
41+
value = replaces !== undefined && replaces[i] !== undefined
42+
? value.replace('$' + i, replaces[i](match[i]))
43+
: value.replace('$' + i, match[i]);
44+
}
45+
link = link.replace('https://github.com/', '').replace('$key', this.getKey());
46+
value = isSameRepository
47+
? value.replace(this.getRepository(), '').replace('/$key/', this.hasForceSplitter() ? this.getSplitter() : '')
48+
: value.replace('/$key/', this.getSplitter());
49+
return `<a href="https://github.com/${link}" target="_blank" rel="noopener noreferrer">${codePrefix}${value}${codeSuffix}<ExternalLinkIcon /></a>`;
50+
}
51+
replace(match, to) {
52+
// return this.manager.text.replace(match[0], to)
53+
const index = match['index'];
54+
const from = match[0];
55+
return this.manager.text.slice(0, index) + to + this.manager.text.slice(index + from.length);
56+
}
57+
getRepository() {
58+
return this.manager.repository;
59+
}
60+
getBlock() {
61+
return this.block.repeat(2);
62+
}
63+
getKey() {
64+
return this.manager.key;
65+
}
66+
getPatterns() {
67+
return this.manager.patterns;
68+
}
69+
getFormatValue() {
70+
return this.manager.formatValue;
71+
}
72+
getFormatLink() {
73+
return this.manager.formatLink;
74+
}
75+
getSplitter() {
76+
return this.manager.splitter;
77+
}
78+
hasForceSplitter() {
79+
return this.manager.forceSplitter;
80+
}
81+
getExpandReplaces() {
82+
return this.manager.formatReplaces;
83+
}
84+
getExpandPattern() {
85+
return [new RegExp(`${this.block}{2}${this.getKey()}${this.block}{2}` +
86+
`([\\w\\d\\/.\\-_]+)${this.block}{2}` +
87+
`([\\w\\d\\/.\\-_]+)${this.block}{2}` +
88+
`([\\w\\d\\/.\\-_]+)?${this.block}{0,2}`, 'g')];
89+
}
90+
asCode() {
91+
return this.manager.asCode;
92+
}
93+
}

lib/node/plugins/template.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

lib/node/plugins/template.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)