Skip to content

Commit

Permalink
fix(gh): 之前的代码没有渲染type下面的table of repos,另外,优化renderSubComponents() 相关代码
Browse files Browse the repository at this point in the history
  • Loading branch information
xbpk3t committed Dec 23, 2024
1 parent 113db98 commit aa2fbcf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 48 deletions.
Binary file removed alfred/.workflow/icons/repo.png
Binary file not shown.
19 changes: 5 additions & 14 deletions alfred/cmd/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ func buildRepoDescription(repo gh2.Repository) string {
var des strings.Builder

for flag, prefix := range map[bool]string{
repo.IsSubRepo: "Sub",
repo.IsReplacedRepo: "Replaced",
repo.IsRelatedRepo: "Related",
repo.IsSubRepo: "SUB",
repo.IsReplacedRepo: "REP",
repo.IsRelatedRepo: "REL",
} {
if flag {
des.WriteString(fmt.Sprintf("[%s#%s]", prefix, repo.MainRepo))
Expand All @@ -135,31 +135,24 @@ func buildRepoDescription(repo gh2.Repository) string {
// 分为三种情况:
// 1、如果有qs就直接跳转到对应repo
// 2、如果是sub, rep, rel repos 就跳转到对应的主repo
// 3、如果没有qs,也没有上面这几种repos的repo,就直接跳转到type
// 3、如果没有qs,也没有上面这几种repos的repo(说明是某个type下面的repo),就直接跳转到type
func buildDocsURL(repo gh2.Repository) string {
var docsURL strings.Builder
docsPath := wf.Config.Get("docs")

if docsPath == "" {
return ""
}

// 构建基础URL
docsURL.WriteString(fmt.Sprintf("%s/%s#", docsPath, strings.ToLower(repo.Tag)))

// 1. 如果有qs就直接跳转到对应repo
if repo.HasQs() {
docsURL.WriteString(strings.ToLower(pkg.JoinSlashParts(repo.FullName())))
return docsURL.String()
}

// 2. 如果是sub, rep, rel repos就跳转到对应的主repo
if repo.IsSubOrDepOrRelRepo() {
docsURL.WriteString(strings.ToLower(pkg.JoinSlashParts(repo.MainRepo)))
return docsURL.String()
}

// 3. 如果没有qs,也没有上面这几种repos的repo,就直接跳转到type
docsURL.WriteString(strings.ToLower(repo.Type))
return docsURL.String()
}
Expand All @@ -173,10 +166,8 @@ func determineRepoIcon(repo gh2.Repository) string {
return cons.IconQs
case repo.Doc != "":
return cons.IconDoc
case repo.IsStar:
return cons.IconStar
default:
return cons.IconRepo
return cons.IconCheck
}
}

Expand Down
2 changes: 1 addition & 1 deletion alfred/cmd/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func handleWsCommand(cmd *cobra.Command, args []string) {
tk.Name,
tk.Des,
tk.URL,
cons.IconStar,
cons.IconCheck,
)
builder.AddCommonModifiers(item, tk.URL, tk.Des)
}
Expand Down
3 changes: 1 addition & 2 deletions alfred/internal/cons/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package cons

const (
// Icons
IconStar = "icons/check.svg"
IconRepo = "icons/repo.png"
IconCheck = "icons/check.svg"
IconSearch = "icons/search.svg"
IconQs = "icons/a.svg"
IconDoc = "icons/b.svg"
Expand Down
58 changes: 27 additions & 31 deletions service/gh/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,26 @@ import (
// GhRenderer Markdown渲染器
type GhRenderer struct {
render.MarkdownRenderer
Config ConfigRepos
Config ConfigRepos
repoConfigs []repoRenderConfig
}

// 定义仓库类型和对应的渲染配置
type repoRenderConfig struct {
repos Repos
admonitionType render.AdmonitionType
title string
}

// Renderer 相关方法
func NewGhRenderer() *GhRenderer {
return &GhRenderer{}
return &GhRenderer{
repoConfigs: []repoRenderConfig{
{admonitionType: render.AdmonitionTip, title: "Sub Repos"},
{admonitionType: render.AdmonitionWarning, title: "Replaced Repos"},
{admonitionType: render.AdmonitionInfo, title: "Related Repos"},
},
}
}

func (g *GhRenderer) Render(data []byte) (string, error) {
Expand All @@ -30,6 +44,7 @@ func (g *GhRenderer) Render(data []byte) (string, error) {
func (g *GhRenderer) renderContent() (string, error) {
for _, repo := range g.Config {
g.RenderHeader(2, repo.Type)
RenderRepositoriesAsMarkdownTable(repo.Repos)
g.renderRepos(repo.Repos)
}
return g.String(), nil
Expand All @@ -46,44 +61,25 @@ func (g *GhRenderer) renderRepos(repos Repos) {
}

func (g *GhRenderer) renderSubComponents(repo Repository) {
// 渲染子仓库
if len(repo.SubRepos) > 0 {
g.renderSubRepos(repo.SubRepos)
}

if len(repo.ReplacedRepos) > 0 {
g.renderReplacedRepos(repo.ReplacedRepos)
}
reposSlices := []Repos{repo.SubRepos, repo.ReplacedRepos, repo.RelatedRepos}

if len(repo.RelatedRepos) > 0 {
g.renderRelatedRepos(repo.RelatedRepos)
for i, repos := range reposSlices {
if len(repos) > 0 {
config := g.repoConfigs[i]
config.repos = repos
g.renderRepoComponent(config)
}
}

// 渲染命令
if len(repo.Cmd) > 0 {
g.RenderCodeBlock("shell", strings.Join(repo.Cmd, "\n"))
}
}

func (g *GhRenderer) renderSubRepos(repos Repos) {
if len(repos) > 0 {
content := RenderRepositoriesAsMarkdownTable(repos)
g.RenderAdmonition(render.AdmonitionTip, "SubRepos Repos", content)
}
}

func (g *GhRenderer) renderReplacedRepos(repos Repos) {
if len(repos) > 0 {
content := RenderRepositoriesAsMarkdownTable(repos)
g.RenderAdmonition(render.AdmonitionWarning, "Replaced Repos", content)
}
}

func (g *GhRenderer) renderRelatedRepos(repos Repos) {
if len(repos) > 0 {
content := RenderRepositoriesAsMarkdownTable(repos)
g.RenderAdmonition(render.AdmonitionInfo, "Related Repos", content)
}
func (g *GhRenderer) renderRepoComponent(config repoRenderConfig) {
content := RenderRepositoriesAsMarkdownTable(config.repos)
g.RenderAdmonition(config.admonitionType, config.title, content)
}

func (g *GhRenderer) renderQuestions(qs Questions) {
Expand Down

0 comments on commit aa2fbcf

Please sign in to comment.