-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
plugin.ts
89 lines (76 loc) · 3.21 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import * as utils from './utils'
import { LocalCollection, LocalSidecar, RemotePlugin } from './types'
import axios from 'axios'
export class Plugin {
id: string // internal id of plugin
name: string // display name of plugin
description: string // description of plugin if available
index: string // index url of collection
repo: string // repository of collection
author: string // author of plugin
path: string // path to plugin in repository
screenshots: string[] // screenshots of plugin
readme: string | boolean | undefined // readme file
base_path: string // path to plugins/ folder
repo_path: string // path to repository in the format of owner/repo
constructor(defaults: LocalCollection, sidecar: LocalSidecar | undefined, index: RemotePlugin) {
this.id = index.id
this.name = index.name
this.description = index?.metadata?.description
?? sidecar?.description
?? "No Description Provided"
this.index = defaults.index
this.repo = defaults.global_repo
this.author = sidecar?.author
?? defaults?.global_author // fall back to global author
?? "No Author" // if no author
this.path = sidecar?.path
?? index.id // default to ID
this.screenshots = sidecar?.screenshots ?? []
this.readme = defaults?.global_readme ?? sidecar?.readme // readme file
this.base_path = defaults.base_path ?? "main/plugins"
this.repo_path = `${this.base_path}/${this.path}`
}
async checkReadme(): Promise<void> {
// test readme if undefined
if (this.readme === undefined) {
this.readme = await axios.head(`https://github.com/${this.repo}/blob/${this.repo_path}/README.md`, {
validateStatus: status => status == 200 || status == 404
})
.then(res => res.status == 200)
}
}
printMD() {
// pre prepared values
const folderPath = `https://github.com/${this.repo}/tree/${this.repo_path}`
const filePath = `https://github.com/${this.repo}/blob/${this.repo_path}`
// if false, no readme
// if true, default readme
// otherwise, follow path or url
const readme = typeof(this.readme) == "string" // if readme is string
? this.readme.includes("http") // if link, add target blank
? `View [README](${this.readme}){target=_blank}`
: `View [README](${filePath}/${this.readme}){target=_blank}`
: this.readme // readme is boolean
? `View [README](${filePath}/README.md){target=_blank}` // default path
: "No README available"
const screenshots = this.screenshots
.map(screenshot => `![${this.name} screenshot](${screenshot}){ loading=lazy } `)
.join("")
// ugly formatted markdown
return `
### [${this.name}](${folderPath}){target=_blank}
=== "Description"
${utils.sanitizeMD(this.description)}
=== "Source URL"
\`\`\`
${this.index}
\`\`\`
=== "README"
${readme}
=== "Author"
${this.author.replace(/(\[.+\]\(http.+\)(?:,|\r|\n|\r\n))/g, "{target=_blank}")}
=== "Screenshots"
${screenshots}`
}
}