Skip to content

Commit f54766b

Browse files
Merge pull request #6 from TheDragonCode/1.x
Upgrade project structure
2 parents ad5ec2d + 44c500d commit f54766b

File tree

17 files changed

+389
-227
lines changed

17 files changed

+389
-227
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { githubLinkifyPlugin } from 'vuepress-plugin-github-linkify'
2828
}
2929
```
3030

31-
### Support links
31+
### Support Links
3232

3333
* [x] mentions
3434
* [x] commits

index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@
6363
<li>https://github.com/Laravel-Lang/common/blob/v14.6.2/src/Services/Renderer/ParagraphRenderer.php</li>
6464
</ul>
6565

66-
<p>**Full Changelog**: https://github.com/Laravel-Lang/common/compare/12.18.3...12.8.4</p>
66+
<p>**Full Changelog**: https://github.com/Laravel-Lang/common/compare/12.18.3...12.20.4</p>
6767

68-
<p>**Full Changelog**: 12.18.3...12.8.4</p>
68+
<p>**Full Changelog**: 12.18.3...12.20.4</p>
6969
</td>
7070
<td width="50%" valign="top">
7171
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit https://github.com/Laravel-Lang/lang/pull/2275.</p>
@@ -118,9 +118,9 @@
118118
<li>https://github.com/Laravel-Lang/publisher/blob/v14.6.2/src/Services/Renderer/ParagraphRenderer.php</li>
119119
</ul>
120120

121-
<p>**Full Changelog**: https://github.com/Laravel-Lang/lang/compare/12.18.3...12.8.4</p>
121+
<p>**Full Changelog**: https://github.com/Laravel-Lang/lang/compare/12.18.3...12.20.4</p>
122122

123-
<p>**Full Changelog**: 12.18.3...12.8.4</p>
123+
<p>**Full Changelog**: 12.18.3...12.20.4</p>
124124
</td>
125125
</tr>
126126
</table>

src/node/plugins/helpers.ts

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

src/node/plugins/manager.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { Replacer } from './replacer'
2+
3+
export interface ManagerContract
4+
{
5+
key: string
6+
repository: string
7+
text: string
8+
9+
splitter: string
10+
forceSplitter: boolean
11+
12+
patterns: Array<RegExp>
13+
14+
formatValue: string
15+
formatLink: string
16+
formatReplaces?: object
17+
18+
asCode: boolean
19+
20+
setRepository(name: string): Manager
21+
22+
setKey(key: string): Manager
23+
24+
setSplitter(value: string, force: boolean): Manager
25+
26+
setText(text: string): Manager
27+
28+
setCompactPatterns(patterns: RegExp | Array<RegExp>): Manager
29+
30+
setExpandFormat(value: string, link?: string): Manager
31+
32+
setExpandValueReplaces(values: object): Manager
33+
34+
setAsCode(): Manager
35+
36+
compact(): string
37+
38+
expand(): string
39+
}
40+
41+
export class Manager implements ManagerContract
42+
{
43+
public key: string = 'key'
44+
public repository: string = ''
45+
public text: string = ''
46+
47+
public splitter: string = '#'
48+
public forceSplitter: boolean = false
49+
50+
public patterns: Array<RegExp> = []
51+
52+
public formatValue: string = '$1/$key/$2'
53+
public formatLink: string = '$1/$key/$2'
54+
public formatReplaces?: object
55+
56+
public asCode: boolean = false
57+
58+
static create(): Manager
59+
{
60+
return new Manager()
61+
}
62+
63+
setRepository(name: string): Manager
64+
{
65+
this.repository = name.replace('https://github.com/', '')
66+
67+
return this
68+
}
69+
70+
setKey(key: string): Manager
71+
{
72+
this.key = key
73+
74+
return this
75+
}
76+
77+
setSplitter(value: string, force: boolean = false): Manager
78+
{
79+
this.splitter = value
80+
this.forceSplitter = force
81+
82+
return this
83+
}
84+
85+
setText(text: string): Manager
86+
{
87+
this.text = text
88+
89+
return this
90+
}
91+
92+
setCompactPatterns(patterns: RegExp | Array<RegExp>): Manager
93+
{
94+
this.patterns = Array.isArray(patterns) ? patterns : [patterns]
95+
96+
return this
97+
}
98+
99+
setExpandFormat(value: string, link?: string): Manager
100+
{
101+
this.formatValue = value
102+
this.formatLink = link || value
103+
104+
return this
105+
}
106+
107+
setExpandValueReplaces(values: object): Manager
108+
{
109+
this.formatReplaces = values
110+
111+
return this
112+
}
113+
114+
setAsCode(): Manager
115+
{
116+
this.asCode = true
117+
118+
return this
119+
}
120+
121+
compact(): string
122+
{
123+
return this.resolveReplacer().compact()
124+
}
125+
126+
expand(): string
127+
{
128+
return this.resolveReplacer().expand()
129+
}
130+
131+
private resolveReplacer(): Replacer
132+
{
133+
return Replacer.create(this)
134+
}
135+
}

src/node/plugins/regex.ts

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

src/node/plugins/replacer.ts

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
}

src/node/plugins/template.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
1-
import type { GitHubLinkifyTransformer } from '../../types/transformer.js'
2-
import { replace } from '../helpers.js'
3-
import { template } from '../template.js'
4-
import { regex } from '../regex'
5-
import { url } from '../url'
6-
7-
export const blobsCompact: GitHubLinkifyTransformer = (text: string, repo: string) => {
8-
const replacer = (value, item) => replace(value, item, template('blob', `${ item[1] }/${ item[2] }`, item[3]))
9-
10-
text = regex(text, /\[[\w\d\s`]+]\(https:\/\/github\.com\/([\w\d\-_]+)\/([\w\d\-_]+)\/blob\/([\w\d\/\.\-_]+)\)/g, replacer)
11-
text = regex(text, /https:\/\/github\.com\/([\w\d\-_]+)\/([\w\d\-_]+)\/blob\/([\w\d\/\.\-_]+)/g, replacer)
12-
13-
return text
14-
}
15-
16-
export const blobsExpand: GitHubLinkifyTransformer = (text: string, repo: string) => {
17-
const replacer = (value, item) => value.replace(item[0], url(repo, `${ item[1].includes(repo) ? '' : item[1] + '#' }${ item[2] }`, `${ item[1] }/blob/${ item[2] }`))
18-
19-
text = regex(text, /::blob::([\w\d\-_\/]+)::([\w\d\/\.\-_]+)::/g, replacer)
20-
21-
return text
22-
}
1+
import { Manager } from '../manager'
2+
3+
export const blobsTransformer = Manager.create()
4+
.setKey('blob')
5+
.setCompactPatterns([
6+
/\[[\w\d\s`]+]\(https:\/\/github\.com\/([\w\d\-_]+)\/([\w\d\-_]+)\/blob\/([\w\d\/.\-_]+)\)/g,
7+
/https:\/\/github\.com\/([\w\d\-_]+)\/([\w\d\-_]+)\/blob\/([\w\d\/.\-_]+)/g
8+
])

0 commit comments

Comments
 (0)