Skip to content

Commit

Permalink
Merge pull request #6 from TheDragonCode/1.x
Browse files Browse the repository at this point in the history
Upgrade project structure
  • Loading branch information
andrey-helldar authored Mar 2, 2023
2 parents ad5ec2d + 44c500d commit f54766b
Show file tree
Hide file tree
Showing 17 changed files with 389 additions and 227 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { githubLinkifyPlugin } from 'vuepress-plugin-github-linkify'
}
```

### Support links
### Support Links

* [x] mentions
* [x] commits
Expand Down
8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
<li>https://github.com/Laravel-Lang/common/blob/v14.6.2/src/Services/Renderer/ParagraphRenderer.php</li>
</ul>

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

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

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

<p>**Full Changelog**: 12.18.3...12.8.4</p>
<p>**Full Changelog**: 12.18.3...12.20.4</p>
</td>
</tr>
</table>
Expand Down
7 changes: 0 additions & 7 deletions src/node/plugins/helpers.ts

This file was deleted.

135 changes: 135 additions & 0 deletions src/node/plugins/manager.ts
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)
}
}
7 changes: 0 additions & 7 deletions src/node/plugins/regex.ts

This file was deleted.

156 changes: 156 additions & 0 deletions src/node/plugins/replacer.ts
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
}
}
5 changes: 0 additions & 5 deletions src/node/plugins/template.ts

This file was deleted.

30 changes: 8 additions & 22 deletions src/node/plugins/transformers/blobs.ts
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
])
Loading

0 comments on commit f54766b

Please sign in to comment.